#include <bits/stdc++.h>
using namespace std;
int n;
int p[1000010];
int main() {
  scanf("%d", &n);
  long long res = n;
  res *= n + 1;
  cout << res << endl;
  int nn = n;
  while (nn >= 0) {
    int m = 1;
    while (m <= nn) m *= 2;
    m--;
    int k = nn;
    while (1) {
      int inv_k = m - k;
      if (inv_k > nn) {
        nn = k;
        break;
      }
      p[k] = inv_k;
      k--;
      if (k < 0) {
        nn = -1;
        break;
      }
    }
  }
  long long test = 0;
  for (int i = 0; i <= n; i++) {
    if (i) cout << " ";
    cout << p[i];
    assert((i & p[i]) == 0);
    test += i ^ p[i];
  }
  assert(test == res);
  cout << endl;
  return 0;
}
