#include <bits/stdc++.h>
using namespace std;
int cbit(int n) {
  int cc = 0;
  while (n) {
    cc++;
    n >>= 1;
  }
  return cc;
}
int n, t1, t2;
int main(void) {
  ios_base::sync_with_stdio(0);
  cin >> n;
  vector<int> v(n + 1, -1);
  long long t3 = 0;
  for (int i = n; i >= 0; i--) {
    if (v[i] != -1) continue;
    t1 = cbit(i);
    t2 = (1 << t1) - 1;
    t2 = i ^ t2;
    v[t2] = i;
    v[i] = t2;
    t3 += 2 * (t2 ^ i);
  }
  cout << t3 << endl;
  for (int i(0), _n(n + 1); i < _n; ++i) {
    if (v[i] == -1) v[i] = i;
    cout << v[i] << " ";
  }
  return 0;
}
