#include <bits/stdc++.h>
using namespace std;
template <class T, class L>
bool smax(T &x, L y) {
  return x < y ? (x = y, 1) : 0;
}
template <class T, class L>
bool smin(T &x, L y) {
  return y < x ? (x = y, 1) : 0;
}
const int maxn = 1e6 + 17;
bool mark[maxn];
int a[maxn], n;
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  cout << (long long)n * (n + 1) << '\n';
  for (int i = n; i; i--) {
    if (mark[i]) continue;
    int b = 1;
    while (b < i) b = b << 1 | 1;
    int rev = b ^ i;
    assert(!mark[rev]);
    mark[rev] = 1;
    a[i] = rev, a[rev] = i;
  }
  int fac = 0;
  for (int i = 0; i <= n; i++) cout << a[i] << ' ', fac += i ^ a[i];
  cout << '\n';
  return 0;
}
