#include <bits/stdc++.h>
using namespace std;
int ans[1000449];
bool flag[1000449];
int main() {
  int n;
  cin >> n;
  int k = 1;
  int temp = n;
  while (temp > 0) {
    temp = temp >> 1;
    k = k << 1;
  }
  for (int i = 0; i <= n; i++) flag[i] = true;
  for (int i = n; i >= 0; i--) {
    int temp_k = k - 1;
    while ((temp_k ^ i) > n || flag[temp_k ^ i] == false)
      temp_k = ((temp_k + 1) >> 1) - 1;
    ans[i] = temp_k ^ i;
    flag[temp_k ^ i] = false;
  }
  long long total = 0;
  for (int i = 0; i <= n; i++) total += ans[i] ^ i;
  cout << total << endl;
  for (int i = 0; i < n; i++) printf("%d ", ans[i]);
  cout << ans[n] << endl;
  return 0;
}
