#include <bits/stdc++.h>
using namespace std;
int pow2[30];
int ar[1000900], mark[1000090];
int main() {
  int n;
  int a, b;
  pow2[0] = 1;
  for (int i = 1, _n = 30; i < _n; i++) pow2[i] = pow2[i - 1] * 2;
  while (cin >> n) {
    memset(mark, -1, sizeof(mark));
    int m = n - 1, r = 0;
    while (m > 1) {
      if (m % 2) r = 1;
      m /= 2;
    }
    if (n != 2 && !r) {
      mark[1] = 0;
      mark[0] = 1;
    }
    for (int i = n; i >= 0; i--) {
      if (mark[i] == -1) {
        int hi = 29, low = 0, x = 0;
        while (hi >= low) {
          int mid = (hi + low) / 2;
          if (pow2[mid] > i) {
            hi = mid - 1;
          } else if (pow2[mid] <= i)
            low = mid + 1, x = pow2[mid];
        }
        int d = i - x;
        mark[i] = x - d - 1;
        mark[x - d - 1] = i;
      }
    }
    long long ret = 0;
    for (int i = 0, _n = n + 1; i < _n; i++) ret += (i ^ max(mark[i], 0));
    cout << ret << endl;
    for (int i = 0, _n = n + 1; i < _n; i++) {
      if (i) cout << " ";
      if (mark[i] == -1) mark[i] = 0;
      cout << mark[i];
    }
    cout << endl;
  }
  return 0;
}
