#include <bits/stdc++.h>
using namespace std;
int a[1000005];
int main() {
  int n;
  long long sum = 0;
  cin >> n;
  memset(a, -1, sizeof(a));
  for (int i = n; i >= 0; i--) {
    if (a[i] != -1)
      continue;
    else {
      for (int tt = 1;; tt *= 2) {
        if (tt - 1 >= i) {
          a[i] = tt - 1 - i;
          a[tt - 1 - i] = i;
          sum = sum + 2 * (tt - 1);
          break;
        }
      }
    }
  }
  cout << sum << endl;
  for (int i = 0; i <= n; i++) {
    if (i != 0) printf(" ");
    printf("%d", a[i]);
  }
  cout << endl;
  return 0;
}
