#include <bits/stdc++.h>
using namespace std;
int num[1000005] = {};
int main() {
  int n;
  scanf("%d", &n);
  if (n == 1) {
    puts("2");
    puts("1 0");
    return 0;
  }
  if (n == 2) {
    puts("6");
    puts("0 2 1");
    return 0;
  }
  int binary[21];
  binary[0] = 1;
  for (int i = 1; i <= 20; i++) {
    binary[i] = binary[i - 1] * 2;
  }
  vector<pair<int, int> > vec;
  int ru = n, id;
  while (1) {
    for (int i = 1; i <= 20; i++) {
      if (ru <= binary[i] - 1) {
        id = i;
        break;
      }
    }
    vec.push_back(make_pair(binary[id] - 1 - ru, ru));
    ru = binary[id] - 2 - ru;
    if (ru < 1) break;
  }
  for (int i = 0; i < vec.size(); i++) {
    int f = vec[i].first;
    int s = vec[i].second;
    for (int i = f; i <= s; i++) {
      num[i] = s + f - i;
    }
  }
  long long ret;
  ret = (long long)n * (long long)(n + 1);
  printf("%lld\n", ret);
  for (int i = 0; i <= n; i++) {
    printf("%d%c", num[i], i == n ? '\n' : ' ');
  }
}
