#include <bits/stdc++.h>
using namespace std;
int out[1000006];
int get_top(int x) {
  int tmp = 1;
  while (tmp <= x) tmp *= 2;
  return tmp - 1;
}
int main() {
  int n;
  scanf("%d", &n);
  printf("%I64d\n", (long long)n * (n + 1));
  int a = n, b = get_top(n) - n;
  int next = b - 1;
  while (1) {
    while (a >= b) {
      out[a] = b, out[b] = a;
      a--, b++;
    }
    if (next == -1) break;
    a = next;
    b = get_top(a) - a;
    next = b - 1;
  }
  for (int i = 0; i <= n; i++) printf("%d%c", out[i], i == n ? '\n' : ' ');
  return 0;
}
