#include <bits/stdc++.h>
using namespace std;
int visit[1000001];
int main() {
  int n;
  long long ans = 0;
  int a[] = {1,     2,     4,     8,      16,     32,    64,
             128,   256,   512,   1024,   2048,   4096,  8192,
             16384, 32768, 65536, 131072, 262144, 524288};
  scanf("%d", &n);
  int l = 0, r = sizeof(a) / sizeof(int) - 1, mid;
  int temp = -1;
  while (l <= r) {
    mid = (l + r) / 2;
    if (a[mid] <= n)
      l = mid + 1, temp = mid;
    else
      r = mid - 1;
  }
  for (int j = temp; j >= 0; j--)
    for (int i = a[j]; !visit[i] && i <= n && !visit[a[j] - (i - a[j] + 1)];
         i++)
      ans += (i ^ (a[j] - (i - a[j] + 1))), visit[i] = a[j] - (i - a[j] + 1),
                                            visit[a[j] - (i - a[j] + 1)] = i;
  printf("%I64d\n", ans * 2);
  for (int i = 0; i <= n; i++) printf("%d ", visit[i]);
  putchar(10);
  return 0;
}
