module Memoizable
	def memoize(name)
		original = "__original_#{name}__"
		([Class, Module].include?(self.class) ? self : self.class).class_eval {
			alias_method(original, name)
			private(original)
			cache = {}
			define_method(name){|*args|
				cache[args] ||= send(original, *args)
			}
		}
	end 
end

include Memoizable

def f(a, b)
	return 0 if a.empty?
	d = b[0] - a[0]
	return f(a[1..-1], b[1..-1]) if d == 0
	min = Float::INFINITY
	(0..a.size-1).each do |i|
		c = a[0..i].map {|x| (x + d) % 10} + a[i+1..-1]
		r = f(c, b)
		min = r if r < min
	end
	min + 1
end

memoize :f

loop do
	n = gets.to_i
	break if n == 0
	x, y = gets.chomp.split
	p f(*[x, y].map{|s| s.split("").map(&:to_i)})
end