#include <bits/stdc++.h>
using namespace std;
int A[1000010];
int calc(int temp) {
  int pos = 0;
  for (int i = 22; i >= 0; i--)
    if ((temp & (1 << i))) {
      pos = i;
      break;
    }
  int ans = 0;
  for (int i = pos; i >= 0; i--)
    if (!(temp & (1 << i))) ans = (ans | (1 << i));
  return ans;
}
int main() {
  int N;
  cin >> N;
  N++;
  for (int i = 0; i < N; i++) A[i] = -1;
  if (N % 2 == 1) A[0] = 0;
  for (int i = N - 1; i >= 0; i--) {
    if (A[i] != -1) continue;
    int temp = calc(i);
    A[temp] = i;
    A[i] = temp;
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    int temp = i ^ (A[i]);
    ans = ans + (long long)temp;
  }
  cout << ans << endl;
  for (int i = 0; i < N; i++) cout << A[i] << " ";
  cout << endl;
  return 0;
}
