#include <bits/stdc++.h>
using namespace std;
int check[1000010];
void solve(int x) {
  int n = x;
  if (n <= 0) return;
  int p;
  for (int i = 20; i >= 0; i--) {
    if ((n & (1 << i)) != 0) {
      p = i;
      break;
    }
  }
  int m = (1 << p);
  for (int i = m; i <= n; i++) {
    int x = i - m;
    check[m - 1 - x] = i;
    check[i] = m - 1 - x;
  }
  n = m - 1 - (n - m) - 1;
  solve(n);
}
int main() {
  int n;
  scanf("%d", &n);
  printf("%lld\n", 1ll * n * (n + 1));
  solve(n);
  for (int i = 0; i <= n; i++) printf("%d ", check[i]);
}
