#include <bits/stdc++.h>
using namespace std;
long long a[1000001];
int main() {
  long long n;
  cin >> n;
  cout << n * (n + 1) << '\n';
  for (int i = n; i >= 0; i--) {
    int x = 0;
    if (a[i]) continue;
    for (int j = 0; j <= 32; j++) {
      if ((1ll << (j)) > i) {
        x = j;
        break;
      }
    }
    int y = i ^ ((1ll << (x)) - 1);
    a[i] = y;
    a[y] = i;
  }
  for (int i = 0; i <= n; i++) printf("%d ", a[i]);
  return 0;
}
