#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 100;
const long long mod = 1e9 + 7;
int a[maxn];
bool ok[maxn];
int h(int x) {
  if (x == 0) {
    return 0;
  }
  for (int i = 23; i >= 0; --i) {
    if ((1 << i) <= x) {
      return (x ^ ((1 << (i + 1)) - 1));
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int p = n;
  while (p >= 0) {
    if (ok[p]) {
      p--;
      continue;
    }
    a[p] = h(p);
    a[h(p)] = p;
    ok[p] = 1;
    ok[h(p)] = 1;
    p--;
  }
  cout << 1LL * n * (n + 1) << endl;
  for (int i = 0; i <= n; ++i) {
    cout << a[i] << ' ';
  }
  cout << endl;
  return 0;
}
