#include <bits/stdc++.h>
using namespace std;
int n;
int a[1000500];
int main() {
  cin >> n;
  n++;
  for (int i = n - 1; i >= 0; i--) {
    int t = i, k = 0, two = 1;
    while (1) {
      if (k + two > i) break;
      if (t % 2 == 0) k += two;
      t /= 2;
      two *= 2;
    }
    for (int j = k; j <= i; j++) a[i - j + k] = j;
    i = k;
  }
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i] ^ i;
  }
  cout << sum << "\n";
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << "\n";
  return 0;
}
