#include <bits/stdc++.h>
using namespace std;
int a[1000005];
void work(int x) {
  if (x <= 0) return;
  int t = 1;
  while (t <= x) t *= 2;
  t--;
  int st = t - x;
  for (int i = st; i <= x; i++) a[i] = t - i;
  work(st - 1);
}
int main() {
  int n;
  scanf("%d", &n);
  printf("%I64d\n", (long long)n * (n + 1));
  work(n);
  for (int i = 0; i <= n; i++) printf("%d ", a[i]);
  return 0;
}
