Hey can someone please help me to find an efficient solution for this problem in python/c++ which has less complexity even for long long int values of k and n?

My solution has complexity N^2 and is not accepted, more efficient code is needed

////

Bob is on an adventure to save princess from villain. There are N towns numbered 1 to N where prince is hidden, Bob starts to search from town 1.

He asks the locals where they last saw the princess being taken, and all locals of a town give the same answer everytime he asked.

The locals of town i will always give answer Ai. Bob decides to travel k times before giving up.

Find out in which city Bob gave up.

Input format:

N k

A1 A2 A3…. AN

Out format:

City number at which Bob gave up

Hint: Eventually Bob is stuck in a loop

///////

Source: Windows Questions C++