#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
const int mod = 1e9 + 7;
int a[N], b[N];
int work(int x) {
  int ret = 0;
  while (x) {
    ret++;
    x >>= 1;
  }
  return ret;
}
int main() {
  int n;
  long long m = 0;
  scanf("%d", &n);
  for (int i = n; i >= 0; i--)
    if (!a[i]) a[i] = ((1 << work(i)) - 1) ^ i, a[((1 << work(i)) - 1) ^ i] = i;
  for (int i = 0; i <= n; i++) m += a[i] ^ i;
  cout << m << endl;
  for (int i = 0; i <= n; i++) cout << a[i] << " ";
  cout << endl;
  return 0;
}
