#include <bits/stdc++.h>
using namespace std;
long long n;
int m, vis[1001010];
long long re[1001010];
long long ans;
int main() {
  long long i;
  while (scanf("%I64d", &n) != EOF) {
    long long tmp = n, ret = 1;
    memset(vis, 0, sizeof(vis));
    for (i = n; i >= 0; i--) {
      if (vis[i]) continue;
      tmp = i;
      ret = 1;
      while (ret <= tmp) ret *= 2;
      ret--;
      re[ret ^ i] = i;
      vis[ret ^ i] = 1;
      re[i] = ret ^ i;
      vis[i] = 1;
    }
    ans = 0;
    for (i = 0; i <= n; i++) ans += (i ^ re[i]);
    printf("%I64d\n", ans);
    for (i = 0; i <= n; i++)
      if (i == n)
        printf("%I64d\n", re[i]);
      else
        printf("%I64d ", re[i]);
  }
}
