read n
read -d ""
 
LC_ALL=C
X=0
AB=`echo "${REPLY}" | tac`
B_CHK=`echo "${REPLY}" | awk '{print $2}' | grep -wc 1`
 
for i in `seq 2 $((n + 1))`
do
	A=`echo "${AB}" | awk -v NUM=$i 'NR==NUM {print $1}'`
	A=$((A + X))
	B=`echo "${AB}" | awk -v NUM=$i 'NR==NUM {print $2}'`
 
	if [ $A -gt $B ]
	then
		X=$(( X + $((B - $((A % B)) )) ))
	elif [ $A -lt $B ]
	then
		X=$(( X + $((B - A)) ))
	fi
done
echo $((X - B_CHK))
