#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
int part[N];
int max_bit(int n) {
  int cnt = 0;
  while (n) {
    n >>= 1;
    cnt++;
  }
  return cnt;
}
int mask(int n) { return (1 << max_bit(n)) - 1; }
int main() {
  int n;
  cin >> n;
  memset(part, -1, (n + 2) * sizeof(int));
  long long ans = 0;
  for (int val = n; val >= 1; val--) {
    if (part[val] == -1) {
      int b = (mask(val) & (~(val)));
      assert(part[b] == -1);
      part[b] = val;
      part[val] = b;
      ans += 2 * (val ^ b);
    }
  }
  if (part[0] == -1) part[0] = 0;
  cout << ans << endl;
  cout << part[0];
  for (int i = 1; i <= n; i++) cout << " " << part[i];
  cout << endl;
  return 0;
}
