#include <bits/stdc++.h>
int ct = 0;
int a[1000050];
void solve(int n) {
  if (n == 0) return;
  int nx = (1 << ((int)log2(n) + 1)) - 1, i;
  if (nx - n > 1)
    solve(nx - n - 1);
  else if (nx - n == 1)
    a[ct++] = 0;
  int dif = nx - n;
  for (i = n; i >= dif; i--) a[ct++] = i;
}
int main() {
  int n;
  scanf("%d", &n);
  solve(n);
  int i;
  long long int be = 0;
  for (i = 0; i < ct; i++) be += i ^ a[i];
  printf("%I64d\n", be);
  for (i = 0; i < ct; i++) printf("%d%c", a[i], (i == ct - 1) ? '\n' : ' ');
  return 0;
}
