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