#include <bits/stdc++.h>
using namespace std;
const long long mode = 1000000000 + 7;
bool mark[1000000 + 10];
long long ans[1000000 + 10];
long long Xo(long long x) {
  if (x == 0) return 0;
  if (x == 1) return 0;
  long long ret = 0;
  long long now = 1;
  while (x) {
    if (x % 2 == 0) ret += now;
    x /= 2;
    now *= 2;
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  long long n, sum = 0;
  cin >> n;
  for (long long i = n; i >= 0; i--) {
    if (!mark[i]) {
      long long now = Xo(i);
      mark[now] = true;
      ans[i] = now;
      ans[now] = i;
      sum += i ^ now;
    } else
      sum += i ^ ans[i];
  }
  cout << sum << endl;
  for (long long i = 0; i <= n; i++) cout << ans[i] << " ";
}
