#include <bits/stdc++.h>
using namespace std;
int main() {
  if (false) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
  int n;
  cin >> n;
  vector<int> v(n + 1, -1);
  int xx = 1048576;
  long long sum = 0;
  for (int i = n; i > 0; i--) {
    if (v[i] >= 0) {
      continue;
    }
    while (xx / 2 > i) {
      xx /= 2;
    }
    int a = i ^ (xx - 1);
    v[i] = a;
    v[a] = i;
    sum += (a + i) * 2;
  }
  if (v[0] < 0) {
    v[0] = 0;
  }
  cout << sum << endl;
  for (int i = 0; i <= n; i++) {
    if (i > 0) {
      cout << " ";
    }
    cout << v[i];
  }
  cout << endl;
  if (false) {
    fclose(stdin);
    fclose(stdout);
  }
  return 0;
}
