#include <bits/stdc++.h>
using namespace std;
int a[100];
int aa[1000005];
bool vis[1000005];
int main() {
  memset(vis, 0, sizeof(vis));
  int n, now, cnt, i;
  scanf("%d", &n);
  long long ans = (long long)n * (n + 1);
  cout << ans << endl;
  now = 1;
  cnt = 0;
  do {
    a[cnt++] = now - 1;
    now <<= 1;
  } while ((now - 1) < n);
  a[cnt++] = now - 1;
  for (i = n; i >= 0; i--) {
    int cc = cnt - 1;
    int u;
    do {
      u = a[cc] - i;
      if (u > n || (i ^ u) != a[cc] || vis[u])
        cc--;
      else
        break;
    } while (1);
    aa[i] = u;
    vis[u] = 1;
  }
  for (i = 0; i < n; i++) printf("%d ", aa[i]);
  printf("%d\n", aa[n]);
  return 0;
}
