#include <bits/stdc++.h>
using namespace std;
int a[1000002], n, num, rec;
long long ans;
void deal(int n) {
  if (n < 0) return;
  int num, rec, i;
  num = 1;
  while (num - 1 < n) num <<= 1;
  num--;
  rec = num - n;
  for (i = rec; i <= n; i++) {
    a[i] = num - i;
    a[num - i] = i;
  }
  deal(rec - 1);
}
int main() {
  int i, j, k, m, t;
  scanf("%d", &n);
  deal(n);
  ans = 0;
  for (i = 0; i <= n; i++) ans += a[i] ^ i;
  cout << ans << endl;
  for (i = 0; i <= n - 1; i++) printf("%d ", a[i]);
  printf("%d\n", a[n]);
}
