#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000010;
int a[MAXN];
bool used[MAXN];
int f[30];
void init() {
  f[0] = 1;
  for (int i = 1; i <= 20; i++) {
    f[i] = (f[i - 1] << 1);
  }
}
int calc(int x) {
  if (x == 0) return 0;
  int t = 0;
  while (f[t] <= x) t++;
  return (f[t] - 1) ^ x;
}
int main() {
  init();
  int n;
  while (scanf("%d", &n) == 1) {
    memset(used, false, sizeof(used));
    long long ans = 0;
    for (int i = n; i >= 0; i--) {
      if (used[i]) continue;
      int x = calc(i);
      used[i] = true;
      used[x] = true;
      a[i] = x;
      a[x] = i;
      ans += 2 * (x ^ i);
    }
    cout << ans << endl;
    for (int i = 0; i <= n; i++) {
      if (i == n)
        printf("%d\n", a[i]);
      else
        printf("%d ", a[i]);
    }
  }
  return 0;
}
