#include <bits/stdc++.h>
using namespace std;
int n;
int v[2][1000000 + 10];
void solve(int n, int p) {
  if (n == 0) {
    v[p][0] = 0;
    return;
  }
  if (!(n & (n + 1))) {
    for (int i = 0; i <= n; ++i) {
      v[p][i] = n - i;
    }
  } else {
    int two = 1;
    while (two * 2 < n + 1) two *= 2;
    solve(n - two, p ^ 1);
    for (int i = 0; i <= n - two; ++i) {
      v[p][i] = two + v[p ^ 1][i];
    }
    int cur = two;
    for (int i = two; i <= n; ++i) {
      --cur;
      v[p][i] = cur;
    }
    for (int i = n - two + 1; i < two; ++i) {
      --cur;
      v[p][i] = cur;
    }
  }
}
int main() {
  cin >> n;
  long long s = 0;
  for (int i = 0; i <= n; ++i) s += i;
  cout << s + s << endl;
  solve(n, 0);
  for (int i = 0; i <= n; ++i) {
    printf("%d ", v[0][i]);
  }
  return 0;
}
