#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
struct rec {
  int a, b;
};
long long a[100], d[1000500], col, n, m, i, j, k, r, x, xx, ans, N;
int main() {
  scanf("%d", &n);
  N = n;
  while (1) {
    x = n;
    col = 0;
    a[0] = 0;
    while (x > 0) {
      a[col++] = x % 2;
      x /= 2;
    }
    r = 0;
    for (i = 0; i < col; i++) r += (1 << i) * (1 - a[i]);
    xx = n;
    for (i = r; i <= n; i++) {
      d[i] = xx;
      xx--;
    }
    n = r - 1;
    if (col == 0) break;
  }
  ans = 0;
  for (i = 0; i <= N; i++) ans += i ^ d[i];
  cout << ans << endl;
  for (i = 0; i <= N; i++) cout << d[i] << " ";
  return 0;
}
