#include <bits/stdc++.h>
using namespace std;
int ans[1000010];
int a[22];
bool vis[1000010];
void initi() {
  int temp = 1;
  a[0] = 1;
  for (int i = 1; i <= 20; i++) {
    temp *= 2;
    a[i] = temp - 1;
  }
}
int get(int l, int r, int n) {
  int mid = (l + r) / 2;
  if (n == a[mid]) {
    return mid;
  }
  if (r - l == 1) {
    return r;
  }
  if (n < a[mid]) {
    return get(l, mid, n);
  } else {
    return get(mid, r, n);
  }
}
int main() {
  int n;
  long long sum = 0;
  int temp;
  int j;
  initi();
  scanf("%d", &n);
  memset(ans, -1, n + 1);
  memset(vis, false, n + 1);
  temp = get(0, 20, n);
  for (int i = n; i >= 0; i--) {
    j = temp;
    while (vis[a[j] - i] || (a[j] - i) > n) {
      j--;
    }
    ans[i] = a[j] - i;
    sum += a[j];
    vis[a[j] - i] = true;
  }
  printf("%I64d\n", sum);
  for (int i = 0; i <= n; i++) {
    printf("%d ", ans[i]);
  }
  printf("\n");
  return 0;
}
