#include <bits/stdc++.h>
using namespace std;
long long int arr[1000005];
bool vis[1000005];
long long int digit(long long int val) {
  long long int cnt = 0;
  while (val) {
    val /= 2;
    cnt++;
  }
  return cnt;
}
int main() {
  long long int n, tot, shft, num, get;
  cin >> n;
  tot = (n * (n + 1));
  if (n == 1) {
    cout << tot << endl;
    cout << 1 << " " << 0 << endl;
    return 0;
  }
  for (int i = n; i >= 1; i--) {
    shft = digit(i);
    num = (1 << shft) - 1;
    if (!vis[i]) {
      get = num ^ i;
      arr[i] = get;
      arr[get] = i;
      vis[get] = vis[i] = true;
    }
  }
  cout << tot << endl;
  cout << arr[0];
  for (int i = 1; i <= n; i++) {
    cout << " " << arr[i];
  }
  cout << endl;
  return 0;
}
