#include <bits/stdc++.h>
using namespace std;
int f[1000009];
int an[1000009];
int bit(int x) {
  int ans = 0;
  while (x) {
    x >>= 1;
    ans++;
  }
  return ans - 1;
}
int solve(int x) {
  if (x == 0) {
    return 0;
  }
  int ans = 0;
  for (int i = bit(x); i >= 0; i--) {
    if (((x >> i) & 1) == 0) ans += (1 << i);
  }
  return ans;
}
int main() {
  int n;
  cin >> n;
  long long ans = 0;
  for (int i = n; i > 0; i--) {
    if (f[i]) continue;
    int j = solve(i);
    f[i] = f[j] = 1;
    an[i] = j;
    an[j] = i;
    ans += (long long)2 * (i | j);
  }
  cout << ans << endl;
  for (int i = 0; i <= n; i++) printf("%d ", an[i]);
  return 0;
}
