#include <bits/stdc++.h>
using namespace std;
int get(int a) {
  int maxx = 0;
  for (int i = 0; i < 21; i++)
    if (a & (1 << i)) maxx = i;
  int ans = (1 << (maxx + 1)) - 1;
  return (ans ^ a);
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = n; i >= 0; i--) {
    if (a[i] == 0) {
      for (int j = 21; j >= 0; j--) {
        int tmp = get(i);
        if ((i ^ tmp) == i + tmp && a[tmp] == 0) {
          a[tmp] = i;
          a[i] = tmp;
          break;
        }
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i <= n; i++) ans += (a[i] ^ i);
  cout << ans << endl;
  for (int i = 0; i <= n; i++) cout << a[i] << ' ';
  return 0;
}
