#include <bits/stdc++.h>
using namespace std;
int a[1000010];
int find(int k) {
  int sit = 0, tem = 1;
  while (k >= tem) {
    sit++;
    tem *= 2;
  }
  for (int i = k; i >= tem - 1 - k; i--) a[i] = tem - 1 - i;
  return tem - 1 - k - 1;
}
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    memset(a, 0, sizeof(a));
    int t = n;
    while (find(t) != -1) {
      t = find(t);
    }
    long long ans = 0;
    for (int i = 0; i <= n; i++) ans += i ^ a[i];
    printf("%I64d\n", ans);
    for (int i = 0; i <= n; i++) printf("%d ", a[i]);
    printf("\n");
  }
  return 0;
}
