#include <bits/stdc++.h>
using namespace std;
char rd;
int sig;
template <class T>
inline void read(T &a) {
  for (a = 0, rd = getchar(); ('0' > rd || rd > '9') && rd != '-';
       rd = getchar())
    ;
  for (rd == '-' ? sig = -1, rd = getchar() : sig = 1; '0' <= rd && rd <= '9';)
    a *= 10, a += rd - '0', rd = getchar();
  a *= sig;
}
int n, a[10000005];
inline void work(int k) {
  if ((k & (k + 1)) == 0) {
    for (int i = (0); i <= (k); ++i) a[i] = k - i;
    return;
  }
  int t = 1, kk = k, p;
  for (; kk > 1; kk >>= 1, t <<= 1)
    ;
  p = k - t;
  for (int i = (0); i <= (p); ++i) a[t + i] = t - i - 1, a[t - i - 1] = t + i;
  work(t - p - 2);
}
int main() {
  read(n);
  work(n);
  long long ans = 0;
  for (int i = (0); i <= (n); ++i) ans = ans + (a[i] ^ i);
  printf("%I64d\n", ans);
  for (int i = (0); i <= (n); ++i) printf("%d ", a[i]);
  puts("");
  return 0;
}
