#include <bits/stdc++.h>
using namespace std;
int p[1000111];
int main() {
  int n;
  scanf("%d", &n);
  memset(p, -1, sizeof(p));
  vector<int> vec;
  for (int i = (n); i >= (0); i--) {
    if (p[i] != -1) continue;
    int tmp = i;
    int tmp1 = 0;
    int j = 0;
    while (tmp > 0) {
      if (tmp % 2 == 0) tmp1 += (1 << j);
      j++;
      tmp /= 2;
    }
    if (tmp1 > n) vec.push_back(i);
    p[tmp1] = i;
    p[i] = tmp1;
  }
  if (vec.size() != 0) {
    int mx = 0, x;
    for (int i = 0; i < (vec.size()); i++) {
      if (p[vec[i]] != -1) continue;
      for (int j = 0; j < (vec.size()); j++) {
        if (p[vec[j]] != -1) continue;
        if (mx < (vec[i] ^ vec[j])) {
          mx = vec[i] ^ vec[j];
          x = j;
        }
      }
      p[vec[i]] = vec[x];
      p[vec[x]] = vec[i];
    }
  }
  long long ans = 0;
  for (int i = 0; i < (n + 1); i++) ans += (i ^ p[i]);
  printf("%I64d\n", ans);
  for (int i = 0; i < (n + 1); i++) printf("%d ", p[i]);
  puts("");
  return 0;
}
