#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const double eps = 1e-9;
const int N = 2e6 + 6;
bool taken[N];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, mxBits = 0;
  cin >> n;
  long long ans = 0;
  while ((1 << mxBits) <= n) mxBits++;
  vector<int> res(n + 1);
  for (int i = n; i >= 0; --i) {
    int cr = 0;
    vector<int> all;
    all.push_back(cr);
    for (int k = 0; k < mxBits; k++) {
      if ((i & (1 << k)) == 0 && (cr | (1 << k)) <= n) {
        cr |= (1 << k);
        all.push_back(cr);
      }
    }
    for (int k = ((int)(all).size()) - 1; k >= 0; k--) {
      if (!taken[all[k]]) {
        cr = all[k];
        break;
      }
    }
    taken[cr] = 1;
    ans += cr ^ i;
    res[i] = cr;
  }
  cout << ans << endl;
  for (int i = 0; i < n + 1; i++) cout << res[i] << " ";
  return 0;
}
