#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:36777216")
using namespace std;
const int INF = ~(1 << 31);
const double EPS = 1;
const double PI = 3.141592653589793;
int inv(int x) {
  int y = 0, i = 0;
  while (x) {
    if (!(x & 1)) {
      y |= (1 << i);
    }
    x >>= 1;
    i++;
  }
  return y;
}
int main() {
  int n;
  cin >> n;
  set<int> s;
  for (int i = 0; i < n; i++) {
    s.insert(i + 1);
  }
  vector<int> p(n + 1, 0);
  long long ans = 0;
  while (!s.empty()) {
    int x = *(--s.end());
    s.erase(x);
    int y = inv(x);
    p[x] = y;
    p[y] = x;
    s.erase(y);
    ans += 2 * (x ^ y);
  }
  cout << ans << endl;
  for (int i = 0; i < n + 1; i++) {
    cout << p[i] << ' ';
  }
  return 0;
}
