#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 temp = cnt - N;
  for (int i = N; i >= 0; i--) {
    P[i] = temp++;
    if (temp > N) {
      solve(i - 1);
      return;
    }
  }
}
int main() {
  int N;
  cin >> N;
  memset(P, 0, sizeof(P));
  solve(N);
  long long ans = 0;
  for (int i = (0); i < (N + 1); ++i) ans += (long long)(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;
}
