#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, const U &b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, const U &b) {
  if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
template <class T1, class T2>
inline void gn(T1 &x1, T2 &x2) {
  gn(x1), gn(x2);
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
int p[1000100];
int main() {
  int n;
  cin >> n;
  memset(p, -1, sizeof(int) * (n + 1));
  for (int i = n; i >= 0; i--) {
    if (p[i] >= 0) continue;
    if (i == 0) {
      p[i] = 0;
      break;
    }
    int k = 32 - __builtin_clz(i);
    int first = (1 << k) - 1 ^ i;
    p[first] = i;
    p[i] = first;
  }
  cout << (long long)n * (n + 1) << endl;
  for (int i = 0; i <= n; i++) printf("%d ", p[i]);
  return 0;
}
