#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10, maxm = (1 << 17), mod = 1e9 + 7, hash = 1239;
const double PI = 3.14159265359;
int a[maxn];
long long ans, d[110];
void find(int n) {
  int b = lower_bound(d, d + 25, n) - d;
  if (n < 1) return;
  if (d[b] == n) {
    a[n - 1] = n, a[n] = n - 1;
    ans += 2 * (n ^ (n - 1));
    find(n - 2);
    return;
  }
  int m = d[b] - n;
  for (int i = m - 1; i <= n; i++) {
    a[i] = n - i + m - 1, a[n - i + m - 1] = i;
    ans += (i ^ (n - i + m - 1));
  }
  find(m - 2);
}
int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n, d[0] = 1;
  for (int i = 1; i < 30; i++) d[i] = d[i - 1] * 2;
  find(n);
  cout << ans << endl;
  for (int i = 0; i <= n; i++) cout << a[i] << " ";
  return 0;
}
