#include <bits/stdc++.h>
using namespace std;
long long solve(long long val) {
  int tmp = 0, ans = 0;
  for (int i = 0; i < 21; i++) {
    if (val & (1 << i)) tmp = i;
  }
  for (int i = 0; i < tmp; i++) {
    if (val & (1 << i)) continue;
    ans |= (1 << i);
  }
  return ans;
}
long long n, ans[1000010], anss = 0;
int main() {
  cin >> n;
  long long to = solve(n), m = n;
  while (1) {
    long long fro = to, bac = m;
    while (fro <= bac) {
      ans[fro] = bac;
      ans[bac] = fro;
      fro++;
      bac--;
    }
    if (to == 0)
      break;
    else {
      m = to - 1;
      to = solve(m);
    }
  }
  for (int i = 0; i <= n; i++) anss += i ^ ans[i];
  cout << anss << '\n';
  for (int i = 0; i <= n; i++) {
    cout << ans[i] << ' ';
  }
  cout << '\n';
  return 0;
}
