#include <bits/stdc++.h>
using namespace std;
int n, k;
bool done[1000005] = {0};
int ans[1000005] = {0};
vector<int> adj[1000005];
int maxPowerOfTwo(int x) {
  int i = 0;
  while (x) {
    x /= 2;
    i++;
  }
  return (1 << i) - 1;
}
int main() {
  int T;
  T = 1;
  for (int t = 0; t < T; t++) {
    cin >> n;
    int x = maxPowerOfTwo(n);
    long long sum = 0;
    int i = n;
    if (maxPowerOfTwo(n - 1) == n - 1) {
      i = n - 2;
      sum += (2 * (n + n - 1));
      x = maxPowerOfTwo(n - 1);
      ans[n] = n - 1;
      ans[n - 1] = n;
    }
    for (; i >= 0; i--) {
      if (done[i]) continue;
      x = maxPowerOfTwo(i);
      int s = x ^ i;
      ans[s] = i;
      ans[i] = s;
      sum += (x * 2);
      done[s] = 1;
    }
    cout << sum << endl;
    for (int i = 0; i < n + 1; i++) {
      cout << ans[i] << " ";
    }
  }
}
