#include <bits/stdc++.h>
using namespace std;
int P[(1 << 20) + 10];
void solve(int N) {
  int cnt = 1;
  while (cnt - 1 < N) cnt *= 2;
  cnt--;
  int now = cnt - N;
  for (int i = (N); i > (-1); --i) {
    P[i] = now++;
    if (now > N) {
      solve(i - 1);
      break;
    }
  }
}
int main() {
  int N;
  while (cin >> N) {
    solve(N);
    long long ans = 0;
    for (int i = (0); i < (N + 1); ++i) ans += (P[i] ^ i);
    cout << ans << endl;
    for (int i = (0); i < (N + 1); ++i)
      if (i == N)
        cout << P[i] << endl;
      else
        cout << P[i] << " ";
  }
  return 0;
}
