Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive.

For permutation p = p_0, p_1, ..., p_{n}, Polo has defined its beauty — number $(0 \oplus p_{0}) +(1 \oplus p_{1}) + \cdots +(n \oplus p_{n})$.

Expression $x \oplus y$ means applying the operation of bitwise excluding "OR" to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is represented as "^" and in Pascal — as "xor".

Help him find among all permutations of integers from 0 to n the permutation with the maximum beauty.


-----Input-----

The single line contains a positive integer n (1 ≤ n ≤ 10^6).


-----Output-----

In the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from 0 to n with the beauty equal to m.

If there are several suitable permutations, you are allowed to print any of them.


-----Examples-----
Input
4

Output
20
0 2 1 4 3

Difficulty: MEDIUM_HARD

Solutions:
s = n = int(input()) + 1
(t, r) = ([], list(range(n))[::-1])
k = 2 ** 20
while s:
	while k >= 2 * s:
		k //= 2
	t = r[n - s:n + s - k] + t
	s = k - s
print(n * n - n)
print(' '.join(map(str, t)))


n = int(input())
p = [i for i in range(n + 1)]
k = 1
while 2 * k <= n:
	k *= 2
m = n + 1
while m > 0:
	while k >= m:
		k //= 2
	for i in range(m - k):
		if k - i - 1 >= 0:
			(p[k + i], p[k - i - 1]) = (p[k - i - 1], p[k + i])
	m = k - i - 1
print(n * (n + 1))
print(' '.join(map(str, p)))


s = n = int(input()) + 1
(t, r) = ([], list(range(n))[::-1])
k = 2 ** 20
while s:
	while k >= 2 * s:
		k //= 2
	t = r[n - s:n + s - k] + t
	s = k - s
print(n * (n - 1))
print(*t)


import math

def ones(n):
	number_of_bits = int(math.floor(math.log(n) / math.log(2))) + 1
	return [(1 << number_of_bits) - 1 ^ n, (1 << number_of_bits) - 1]
n = int(input())
has = [0] * (n + 1)
ans = [0] * (n + 1)
fin = 0
for i in range(n, 0, -1):
	if has[i] == 0:
		(com, fi) = ones(i)
		has[com] = 1
		fin += 2 * fi
		ans[com] = i
		ans[i] = com
print(fin)
print(*ans)


n = int(input()) + 1
print(n * n - n)
t = []
k = 1 << 20
while n:
	while k >> 1 >= n:
		k >>= 1
	t = [k - 1 ^ i for i in range(k - n, n)] + t
	n = k - n
print(' '.join(map(str, t)))


import sys
from math import gcd, sqrt, ceil
from collections import defaultdict, Counter, deque
from bisect import bisect_left, bisect_right
import math
from itertools import permutations
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def get_sum(bit, i):
	s = 0
	i += 1
	while i > 0:
		s += bit[i]
		i -= i & -i
	return s

def update(bit, n, i, v):
	i += 1
	while i <= n:
		bit[i] += v
		i += i & -i

def modInverse(b, m):
	g = math.gcd(b, m)
	if g != 1:
		return -1
	else:
		return pow(b, m - 2, m)

def primeFactors(n):
	sa = set()
	sa.add(n)
	while n % 2 == 0:
		sa.add(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			sa.add(i)
			n = n // i
	return sa

def seive(n):
	pri = [True] * (n + 1)
	p = 2
	while p * p <= n:
		if pri[p] == True:
			for i in range(p * p, n + 1, p):
				pri[i] = False
		p += 1
	return pri

def debug(n):
	l = [i for i in range(n + 1)]
	z = permutations(l)
	maxi = 0
	for i in z:
		fin = 0
		for j in range(n + 1):
			fin += j ^ i[j]
		maxi = max(maxi, fin)
	return maxi
n = int(input())
hash = defaultdict(int)
ans = [0] * (n + 1)
seti = set()
for i in range(n, 0, -1):
	if i not in seti:
		z1 = i ^ int('1' * len(bin(i)[2:]), 2)
		seti.add(z1)
		ans[z1] = i
		ans[i] = z1
fin = 0
for i in range(n + 1):
	fin += i ^ ans[i]
print(fin)
print(*ans)


import math

def solve():
	(n,) = map(int, input().split())
	top = 1
	while top <= n:
		top *= 2
	top -= 1
	ans = [0] * (n + 1)
	i = n
	cur = n
	while i > 0:
		if top - i > cur:
			cur = i
			top = 1
			while top <= cur:
				top *= 2
			top -= 1
		ans[i] = top - i
		ans[top - i] = i
		i -= 1
	score = 0
	if n % 2 == 1:
		ans[top] = 0
		ans[0] = top
	for i in range(n + 1):
		score += ans[i] ^ i
	print(score)
	print(*ans)
t = 1
for _ in range(t):
	solve()