#include <bits/stdc++.h>
using namespace std;
int a[1100010];
int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  int p = 1;
  while (p < 1100010) {
    a[p - 1] = p - 1;
    p *= 2;
  }
  for (int i = 1100010 - 2; i; i--) {
    if (a[i] == 0) a[i] = a[i + 1];
  }
  int ans[n + 1];
  memset(ans, -1, sizeof(ans));
  long long res = 0;
  for (int i = n; i >= 0; i--) {
    if (ans[i] == -1) {
      ans[i] = a[i] - i;
      ans[a[i] - i] = i;
      res += 2 * (long long)(i ^ (a[i] - i));
    }
  }
  cout << res << endl;
  for (int i : ans) cout << i << " ";
  return 0;
}
