{$I-,Q-,R-,S-,D-,L-,O+}
{$I+,Q+,R+,S+,D+,L+,O-}
program _I;
{$APPTYPE CONSOLE}
const
  inf = 'irrelevant.in';
  ouf = 'irrelevant.out';
  maxn = 100002;

type
  int = longint;

var
  a : array [0..1] of array [0..maxn] of int;
  ans : array [0..maxn] of int;
  i, col, n, m : int;
  cur : int64;

procedure solve2;
begin
  col := 0;
  if(n = 1)then exit;
  if(n = 2)then exit;
  cur := 1;
  for i := 0 to n - 2 do begin
    cur := (cur * (n - 1 - i)) div (i + 1);
    if cur mod m = 0 then begin
      inc(col);
      ans[col] := i;
      cur := (2 + ((n - 1) div m)) * m ;
    end
    else begin
      cur := (cur mod m) + m * (n - 1);
    end;
  end;
end;

procedure solve;
var
  tmp, cur, last, i, j : int;
begin
  last := 0;
  cur := 0;
  a[last][1] := 1;
  for i := 2 to n do begin
    cur := 1 - last;
    if(not odd(i))then begin
      for j := 1 to (i + 1) shr 1 do begin
        tmp := a[last][j] + a[last][j - 1];
        while tmp >= m do dec(tmp, m);
        a[cur][j] := tmp;
      end;
    end else begin
      for j := 1 to (i + 1) shr 1 - 1 do begin
        tmp := a[last][j] + a[last][j - 1];
        while tmp >= m do dec(tmp, m);
        a[cur][j] := tmp;
      end;
      j := (i + 1) shr 1;
      tmp := a[last][j - 1] shl 1;
      while tmp >= m do dec(tmp, m);
      a[cur][j] := tmp;
    end;
    last := cur;
  end;
  for i := 1 to (n + 1) shr 1 do
    if a[cur][i] = 0 then begin
      inc(col);
      ans[col] := i;
    end;
  for i := 1 to col do if ans[i] <> n - ans[i] + 1 then begin
    inc(ans[0]);
    ans[ans[0] + col] := n - ans[i] + 1;
  end;
  col := ans[0] + col;
end;

begin
  assign(input, inf);
  reset(input);
  read(n, m);
  close(input);

  assign(output, ouf);
  rewrite(output);

  solve;

  writeln(col);
  for i := 1 to col - 1 do write(ans[i], ' ');
  write(ans[col]);
  close(output);
end.
