#include <bits/stdc++.h>
int ans[1000001];
unsigned long long n;
using namespace std;
int main() {
  cin >> n;
  memset(ans, -1, sizeof(ans));
  for (int i = n; i >= 0; i--)
    if (ans[i] == -1) {
      unsigned long long comp =
          ~i & ((1ull << (unsigned long long)log2(i)) - 1);
      ans[i] = comp;
      ans[comp] = i;
    }
  cout << n * (n + 1) << "\n";
  for (int i = 0; i <= n; i++) printf("%d ", ans[i]);
  return 0;
}
