#include <bits/stdc++.h>
using namespace std;
typedef int32_t itn;
const struct in_ {
} in;
template <typename T>
struct Proxy {
  Proxy(const T& t) : t_(t) {}
  const T& t_;
};
template <typename T>
Proxy<T> operator<<(const T& lhs, const in_& rhs) {
  return Proxy<T>(lhs);
}
template <typename T>
bool operator>>(const Proxy<T>& lhs, const vector<T>& rhs) {
  for (auto& a : rhs)
    if (lhs.t_ == a) return true;
  return false;
}
char _;
const int M = 1e6 + 5;
int n, res[M];
long long sm = 0, x = 1;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  while (n >= x) x *= 2;
  x--;
  for (int i = n + 1 - 1; i >= 0; i--) {
    if (res[i] != 0) continue;
    while ((x xor i) > n or res[x xor i] != 0) x /= 2;
    res[x xor i] = i, res[i] = x xor i;
  }
  for (int i = 0; i < n + 1; i++) sm += i xor res[i];
  cout << sm << '\n';
  for (int i = 0; i < n + 1; i++) cout << res[i] << ' ';
  cout << '\n';
  return 0;
}
