#include <bits/stdc++.h>
using namespace std;
int a[1111111];
int main() {
  int n;
  cin >> n;
  int N = n;
  while (N) {
    int b = 1;
    while ((1 << b) <= N) b++;
    int _N = (1 << (b - 1)) - 1;
    for (int i = 1 << (b - 1); i <= N; i++, _N--) {
      a[i] = _N;
      a[_N] = i;
    }
    N = _N;
  }
  long long ans = 0;
  for (int i = 0; i <= n; i++) ans += a[i] ^ i;
  cout << ans << endl;
  for (int i = 0; i <= n; i++) cout << a[i] << endl;
}
