#include <bits/stdc++.h>
using namespace std;
int Pair[1000005];
bool t2[1000005];
int main() {
  long long n;
  cin >> n;
  int t = 0;
  while (t < n) {
    t2[t] = true;
    t = 2 * t + 1;
  }
  cout << n * (n + 1) << endl;
  int i = n;
  while (i >= 0) {
    int nextT = 0;
    for (int j = i; j >= t - i; j--) {
      Pair[j] = t - j;
      if (t2[j]) nextT = j;
    }
    i = t - i - 1;
    t = (t2[i]) ? i : nextT;
  }
  for (int i = 0; i <= n; i++) cout << Pair[i] << ' ';
  cout << endl;
  return 0;
}
