#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 85 - 69;
int ans[maxn], n;
long long int sum;
void solve(int n, int st = 0) {
  int id = 0;
  while ((1 << (id + 1)) <= n) id++;
  if ((1 << id) != n) {
    int rem = n & ((1 << id) - 1);
    sum += 2LL * rem * (1 << id);
    for (int i = 0; i < rem; i++) ans[st + n - rem - i - 1] += 1 << id;
    solve(n - rem, st);
    solve(rem, st + n - rem);
    return;
  }
  sum += 1LL * (n - 1) * n;
  for (int i = 0; i < n; i++) ans[st + n - 1 - i] += i;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n, n++;
  solve(n);
  cout << sum << '\n';
  for (int i = 0; i < n; i++) cout << ans[i] << ' ';
  cout << '\n';
  return 0;
}
