#include <bits/stdc++.h>
using namespace std;
const double PI = 3.14159265358979;
const double PI2 = 6.28318530717958;
const double PId2 = 1.570796326794895;
template <typename T>
T sqr(T x) {
  return x * x;
}
template <typename T>
T gcd(T a, T b) {
  return (b ? gcd(b, a % b) : a);
}
long long pw(long long a, long long p, int mod) {
  long long res = 1;
  while (p) {
    if (p & 1) {
      --p;
      res = (res * a) % mod;
    } else {
      p >>= 1;
      a = (a * a) % mod;
    }
  }
  return res;
}
inline vector<int> ReadVI(int count) {
  vector<int> arrayname(count);
  for (int i = 0; i < count; i++) cin >> arrayname[i];
  return arrayname;
}
inline vector<long long> ReadVlong(int count) {
  vector<long long> arrayname(count);
  for (int i = 0; i < count; i++) cin >> arrayname[i];
  return arrayname;
}
const int MOD = 1000000007;
const int MAXVALUE = 200010;
long long f1(long long k) {
  long long ans = 1;
  while (ans - 1 < k) ans *= 2;
  return ans - 1;
}
vector<int> a;
long long Solve(long long n) {
  if (n <= 0) return 0;
  int q = f1(n);
  int l = n ^ q;
  long long sum = Solve(l - 1);
  for (int i = l; i <= n; i++) {
    a[i] = i ^ q;
    sum += q;
  }
  return sum;
}
int main() {
  long long n;
  cin >> n;
  a.assign(n + 1, 0);
  cout << Solve(n) << endl;
  for (int i = 0; i < n + 1; i++) printf("%d ", a[i]);
  return 0;
}
