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