i = gets.to_i j = gets.chomp $vv = nil puts Enumerator.produce{$vv = ($vv ? $vv * $vv : i) % 10}.take(10)[j[-1].to_i]