#include <bits/stdc++.h>
using namespace std;
long long n, ans, a[1000005], v[1000005];
long long get(long long x) {
  long long len = (long long)log2(x) + 1;
  long long s = (1 << len) - 1;
  return s - x;
}
int main() {
  scanf("%I64d", &n);
  for (long long i = 1; i <= n; i++) a[i] = i;
  for (long long i = n; i; i--)
    if (!v[i]) {
      long long x = get(i);
      v[x] = 1;
      swap(a[x], a[i]);
      ans += x + i + x + i;
    }
  printf("%I64d\n", ans);
  for (long long i = 0; i <= n; i++) printf("%I64d ", a[i]);
}
