#include <bits/stdc++.h>
using namespace std;
int ar[1000005];
int n, m;
void solve() {
  scanf("%d", &n);
  memset(ar, -1, sizeof(ar));
  for (int i = n; i >= 0; i--) {
    if (ar[i] != -1) continue;
    bool f = false;
    int another = 0;
    for (int j = 25; j >= 0; j--) {
      if ((1 << j) & i)
        f = true;
      else {
        if (f) another |= (1 << j);
      }
    }
    ar[another] = i;
    ar[i] = another;
  }
  printf("%lld\n", 1LL * n * (n + 1));
  for (int i = 0; i <= n; i++) {
    printf("%d ", ar[i]);
  }
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
