#include <bits/stdc++.h>
using namespace std;
int n;
long long ans[1000005];
int main() {
  int i;
  long long x, sum;
  while (~scanf("%d", &n)) {
    x = 1;
    while (x <= n) x <<= 1;
    x--;
    memset(ans, -1, sizeof(ans));
    for (i = n; i >= 0; i--) {
      if (ans[i] != -1) continue;
      while ((x ^ i) > n || ans[x ^ i] != -1) x >>= 1;
      ans[x ^ i] = i, ans[i] = x ^ i;
    }
    sum = 0;
    for (i = 0; i <= n; i++) sum += i ^ ans[i];
    printf("%I64d\n", sum);
    for (i = 0; i <= n; i++) printf("%I64d ", ans[i]);
    printf("\n");
  }
  return 0;
}
