#include <bits/stdc++.h>
int taken[1000001];
int a[1000001];
int main() {
  int i, n;
  long long msk;
  long long sum = 0;
  scanf("%d", &n);
  for (i = 0; i < n; i++) taken[i] = 0;
  a[0] = 0;
  for (i = n; i > 0; i--) {
    if (!taken[i]) {
      msk = 1;
      while (msk <= i) msk *= 2;
      msk--;
      a[i] = i ^ msk;
      a[i ^ msk] = i;
      taken[i ^ msk] = 1;
      sum += msk * 2;
    }
  }
  printf("%I64d\n", sum);
  for (i = 0; i <= n; i++) printf("%d ", a[i]);
  printf("\n");
  return 0;
}
