#include <bits/stdc++.h>
double eps = 1e-9;
using namespace std;
int arr[1000001];
int high(int n) {
  int last = 0;
  while (n != 0) {
    last++;
    n /= 2;
  }
  return (1 << (last - 1));
}
void func(int n) {
  if (n < 0) {
    return;
  }
  if (n == 0) {
    arr[0] = 0;
    return;
  }
  int last = high(n);
  int k = last - 1;
  for (int i = last; i <= n; i++) {
    arr[i] = k;
    arr[k] = i;
    k--;
  }
  func(k);
}
int main() {
  std::ios::sync_with_stdio(false);
  memset(arr, -1, sizeof(arr));
  long long n;
  cin >> n;
  func(n);
  cout << (n * (n + 1)) << endl;
  for (int i = 0; i <= n; i++) {
    cout << arr[i] << " ";
  }
  return 0;
}
