#include <bits/stdc++.h>
using namespace std;
int a[1111111], i, j, n, m, b[321];
int main() {
  cin >> n;
  m = n;
  long long ans = n;
  while (n > 0) {
    int t = 1, tt;
    while (t <= n) t *= 2;
    t /= 2;
    tt = t - 1;
    for (; t <= n; t++, tt--) {
      a[t] = tt;
      a[tt] = t;
    }
    n = tt;
  }
  cout << ans * (ans + 1) << endl;
  for (i = 0; i <= m; i++) cout << a[i] << ' ';
}
