#include <bits/stdc++.h>
using namespace std;
vector<bool> used(1000005, false);
int p[1000005];
int main() {
  long long n;
  int y;
  cin >> n;
  cout << n * (n + 1) << "\n";
  for (int i = n; i >= 0; i--) {
    if (used[i]) continue;
    if (i == 0) {
      p[i] = i;
      continue;
    }
    y = __builtin_clz(i);
    y = 32 - y;
    int p2 = (1 << y);
    p2--;
    p[i] = p2 - i;
    p[p2 - i] = i;
    used[i] = used[p2 - i] = true;
  }
  for (int i = 0; i <= n; i++) printf("%d ", p[i]);
}
