#!/bin/sh                                                                                                                             
read n x
m=$n

b=$(( ( n | x ) & -( n | x ) ))
n=$((n/b))
x=$((x/b))

while [ $x -ne 0 ]
do
    x=$(( x / ( x & -x ) ))
    [ $n -gt $x ] && n=$(( ( x ^= ( n^= x ) ) ^ n ))
    x=$(( x - n ))
done
echo $(( (m - n * b) * 3 ))