N = gets.chomp.to_i
P = gets.chomp.split.join
Q = gets.chomp.split.join
 
number=1
array = []
while  N>=number do
  array.push(number)
  number+=1
end
 
aaa = []
 
array.permutation { |p| aaa.push(p.join.to_i) }
a = aaa.index(P) + 1
b = aaa.index(Q) + 1
puts (b-a).abs