#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, BIT = 23;
int n, a[N], two[BIT];
bool mark[N];
int32_t main() {
  two[0] = 1;
  for (int i = 1; i < BIT; i++) two[i] = 2 * two[i - 1];
  scanf("%d", &n);
  long long ans = (((long long)n) * ((long long)(n + 1)));
  printf("%lld\n", ans);
  for (int i = 0; i <= n; i++) {
    for (int b = 0; b < BIT; b++)
      if (((i >> b) & 1) == 0 && a[i] + two[b] <= n && !mark[a[i] + two[b]])
        a[i] += two[b];
    printf("%d ", a[i]);
    mark[a[i]] = 1;
  }
  return 0;
}
