#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000 * 1000 + 10;
int d[MAX], ans[MAX];
int main() {
  ios::sync_with_stdio(false);
  int n, b = 1;
  cin >> n;
  while (b <= n) b *= 2;
  b--;
  fill(ans, ans + n + 1, -1);
  long long javab = 0;
  for (int i = (int)n; i > (int)-1; --i) {
    if (ans[i] == -1) {
      while (b > 0 and ans[b - i] != -1) b /= 2;
      ans[i] = b - i;
      ans[b - i] = i;
      javab += (long long)b * 2;
    }
  }
  cout << javab << endl;
  for (int i = (int)0; i < (int)n + 1; ++i) cout << ans[i] << " ";
  cout << endl;
}
