#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
int p[1111111];
int main() {
  int n;
  cin >> n;
  int b = 1;
  while (b <= n) b <<= 1;
  memset(p, -1, sizeof(p));
  for (int i = n; i >= 0; i--) {
    if (p[i] >= 0) continue;
    while (b - 1 - i > i) b >>= 1;
    int j = b - 1 - i;
    p[j] = i;
    p[i] = j;
  }
  printf("%I64d\n", (long long)n * (n + 1));
  for (int i = 0; i <= n; i++) printf("%d ", p[i]);
  return 0;
}
