#include <bits/stdc++.h>
using namespace std;
int num[1000005];
int f(int x) {
  int tmp = x, i = 0;
  while (tmp) {
    tmp >>= 1;
    i++;
  }
  return i;
}
int main() {
  int n, x, tmp, cnt, s, m;
  long long sum = 0;
  scanf("%d", &n);
  tmp = n;
  while (tmp > 0) {
    x = f(tmp);
    m = (1 << x) - 1;
    s = m - tmp;
    cnt = (tmp - (m >> 1)) * 2;
    while (cnt > 0) {
      num[tmp--] = s++;
      sum += m;
      cnt--;
    }
  }
  cout << sum << endl;
  for (int i = 0; i <= n; i++) {
    cout << num[i];
    if (i != n) cout << " ";
  }
  cout << endl;
}
