import scala.collection.mutable.ArrayBuffer object Main { // 1 桁の数字 d を繰り返しかけていったときの // d の 1 の位のサイクルを求める def calcCycle(d: Int): Array[Int] = { assert(0 <= d && d < 10) val used = Array.ofDim[Boolean](10) val xs = new ArrayBuffer[Int]() var x = d while (!used(x)) { used(x) = true xs.append(x) x = (x * d) % 10 } xs.toArray } def main(args: Array[String]) = { val sc = new java.util.Scanner(System.in) val N = sc.nextLine val M = sc.nextLine val d = N.last - '0' val cycle = calcCycle(d) val index = (BigInt(M) % cycle.size).toInt println(cycle(if (index == 0) cycle.size-1 else index-1)) } }