#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void smn(T &a, const T &b) {
  if (b < a) a = b;
}
template <class T>
inline void smx(T &a, const T &b) {
  if (b > a) a = b;
}
template <class T>
inline T _rev(const T &a) {
  T _ = a;
  reverse(_.begin(), _.end());
  return _;
}
const double eps = 1e-8;
const long double leps = 1e-14;
const int MN = 1000 * 1000 + 1000;
int undo[MN];
int now[MN];
int n;
void build(int a, int t) {
  if (t == 0) return;
  int last = 0;
  for (int i = 0; i < 25; i++)
    if ((1 << i) <= t)
      last = i;
    else
      break;
  int p = (1 << last);
  build(a + p, t - p);
  int temp = a + p - 1;
  for (int i = a; i < a + p; i++) now[i] = temp, undo[temp] = i, temp--;
  for (int i = a + p; i < a + t; i++) {
    int temp = now[i];
    temp -= p;
    now[i] = temp;
    now[undo[temp]] = temp + p;
    undo[temp + p] = undo[temp];
    undo[temp] = i;
  }
}
int main() {
  cin >> n;
  build(0, n + 1);
  long long res = 0;
  for (int i = 0; i <= n; i++) res += i ^ now[i];
  cout << res << '\n';
  for (int i = 0; i <= n; i++) printf("%d ", now[i]);
  printf("\n");
  return 0;
}
