#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000100;
bool mark[MAXN];
long long r[MAXN];
signed main() {
  long long n;
  cin >> n;
  long long last = n;
  for (long long i = 20; i >= 0; i--) {
    long long x = (1 << i);
    if (x <= n && mark[x] == false) {
      for (long long j = x; j <= last; j++) {
        r[j] = x - (j - x + 1);
        r[x - (j - x + 1)] = j;
        mark[j] = mark[x - (j - x + 1)] = true;
      }
      last = x - (last - x + 1) - 1;
    }
  }
  long long soma = 0;
  for (long long i = 1; i <= n; i++) soma += i;
  cout << 2 * soma << endl;
  for (long long i = 0; i <= n; i++) cout << r[i] << " ";
}
