#include <bits/stdc++.h>
using namespace std;
void f(int k) {
  if (k == 0) {
    cout << "0";
    return;
  }
  if (k == 1) {
    cout << "1 0";
    return;
  }
  int p = 1, t = k;
  do {
    t = t / 2;
    p = p * 2;
  } while (t != 0);
  if (p - k - 2 >= 0) {
    f(p - k - 2);
    for (int i = k; i >= p - k - 1; i--) cout << ' ' << i;
  } else {
    for (int i = k; i >= 0; i--) {
      cout << i;
      if (i > 0) cout << ' ';
    }
  }
}
int main() {
  int n;
  cin >> n;
  long long k = (long long)n * (long long)(n + 1);
  cout << k << endl;
  f(n);
  return 0;
}
