#include <bits/stdc++.h>
int Func(std::vector<int>& data, int exp2, int n) {
  int i = exp2;
  int j = exp2 - 1;
  for (; i <= n && j >= 0; ++i, --j) {
    data[i] = j;
    data[j] = i;
  }
  return j;
}
int GetNearestExp2(int n) {
  if (n == 0) return 0;
  int exp = 0;
  while (n != 0) {
    n = (n >> 1);
    ++exp;
  }
  if (exp == 1) return 1;
  return 1 << (exp - 1);
}
int main() {
  int n;
  std::cin >> n;
  std::vector<int> data(n + 1);
  int exp2 = GetNearestExp2(n);
  while (exp2 > 0) {
    n = Func(data, exp2, n);
    if (n <= 0) {
      if (n == 0) data[0] = 0;
      break;
    }
    exp2 = GetNearestExp2(n);
  }
  long long sum = 0;
  for (size_t i = 0; i < data.size(); ++i)
    sum += static_cast<long long>(data[i] ^ i);
  std::cout << sum << std::endl;
  for (size_t i = 0; i < data.size(); ++i) std::cout << data[i] << " ";
  return 0;
}
