#include <bits/stdc++.h>
using namespace std;
vector<long long> val;
long long answer(long long l, long long r, long long bitnum) {
  if (bitnum < 0) return 0;
  if (r < l) return 0;
  long long bitl = (1 & (l >> bitnum));
  long long bitr = (1 & (r >> bitnum));
  if (bitl) assert(false);
  if (!bitr) {
    return answer(l, r, bitnum - 1);
  }
  long long X = (1 << bitnum);
  long long Y = X - 1;
  long long ans = 0;
  while (X <= r) {
    val[X] = Y;
    val[Y] = X;
    ans += X ^ Y;
    X++;
    Y--;
  }
  return ans + answer(l, Y, bitnum - 1);
}
void solve() {
  long long n;
  cin >> n;
  val.resize(n + 1);
  cout << 2 * answer(0, n, 22) << '\n';
  for (long long i = 0; i < n + 1; i++) cout << val[i] << " ";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
