#include <bits/stdc++.h>
using namespace std;
int ans[1000001];
int bit[21];
int main() {
  for (int i = 0; i < 1000001; i++) ans[i] = -1;
  int n;
  scanf("%d", &n);
  for (int i = n; i >= 0; i--) {
    if (ans[i] != -1) continue;
    int val = i;
    memset(bit, 0, sizeof bit);
    int cnt = 0;
    while (val > 0) {
      bit[cnt++] = val & 1;
      val = val / 2;
    }
    int x = 0;
    for (int j = cnt - 1; j >= 0; j--) {
      x = x * 2 + ((1 + bit[j]) % 2);
    }
    ans[i] = x;
    ans[x] = i;
  }
  long long int cnt = 0;
  for (int i = 0; i <= n; i++) {
    cnt = cnt + (ans[i] ^ i);
  }
  printf("%lld\n", cnt);
  for (int i = 0; i <= n; i++) {
    printf("%d ", ans[i]);
  }
}
