#include <bits/stdc++.h>
using namespace std;
long long par[1000006];
bool vis[1000006];
int main() {
  long long n;
  while (scanf("%lld", &n) == 1) {
    memset(vis, 0, sizeof vis);
    par[0] = 0;
    for (long long i = n; i > 0; i--) {
      if (!vis[i]) {
        for (long long j = 20;; j--) {
          if (i & (1ll << (j))) {
            long long mx = ((1ll << (j + 1)) - 1ll) - i;
            par[i] = mx;
            par[mx] = i;
            vis[i] = vis[mx] = 1;
            break;
          }
        }
      }
    }
    long long sum = 0;
    for (long long i = 0; i <= n; i++) sum += (i ^ par[i]);
    printf("%lld\n", sum);
    for (long long i = 0; i <= n; i++) printf("%lld ", par[i]);
    printf("\n");
  }
  return 0;
}
