#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<bool> f(n + 1, false);
  vector<int> p(n + 1, -1);
  int mask = n;
  long long ans = 0;
  while ((mask + 1) & mask) mask |= mask >> 1;
  for (int i = n; i >= 0; --i) {
    while (mask - i > n || f[mask - i]) mask >>= 1;
    p[i] = mask - i;
    f[mask - i] = true;
    ans += mask;
  }
  cout << ans << endl;
  for (int i = 0; i < n + 1; ++i) {
    cout << p[i] << " ";
  }
  cout << endl;
  return 0;
}
