#include <bits/stdc++.h>
using namespace std;
int BIN[30], P[1000005], vis[1000005];
int _bin(int n) {
  int i, now;
  for (i = 0; BIN[i] <= n; i++) now = BIN[i];
  return BIN[i];
}
int main() {
  int i, j, n, k, now, t;
  long long ans = 0;
  for (i = 1, BIN[0] = 1; i < 30; i++) BIN[i] = BIN[i - 1] * 2;
  for (scanf("%d", &n), i = n; i; i--)
    if (vis[i] == 0) {
      now = _bin(i);
      t = (now - 1) ^ i;
      ans += t + i + t + i;
      P[i] = t, P[t] = i;
      vis[t] = 1;
    }
  for (i = 0, printf("%I64d\n", ans); i <= n; i++)
    printf("%d%c", P[i], i == n ? '\n' : ' ');
  return 0;
}
