import java.awt.*;
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        FastReader sc = new FastReader();
        int n=sc.nextInt();
        BigInteger x=BigInteger.ONE;
        long a[]=new long[n];
        for (int i=0;i<n;i++){
            a[i]=sc.nextLong();
            if (a[i]==0){
                System.out.println(0);
                return;
            }
        }
        for (int i=0;i<n;i++){
            x=x.multiply(new BigInteger(a[i]+""));
            if (x.compareTo(new BigInteger("1000000000000000000"))>=1){
                System.out.println(-1);
                return;
            }
        }
        System.out.println(x);
    }
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.util.*;

public class Main {

    public static void main(String[] args) {
    Scanner scanner= new Scanner(System.in);
    int N = scanner.nextInt();
    double[] nums = new double[N];
    for(int i= 0 ; i<N;i++){
    nums[i] = scanner.nextDouble();
    }
    double product = 1;
    for(int i = 0 ; i<N;i++){
    product*= nums[i];
    }
    if(product>Math.pow(10,18)){
        System.out.println("-1");
    } else{
        System.out.println(product);
    }
    }

}

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 200 points

Problem Statement

Given N integers A_1, ..., A_N, compute A_1 \times ... \times A_N.

However, if the result exceeds 10^{18}, print -1 instead.

Constraints

2 \leq N \leq 10^5

0 \leq A_i \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the value A_1 \times ... \times A_N as an integer, or -1 if the value exceeds 10^{18}.

Sample Input 1

2
1000000000 1000000000

Sample Output 1

1000000000000000000

We have 1000000000 \times 1000000000 = 1000000000000000000.

Sample Input 2

3
101 9901 999999000001

Sample Output 2

-1

We have 101 \times 9901 \times 999999000001 = 1000000000000000001, which exceeds 10^{18}, so we should print -1 instead.

Sample Input 3

31
4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5 0

Sample Output 3

0

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] array = new long[n];
		for(int i=0;i<n;i++) {
			array[i] = sc.nextLong();
			if(array[i]==0) {
				System.out.println(0);
				return;
			}
		}
		sc.close();

		long max = 1000000000000000000L;

		long ans = 1L;
		for(long i :array) {
			long tmp = max/ans;
			if(i>tmp) {
				System.out.println(-1);
				return;
			}
			ans = ans*i;

		}
		System.out.println(ans);
	}
}


import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class Main{

	public static void main(String[] args) {
		new Main().run();
	}

	void run() {
		FastScanner sc=new FastScanner();
		int N=sc.nextInt();
		BigInteger prd=BigInteger.valueOf(1);
		for (int i=0;i<N;++i) {
			long A=sc.nextLong();
			if (A==0) prd=BigInteger.ZERO;
			if (prd.equals(BigInteger.valueOf(-1))) continue;
			prd=prd.multiply(BigInteger.valueOf(A));
			if (prd.compareTo(BigInteger.valueOf((long)1e18))==1) {
				prd=BigInteger.valueOf(-1);
			}
		}
		System.out.println(prd);
	}

	void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }

    public double nextDouble() { return Double.parseDouble(next());}
}


import java.util.*;
import java.io.*;

public class Main {

    private static final int M = 1_000_000_007;

    public Main(FastScanner in, PrintWriter out, int test) {
        long A = in.nextLong();
        String[] B = in.next().split("\\.");
        int b = Integer.parseInt(B[0]) * 100 + Integer.parseInt(B[1]);
        out.println(A * b / 100);
    }

    private long pow(int x, int p) {
        long res = 1, a = x;
        while (p > 1) {
            if (p % 2 == 1)
                res = res * a % M;

            a = a * a % M;
            p /= 2;
        }
        res  = res * a % M;
        return res;
    }

    public static void main(String[] args) {
        PrintWriter out = new PrintWriter(System.out);
        // Scanner in = new Scanner(
                // new BufferedReader(new InputStreamReader(System.in)));
        FastScanner in = new FastScanner(System.in);

        // int T = in.nextInt();
        for (int t = 1; t <= 1; t++) {
            Main sol = new Main(in, out, t);
        }

        out.close();
    }
}

class FastScanner{
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public FastScanner(InputStream stream)
    {
        this.stream = stream;
    }

    int read()
    {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars){
            curChar = 0;
            try{
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    boolean isSpaceChar(int c)
    {
        return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
    }

    boolean isEndline(int c)
    {
        return c=='\n'||c=='\r'||c==-1;
    }

    int nextInt()
    {
        return Integer.parseInt(next());
    }

    long nextLong()
    {
        return Long.parseLong(next());
    }

    double nextDouble()
    {
        return Double.parseDouble(next());
    }

    String next(){
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do{
            res.appendCodePoint(c);
            c = read();
        }while(!isSpaceChar(c));
        return res.toString();
    }

    String nextLine(){
        int c = read();
        while (isEndline(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do{
            res.appendCodePoint(c);
            c = read();
        }while(!isEndline(c));
        return res.toString();
    }
}

Score : 300 points

Problem Statement

Compute A \times B, truncate its fractional part, and print the result as an integer.

Constraints

0 \leq A \leq 10^{15}

0 \leq B < 10

A is an integer.

B is a number with two digits after the decimal point.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the answer as an integer.

Sample Input 1

198 1.10

Sample Output 1

217

We have 198 \times 1.10 = 217.8. After truncating the fractional part, we have the answer: 217.

Sample Input 2

1 0.01

Sample Output 2

0

Sample Input 3

1000000000000000 9.99

Sample Output 3

9990000000000000


import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long a = sc.nextLong();
    float b = sc.nextFloat();
    long ans = a * (long)(b*100+0.5);
    System.out.println(ans/100);

  }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
  private static class FastScanner {
    private BufferedReader reader = null;
    private StringTokenizer tokenizer = null;

    public FastScanner(InputStream in) {
      reader = new BufferedReader(new InputStreamReader(in));
      tokenizer = null;
    }

    public String next() {
      if (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          tokenizer = new StringTokenizer(reader.readLine());
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return tokenizer.nextToken();
    }

    public String nextLine() {
      if (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          return reader.readLine();
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }

      return tokenizer.nextToken("\n");
    }

    public long nextLong() {
      return Long.parseLong(next());
    }

    public int nextInt() {
      return Integer.parseInt(next());
    }

    public double nextDouble() {
      return Double.parseDouble(next());
    }

    public int[] nextIntArray(int n) {
      int[] a = new int[n];
      for (int i = 0; i < n; i++)
        a[i] = nextInt();
      return a;
    }

    public long[] nextLongArray(int n) {
      long[] a = new long[n];
      for (int i = 0; i < n; i++)
        a[i] = nextLong();
      return a;
    }
  }

  public static void main(String[] args) {
    FastScanner sc = new FastScanner(System.in);
    long a = sc.nextLong();
    String b = sc.next();
    long bn = Long.parseLong(b.replace(".", ""));
    System.out.println((a * bn) / 100L);
  }
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        long a = sc.nextLong();
        // double b = sc.nextDouble();
        String b = sc.next();

        // long ans = (long)(a*b);

        long b0 = b.charAt(0) - '0';
        long b1 = b.charAt(2) - '0';
        long b2 = b.charAt(3) - '0';

        long ans = a * b0 + a * b1 / 10 + a * b2 / 100;
        System.out.println(ans);
    }
}


import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner scn = new Scanner(System.in);
		long a = scn.nextLong();
		double b = scn.nextDouble();
		long p = (long) (b * 100);
		System.out.println(a*p/100);
		/* 144444445884645447 1.5 */
	}
}


import java.util.*;
import java.io.*;

public class Main {
	static PrintWriter out;
	static StringBuilder sb;
	static int mod = (int) 1e9 + 7;
	static int inf = (int) 1e8;
	static int[] col;
	static int n, m, k;
	static ArrayList<Integer>[] ad;
	static int[][][] memo;
	static ArrayList<Integer> h;
	static char[][] grid;
	static int[] a;
	static boolean f;
	static int offest = 30;

	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		out = new PrintWriter(System.out);
		long a = sc.nextLong();
		double v = sc.nextDouble() * 100;
		long ans=(long)(v*a);
		ans=ans/100;
		System.out.println((long)ans);
		out.flush();
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public int[] nextArrInt(int n) throws IOException {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}

		public long[] nextArrLong(int n) throws IOException {
			long[] a = new long[n];
			for (int i = 0; i < n; i++)
				a[i] = nextLong();
			return a;
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}


import java.util.*;
import java.io.*;
public class Main{
		public static void main(String[]args){
			Scanner in = new Scanner(System.in);
			int a = in.nextInt();
			double b = in.nextDouble();
			System.out.println((int)(a*b));
		}
}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);

		long a = s.nextLong();
		long b = (long)(s.nextDouble() * 100);

		long mul = (long)(a * b);

		System.out.println(mul/100);

	}

}


import java.util.*;import java.io.*;import java.math.*;
public class Main
{
    public static void process()throws IOException
    {
        double a=nd();
        double b=nd();
        long ans=(long)(a*b);
        pn(ans);
    }

    static AnotherReader sc;
    static PrintWriter out;
    public static void main(String[]args)throws IOException
    {
        boolean oj =true;
        if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);}
        else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");}
        int t=1;
        // t=ni();
        while(t-->0) {process();}
        out.flush();out.close();
    }

    static void pn(Object o){out.println(o);}
    static void p(Object o){out.print(o);}
    static void pni(Object o){out.println(o);out.flush();}
    static int ni()throws IOException{return sc.nextInt();}
    static long nl()throws IOException{return sc.nextLong();}
    static double nd()throws IOException{return sc.nextDouble();}
    static String nln()throws IOException{return sc.nextLine();}
    static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;}
    static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;}
    static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

    static class AnotherReader{BufferedReader br; StringTokenizer st;
    AnotherReader()throws FileNotFoundException{
    br=new BufferedReader(new InputStreamReader(System.in));}
    AnotherReader(int a)throws FileNotFoundException{
    br = new BufferedReader(new FileReader("input.txt"));}
    String next()throws IOException{
    while (st == null || !st.hasMoreElements()) {try{
    st = new StringTokenizer(br.readLine());}
    catch (IOException  e){ e.printStackTrace(); }}
    return st.nextToken(); } int nextInt() throws IOException{
    return Integer.parseInt(next());}
    long nextLong() throws IOException
    {return Long.parseLong(next());}
    double nextDouble()throws IOException { return Double.parseDouble(next()); }
    String nextLine() throws IOException{ String str = ""; try{
    str = br.readLine();} catch (IOException e){
    e.printStackTrace();} return str;}}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
}


import java.util.*;
public class Main{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		long A = sc.nextLong();
		double B = sc.nextDouble();
		long C = (long)(B * 100);
		long ans = (A * C) / 100;
		System.out.println(ans);
	}
}


import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long a = in.nextLong();
        double b = in.nextDouble();
        long res = (long) (a * b);
        System.out.println((long) Math.floor(res));
    }
}


import java.util.*;
import java.math.RoundingMode;
import java.math.BigDecimal;

public class Main{
	 public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong();
		double b = sc.nextDouble();
		double b_int = Math.floor(b);
		double b_double = b-b_int;
		BigDecimal ans = new BigDecimal(a);
		BigDecimal b_int1 = new BigDecimal(b_int);
		BigDecimal b_double1 = new BigDecimal(b_double);
		ans = ans.multiply(b_int1);
		BigDecimal a_floor = new BigDecimal(a);
		a_floor = a_floor.multiply(b_double1);
		a_floor = a_floor.setScale(0, BigDecimal.ROUND_DOWN);
		ans = ans.add(a_floor);
		System.out.println(ans);
	 }
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		long n =sc.nextLong();
		int f = (int)Math.floor((double)100*sc.nextDouble());
		long temp = (long)n*f;
		System.out.println((long)temp/100);
	}
}


import java.util.Scanner;

public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        long a = sc.nextLong();
        double b = sc.nextDouble();

        double ans = a * b;

        System.out.println((long)Math.floor(ans));
    }
}


import java.util.*;
import java.math.*;
public class Main {
     public static String mul(String v1, String v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.multiply(b2).setScale(scale, BigDecimal.ROUND_DOWN).toString();
    }
    public static void main(String[] args)
    {
       Scanner input =new Scanner(System.in);
String str1=input.next();
        String str2=input.next();//会从控制5261台读入一4102个字符串，1653以空格版为标志权
        Main c=new Main();
        System.out.println(c.mul(str1,str2,0));

    }
}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long A = Long.parseLong(sc.next());
		double B = Double.parseDouble(sc.next());
		sc.close();

		long hunSum = (long) Math.floor(100 * B);
		long ans = (A * hunSum) / 100L;
		System.out.println(ans);

	}

}


import java.math.BigDecimal;
import java.util.*;

public class Main {

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        double A = sc.nextDouble(),B = sc.nextDouble();
        String S = BigDecimal.valueOf((double) A*B).toPlainString();
        if(S.contains(".")){
            System.out.println(S.substring(0,S.indexOf('.')));
        }else{
            System.out.println(S);
        }
    }
}


import java.util.Scanner;

public class Main {
		public static void main (String args[]) throws Exception{
		Scanner scan = new Scanner(System.in);
		long a = scan.nextLong();
		double b = scan.nextDouble();
		// seisuu
		long ans =  a * (long)(b * 100);
		System.out.println(ans / 100);
	}
}


//package bigneer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Main {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub

		BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
//		int t=Integer.parseInt(br.readLine());
//		while(t-->0) {
//			int n=Integer.parseInt(br.readLine());
			String q[]=br.readLine().split(" ");
			long a=Long.parseLong(q[0]);
			double b=Double.parseDouble(q[1]);
//			long ans=;
			System.out.println((long)Math.floor((a*b)));
//			long a[]=new long[n];
//			for(int i=0;i<n;i++)
//				a[i]=Long.parseLong(q[i]);
////			long p=1;
//			int b=0;
//			BigInteger p=new BigInteger("1");
//			BigInteger p1=new BigInteger("1000000000000000000");
//			for(int i=0;i<n;i++) {
//				if(a[i]==0) {
//					b=0;
//					p=new BigInteger("0");
//					break;
//				}
//				if(b!=1) {
//				BigInteger w=new BigInteger(Long.toString(a[i]));
//				p=p.multiply(w);
//				if(p.compareTo(p1)==1)
//					{b=1;
////					break;
//					}
//				}
////				if(p>Math.pow(10, 18)||p<0||p<a[i])
////					{b=1;
////				break;}
//					}
////			System.out.println(b);
//			if(b==1)
//				System.out.println(-1);
//			else
//				System.out.println(p);
//			System.out.println();
//		}
	}

}


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.*;
import java.math.* ;
public class Main {
InputStream is;
	PrintWriter out;
	String INPUT = "";
//class  Declaration

static class pair implements Comparable<pair>{
	int x;
	int y;

	pair (int i,int j)
	{ x=i; y=j;

	}
	public int compareTo(pair p){
		if(this.x!=p.x) { return this.x-p.x;}
		else { return this.y-p.y;}
	}
	public int hashCode() { return (x+" "+y).hashCode();}
	public String toString(){ return x+" "+y;}
	public boolean equals(Object o){
		pair x = (pair) o ;
		return (x.x==this.x&&x.y==this.y);}
}

int inf = (int)1e9 + 5;
long mod = (long)1e9 + 7;
void solve() throws Exception{
	//long a = nl();
	BigDecimal bd1 = new BigDecimal(ns());
	BigDecimal bd2 = new BigDecimal(ns());
	bd1 = bd1.multiply(bd2);
	//	pn(bd1);

	pn(bd1.toString().split("\\.")[0]);

}

long pow(long a,long b){
	long result = 1;
	while(b>0){
	if(b%2==1) result = (result * a) % mod;
		b/=2;
		a=(a*a)%mod;
	}
	return result;
}

void print(Object o){
System.out.println(o);
System.out.flush();
}

long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
void run() throws Exception{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);

		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
}

public static void main(String[] args) throws Exception { new Main().run(); }

//output methods
private void pn(Object o)
{
	out.println(o);
}
private void p(Object o)
{
	out.print(o);
}

//input methods
		private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;

	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}

	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }

	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}

	private char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}

	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}

	private int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}

		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}

		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); }
	void watch(Object ... a) throws Exception{
		int i=1;
		print("watch starts :");
		for(Object o : a ) {
			//print(o);
			boolean notfound = true;
			if(o.getClass().isArray()){

				String type = o.getClass().getName().toString();
				//print("type is "+type);
				switch (type) {
					case "[I":{
						int[] test = (int[])o ;
						print(i+" "+Arrays.toString(test));
						break;
					}
					case "[[I":{
						int[][] obj = (int[][])o;

						print(i+" "+Arrays.deepToString(obj));
						break;
					}
					case "[J" : {

						long[] obj  = (long[])o ;
						print(i+" "+Arrays.toString(obj));
						break;
					}
					case "[[J": {

						long[][] obj = (long[][])o;
						print(i+" "+Arrays.deepToString(obj));
						break;
					}
					case "[D" :{

						double[] obj= (double[])o;
						print(i+" "+Arrays.toString(obj));
						break;
					}
					case "[[D" :{

						double[][] obj = (double[][])o;
						print(i+" "+Arrays.deepToString(obj));
						break;
					}
					case "[Ljava.lang.String": {

						String[] obj = (String[])o ;
						print(i+" "+Arrays.toString(obj));
						break;
					}
					case "[[Ljava.lang.String": {

						String[][] obj = (String[][])o ;
						print(i+" "+Arrays.deepToString(obj));
						break ;
					}
					case "[C" :{
						char[] obj = (char[])o ;
						print(i+" "+Arrays.toString(obj));
						break;
					}
					case "[[C" :{

						char[][] obj = (char[][])o;
						print(i+" "+Arrays.deepToString(obj));
						break;
					}

					default:{
						print(i+" type not identified");
						break;
					}
				}
				notfound = false;

			}
			if(o.getClass() == ArrayList.class){
				print(i+" al: "+o);
				notfound = false;
			}
			if(o.getClass() == HashSet.class){
				print(i+" hs: "+o);
				notfound = false;
			}
			if(o.getClass() == TreeSet.class){
				print(i+" ts: "+o);
				notfound = false;
			}
			if(o.getClass() == TreeMap.class){
				print(i+" tm: "+o);
				notfound = false;
			}
			if(o.getClass() == HashMap.class){
				print(i+" hm: "+o);
				notfound = false;
			}
			if(o.getClass() == LinkedList.class){
				print(i+" ll: "+o);
				notfound = false;
			}
			if(o.getClass() == PriorityQueue.class){
				print(i+" pq : "+o);
				notfound = false;
			}
			if(o.getClass() == pair.class){
				print(i+" pq : "+o);
				notfound = false;
			}

			if(notfound){
				print(i+" unknown: "+o);
			}
			i++;
		}
		print("watch ends ");
	}

}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = (long) (sc.nextDouble()*100);
		long ans = (a*b)/100;
		System.out.println(ans);

	}
}


import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
    public static void main(String... args) {
        Scanner scanner = new Scanner(System.in);

        long A = scanner.nextLong();
        double B = scanner.nextDouble();

        BigDecimal a = new BigDecimal(String.valueOf(A));
        BigDecimal b = new BigDecimal(String.valueOf(B));

        System.out.println(a.multiply(b).longValue());
    }
}


import java.util.*;

public class Main{

    public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      double a = scanner.nextDouble();
        double b = scanner.nextDouble();
      double c = a*b;
           System.out.printf(" %.0f\n",c);

    }

}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		long n =sc.nextLong();
		double f = sc.nextDouble();
		double f2=(double)n;
		System.out.println((long)Math.floor(f*f2));
	}
}


import java.util.*;
import java.io.*;
import java.math.*;

public class Main {

    public static void main(String[] args) throws IOException {

        Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

        double a = sc.nextDouble();
        double b = sc.nextDouble();

        System.out.println((long)(a * b));

        out.flush();
    }
}


import java.util.*;
public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		double a = sc.nextDouble();
		double b = sc.nextDouble();
		System.out.println((long)(a*b));
	}

}


import java.util.*;

public class Main {

    public static void main(String[] args) {
        // write your code here
        Scanner sc = new Scanner(System.in);
        long a = sc.nextLong();
        double b = sc.nextDouble();
        double ans = a * b;
        System.out.println((long) Math.floor(ans));
    }
}


import java.util.*;

public class Main{

  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long a=sc.nextLong(),f;
    double b=sc.nextDouble();
    System.out.println((long)(a*b));

  }
}


/*****Author: Satyajeet Singh, Delhi Technological University************************************/
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.lang.*;
    import java.math.*;
public class Main{
/*********************************************Constants******************************************/
    static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static long mod=(long)1e9+7;
    static long mod1=998244353;
    static ArrayList<Integer> graph[];
    static int pptr=0,pptrmax=0;
    static String st[];
/*****************************************Solution Begins***************************************/
    public static void main(String args[]) throws Exception{
        double x=pd();
        double y=pd();
        double r=(x*y);
        long ans=(long)r;
        out.println(ans);
/****************************************Solution Ends**************************************************/
        out.flush();
        out.close();
    }
/****************************************Template Begins************************************************/
    static void nl() throws Exception{
        pptr=0;
        st=br.readLine().split(" ");
        pptrmax=st.length;
    }
    static void nls() throws Exception{
        pptr=0;
        st=br.readLine().split("");
        pptrmax=st.length;
    }
    static int pi() throws Exception{
        if(pptr==pptrmax)
            nl();
        return Integer.parseInt(st[pptr++]);
    }
    static long pl() throws Exception{
        if(pptr==pptrmax)
            nl();
        return Long.parseLong(st[pptr++]);
    }
    static double pd() throws Exception{
        if(pptr==pptrmax)
            nl();
        return Double.parseDouble(st[pptr++]);
    }
    static String ps() throws Exception{
        if(pptr==pptrmax)
            nl();
        return st[pptr++];
    }
/***************************************Precision Printing**********************************************/
    static void printPrecision(double d){
        DecimalFormat ft = new DecimalFormat("0.0000000000");
        out.println(ft.format(d));
    }
/**************************************Bit Manipulation**************************************************/
    static int countBit(long mask){
        int ans=0;
        while(mask!=0){
            mask&=(mask-1);
            ans++;
        }
        return ans;
    }
/******************************************Graph*********************************************************/
    static void Makegraph(int n){
        graph=new ArrayList[n];
        for(int i=0;i<n;i++)
            graph[i]=new ArrayList<>();
    }
    static void addEdge(int a,int b){
        graph[a].add(b);
    }
    // static void addEdge(int a,int b,int c){
    //     graph[a].add(new Pair(b,c));
    // }
/*********************************************PAIR********************************************************/
    static class Pair{
        int u;
        int v;
        public Pair(int u, int v) {
            this.u = u;
            this.v = v;
        }
        public int hashCode() {
            int hu = (int) (u ^ (u >>> 32));
            int hv = (int) (v ^ (v >>> 32));
            return 31 * hu + hv;
        }
        public boolean equals(Object o) {
            Pair other = (Pair) o;
            return u == other.u && v == other.v;
        }
        public String toString() {
            return "[u=" + u + ", v=" + v + "]";
        }
    }
/******************************************Long Pair*******************************************************/
    static class Pairl{
        long u;
        long v;
        public Pairl(long u, long v) {
            this.u = u;
            this.v = v;
        }
        public int hashCode() {
            int hu = (int) (u ^ (u >>> 32));
            int hv = (int) (v ^ (v >>> 32));
            return 31 * hu + hv;
        }
        public boolean equals(Object o) {
            Pairl other = (Pairl) o;
            return u == other.u && v == other.v;
        }
        public String toString() {
            return "[u=" + u + ", v=" + v + "]";
        }
    }
/*****************************************DEBUG***********************************************************/
    public static void debug(Object... o){
        System.out.println(Arrays.deepToString(o));
    }
/************************************MODULAR EXPONENTIATION***********************************************/
    static long modulo(long a,long b,long c){
        long x=1,y=a%c;
        while(b > 0){
            if(b%2 == 1)
                x=(x*y)%c;
            y = (y*y)%c;
            b = b>>1;
        }
        return  x%c;
    }
/********************************************GCD**********************************************************/
    static long gcd(long x, long y){
        if(x==0)
            return y;
        if(y==0)
            return x;
        long r=0, a, b;
        a = (x > y) ? x : y;
        b = (x < y) ? x : y;
        r = b;
        while(a % b != 0){
            r = a % b;
            a = b;
            b = r;
        }
        return r;
    }
}


import java.util.*;

public class Main{
    void solve(){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        long k = scan.nextLong();
        int[] a = new int[n];
        for(int i = 0; i < n; i++) a[i] = scan.nextInt() - 1;
        int c = 1;
        int index = 0;
        List<Integer> loop = new ArrayList<>();
        TreeSet<Integer> seen = new TreeSet<>();
        loop.add(0);
        seen.add(0);
        while(!seen.contains(a[index])){
            loop.add(a[index]);
            seen.add(a[index]);
            index = a[index];
            c++;
        }
        int start = loop.indexOf(a[index]) + 1;
        c -= loop.size() - start;
        System.out.println(loop.get((int)(k % c)) + 1);
    }

    public static void main(String[] args){
        new Main().solve();
    }
}

Score: 400 points

Problem Statement

The Kingdom of Takahashi has N towns, numbered 1 through N.

There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.

Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Help the king by writing a program that answers this question.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

public class Main {
	public static void main(String[] args) {
		new Main().solve();
		pw.close();
	}

	/**
	 * ASCII
	 * 0 48
	 * A 65
	 * a 97
	 */

	static PrintWriter pw = new PrintWriter(System.out);

	long MOD = 1_000_000_007;

	void solve() {
		FastScanner sc = new FastScanner();
		int N = sc.nextInt();
		long K = sc.nextLong();
		int[] A = new int[N+1];
		for (int i = 1; i <= N; i++) A[i] = sc.nextInt();
		int[] b = new int[N+1];
		int s = 1;
		int l = 1;
		int pre = 1;
		int[] c = new int[N+1];
		for (int i = 1; i <= N; i++) {
			c[i] = A[pre];
			if (b[A[pre]] != 0 || A[pre] == 1) {
				s = i;
				if (A[pre] == 1) s = 1;
				l = i - b[A[pre]];
				break;
			} else {
				b[A[pre]] = i;
				pre = A[pre];
			}
		}
		if (K <= s) {
			out(c[(int)K]);
		} else {
			out(c[s+(int)((K-s)%l)]);
		}
	}

	void func(List<Integer>[] l, int now, int[] ans) {
		ArrayDeque<Integer> que = new ArrayDeque<Integer>();
		que.addLast(now);
		while (!que.isEmpty()) {
			int x = que.removeFirst();
			for (int t : l[x]) {
				if (ans[t] == 0) {
					ans[t] = x;
					que.addLast(t);
				}
			}
		}
	}

	double getD(double x1, double x2, double y1, double y2) {
		return Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2, 2));
	}

	int[][] d4 = new int[][] {{1,0},{0,1},{-1,0},{0,-1}};
	int[][] d8 = new int[][] {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};

	class Data {
		int a;
		int b;
		int c;
		Data(){}
		Data(int a) {
			this.a = a;
		}
		Data(int a, int b, int c) {
			this.a = a;
			this.b = b;
			this.c = c;
		}
		Data(int a, int b) {
			this.a = a;
			this.b = b;
		}
	}

	class Permutation {
		int[] array;

		Permutation(int N) {
			array = new int[N];
			for (int i = 0; i < N; i++) {
				array[i] = i+1;
			}
		}

		boolean nextPermutation() {
		    int i = array.length - 1;
		    while (i > 0 && array[i - 1] >= array[i])
		        i--;
		    if (i <= 0)
		        return false;

		    int j = array.length - 1;
		    while (array[j] <= array[i - 1])
		        j--;
		    int temp = array[i - 1];
		    array[i - 1] = array[j];
		    array[j] = temp;

		    j = array.length - 1;
		    while (i < j) {
		        temp = array[i];
		        array[i] = array[j];
		        array[j] = temp;
		        i++;
		        j--;
		    }
		    return true;
		}
	}

	class UnionFind {
		int[] parents;
		int[] counts;

		public UnionFind(int size) {
			parents = new int[size];
			counts = new int[size];
			for (int i = 0; i < size; i++) {
				parents[i] = i;
				counts[i] = 1;
			}
		}

		public int find(int a) {
			if (parents[a] == a) return a;
			parents[a] = find(parents[a]);
			return parents[a];
		}

		public void unite(int a, int b) {
			a = find(a);
			b = find(b);
			if (a == b) return;
			if (counts[a] < counts[b]) {
				parents[a] = b;
				counts[b] += counts[a];
			} else {
				parents[b] = a;
				counts[a] += counts[b];
			}
		}

		public boolean differ(int a, int b) {
			a = find(a);
			b = find(b);
			return a != b;
		}

		public int count(int a) {
			return counts[find(a)];
		}
	}

	class Combination {
		final int mod;
		final int max;

		final long[] fact;
		final long[] inv;
		final long[] invfact;

		public Combination(int n) {
			this(n, 1_000_000_007);
		}

		public Combination(int n, int mod) {
			this.mod = mod;
			max = n + 1;
			fact = new long[max];
			invfact = new long[max];
			inv = new long[max];

			inv[1] = 1;
			for (int i = 2; i < inv.length; i++) {
				inv[i] = inv[mod % i] * (mod - mod / i) % mod;
			}

			fact[0] = 1;
			invfact[0] = 1;
			for (int i = 1; i < inv.length; i++) {
				fact[i] = i * fact[i - 1] % mod;
				invfact[i] = inv[i] * invfact[i - 1] % mod;
			}
		}

		public long get(int n, int r) {
			return fact[n] * invfact[n - r] % mod * invfact[r] % mod;
		}

		public long gcd(long a, long b) {
			if (b == 0) return a;
			else {
				b %= MOD;
				if (b < 0) b+=MOD;
				return gcd(b, (b-a*inv[(int)b]%MOD*b%MOD)%MOD);
			}
		}
	}

	public long gcd(long a, long b) {
		if (b == 0) return a;
		else return gcd(b, a%b);
	}

	public long getInv(long a) {
		long b = MOD, u = 1, v = 0;
		while (b > 0) {
			long t = a / b;
			a -= t * b;
			long tmp = a;
			a = b;
			b = tmp;
			u -= t * v;
			tmp = u;
			u = v;
			v = tmp;
		}
		u %= MOD;
		if (u < 0) u += MOD;
		return u;
	}

	public long modPow(long n) {
		if (n == 0) return 1;
		long x = modPow(n / 2);
		x *= x;
		x %= MOD;
		if (n % 2 == 1) x *= 2;
		x %= MOD;
		return x;
	}

	public long choose(long n, long m) {
		long deno = 1;
		long nume = 1;
		m = (n - m < m ? n - m : m);
		for (long i = 1; i <= m; i++) {
			deno = deno * (n - i + 1) % MOD;
			nume = nume * i % MOD;
		}
		return deno * getInv(nume) % MOD;
	}

	public void reverse(int[] a) {
		int last = a.length-1;
		int n = a.length / 2;
		for (int i = 0; i < n; i++) {
			int t = a[i];
			a[i] = a[last-i];
			a[last-i] = t;
		}
	}

	public void reverse(long[] a) {
		int last = a.length-1;
		int n = a.length / 2;
		for (int i = 0; i < n; i++) {
			long t = a[i];
			a[i] = a[last-i];
			a[last-i] = t;
		}
	}

	void fout(Object... args) {
		StringBuilder sb = new StringBuilder();
		for (Object arg : args) {
			sb.append(arg.toString());
			sb.append(" ");
		}
		out(sb.toString());
	}

	void out() {
		pw.println();
	}

	void out(String a) {
		pw.println(a);
	}
	void out(boolean a) {
		pw.println(a);
	}

	void out(int a) {
		pw.println(a);
	}

	void out(long a) {
		pw.println(a);
	}

	void out(double a) {
		pw.println(a);
	}

	void out(char a) {
		pw.println(a);
	}

	void rout(String a) {
		out(a);
		pw.close();
		System.exit(0);
	}
	void rout(int a) {
		out(a);
		pw.close();
		System.exit(0);
	}
	void rout(long a) {
		out(a);
		pw.close();
		System.exit(0);
	}
	void rout(double a) {
		out(a);
		pw.close();
		System.exit(0);
	}
	void rout(char a) {
		out(a);
		pw.close();
		System.exit(0);
	}
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        } else {
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }

    private int readByte() {
        if (hasNextByte())
            return buffer[ptr++];
        else
            return -1;
    }

    private boolean isPrintableChar(int c) {
        return 33 <= c && c <= 126;
    }

    public boolean hasNext() {
        while (hasNextByte() && !isPrintableChar(buffer[ptr]))
            ptr++;
        return hasNextByte();
    }

    public String next() {
        if (!hasNext())
            throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while (isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    public long nextLong() {
        if (!hasNext())
            throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while (true) {
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            } else if (b == -1 || !isPrintableChar(b)) {
                return minus ? -n : n;
            } else {
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }

    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
            throw new NumberFormatException();
        return (int) nl;
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    static int ans = Integer.MAX_VALUE;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int n = Integer.parseInt(st.nextToken());
        long k = Long.parseLong(st.nextToken());

        int[] arr = new int[n];
        st = new StringTokenizer(br.readLine());

        for (int i=0; i<n; i++)
            arr[i] = Integer.parseInt(st.nextToken());

        List<Integer> lst = new ArrayList<>();
        HashSet<Integer> vis = new HashSet<>();
        lst.add(1);
        vis.add(1);
        int i=0;
        int cycleStart = -1;
        for (; i<k; i++){
            int next = arr[lst.get(lst.size()-1)-1];
            if (vis.contains(next)){
                cycleStart = next;
                break;
            }
            vis.add(next);
            lst.add(next);
        }
        if (i==k){
            System.out.println(lst.get(lst.size()-1));
        }
        else{
            int j=0;
            for (; j<lst.size(); j++){
                if (lst.get(j)==cycleStart)
                    break;
                k--;
            }
            List<Integer> newLst = lst.subList(j,lst.size());
            System.out.println(newLst.get((int)(k%newLst.size())));
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.HashMap;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Teleporter solver = new Teleporter();
        solver.solve(1, in, out);
        out.close();
    }

    static class Teleporter {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            long k = in.nextLong();

            HashMap<Integer, Integer> map = new HashMap<>();
            int[] circle = new int[n + 1];

            for (int i = 1; i <= n; i++) {
                int t = in.nextInt();
                map.put(i, t);
            }

            Integer next = 1;
            circle[1]++;

            int close_start;
            while (true) {
                next = map.get(next);
                if (circle[next] == 3) {
                    close_start = next;
                    break;
                }
                circle[next]++;
            }

            int closed_step = 1;
            int tmp_close = map.get(close_start);
            while (tmp_close != close_start) {
                tmp_close = map.get(tmp_close);
                closed_step++;
            }

            long up_steps = 1;
            next = map.get(1);

            while (true) {
                if (next == close_start || up_steps >= k) {
                    break;
                }
                next = map.get(next);
                up_steps++;
            }

            long loop_num = (k - up_steps) % closed_step;
            for (int i = 0; i < loop_num; i++) {
                next = map.get(next);
            }
            out.append(String.valueOf(next));
        }

    }
}


import java.math.BigInteger;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int N = s.nextInt();
        BigInteger K = s.nextBigInteger();
        int[] A = new int[N];
        int count = 0;
        for (int i = 0; i < N; i++) {
            A[i] = s.nextInt();
        }
        K = K.remainder(BigInteger.valueOf(N)).add(BigInteger.valueOf(N));
        int now;
        for (now = 1; BigInteger.valueOf(count).compareTo(K) < 0; count++) {
            now = A[now - 1];
        }

        System.out.print(now);
    }
}


import java.math.BigInteger;
import java.util.*;
import java.io.*;
import java.text.*;

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 32768);
    static PrintWriter out = new PrintWriter(System.out);
    static StringTokenizer t;

    static String sn() {
        while (t == null || !t.hasMoreTokens()) {
            try {
                t = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                throw new RuntimeException();
            }
        }
        return t.nextToken();
    }

    static int ni() {
        return Integer.parseInt(sn());
    }

    static long nlo() {
        return Long.parseLong(sn());
    }

    public static void main(String args[]) {
        int t = 1;
        while (t-- > 0) {
            solve();
        }
        out.close();
    }

    static long mod = (long) 1e9 + 7;

    static long pro(long a, long b) {
        return (a % mod * b % mod) % mod;
    }

    static long add(long a, long b) {
        return (a + b) % mod;
    }

    static long sub(long a, long b) {
        return ((a % mod - b % mod) + mod) % mod;
    }

    static int maxn = 1000005;

    static int pow(int n) {
        return (int) (Math.log10(n) / Math.log10(2));
    }

    static long modpow(long x, long y) {
        long res = 1l;
//        x %= mod;
        while (y > 0) {
            if (y % 2 != 0)
                res *= x;
            x = x * x;
            y /= 2;
        }
        return res;
    }

    static class pair {
        String a;
        int b;

        pair(String x, int y) {
            a = x;
            b = y;
        }
    }

    static long modInverse(long n) {
        BigInteger n1 = new BigInteger(Long.toString(n));
        String s = (n1.modInverse(new BigInteger(Long.toString(mod)))).toString();
        return Long.parseLong(s);
    }

    static boolean v[] = new boolean[1000005];

    static void seive() {
        v[1] = true;
        for (int i = 2; i < 1000005; i++) {
            if (!v[i]) {
                for (int j = 2 * i; j < 1000005; j += i)
                    v[j] = true;
            }
        }
    }

    static boolean isprime(long n) {
        if (n == 1)
            return false;
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }

    static int gcd(int a, int b) {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }

    static DecimalFormat df = new DecimalFormat("0.000000000000000000000");

    static void solve() {
        int n = ni();
        long k = nlo();
        int a[] = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = ni() - 1;
        int chain[] = new int[n];
        int p = 0;
        int cur = 0;
        boolean v[] = new boolean[n];
        int id[] = new int[n];
        while (!v[cur]) {
            v[cur] = true;
            chain[p] = cur;
            id[cur] = p;
            cur = a[cur];
            p += 1;
        }
        int d = p - id[cur];
        int t = (int) (k % d);
        out.println(chain[t] + 1);
    }
}


import java.util.*;
class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long k = scanner.nextLong();
        int[] a = new int[n];
        for(int i = 0; i < n; i++){
            a[i] = scanner.nextInt()-1;
        }
        List<Integer> list = new ArrayList<>();
        int loophead = 0;
        boolean flag = false;
        list.add(0);
        int cur = a[0];
        for(int i = 1; i < n+1; i++){
            if(list.contains(cur)){
                loophead = cur;
                flag = true;
                break;
            }else{
                list.add(cur);
                cur = a[cur];
            }
        }
        if(!flag){System.out.println(cur);return;}
        int loopheadindex = list.indexOf(loophead);
        int looplength = list.size()-loopheadindex;
        /*
        System.out.println("list size " + list.size());
        System.out.println("head loop index " + loopheadindex);
        System.out.println("head loop length " + looplength);
        */
        long ansindex = (k - Long.valueOf(loopheadindex))%Long.valueOf(looplength);
        System.out.println(list.get((int)ansindex + loopheadindex) + 1);
    }
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int townNum = scan.nextInt();
        long loveInt = scan.nextLong();
        Town[] towns = new Town[townNum + 1];
        for (int i = 1; i < townNum + 1; i++) {
            towns[i] = new Town(i);
        }

        for (int i = 1; i < townNum + 1; i++) {
            towns[i].setNextTown(towns[scan.nextInt()]);
        }

        towns[1].reach(1);
        Town endTown = towns[1].endlessGoNext();

        int startLoop=endTown.goNext().getFirstReach();
        int loop=endTown.getFirstReach()-startLoop+1;

        long minStep=loveInt;
        if (loveInt>startLoop){
            minStep=(loveInt-startLoop) % loop+startLoop;
        }

        Town end=towns[1];
        for (int i = 0; i <minStep ; i++) {
            end=end.goNext();
        }

        System.out.println(end.getId());
    }
}

class Town {
    private final int id;
    private int firstReach = 0;
    private Town nextTown;

    public Town(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setNextTown(Town nextTown) {
        this.nextTown = nextTown;
    }

    public boolean reach(int firstReach) {
        if (this.firstReach == 0) {
            this.firstReach = firstReach;
            return true;
        } else {
            return false;
        }
    }

    public int getFirstReach() {
        return firstReach;
    }

    Town goNext() {
        return nextTown;
    }

    Town endlessGoNext() {
        if (nextTown.reach(firstReach + 1)) {
            return nextTown.endlessGoNext();
        } else {
            return this;
        }
    }
}


import java.lang.reflect.Array;
import java.util.*;
import java.io.*;

/*
4 5
3 2 4 1

4

4 4
3 2 4 1

4
 */
public class Main {
    public static void main(String[] args) throws IOException {
        int n = nextInt();
        long k = nextLong() - 1;

        int[] a = readArray(n);
        List<Integer> sorted = new ArrayList<>();
        sorted.add(a[0]);

        for (int i = 1; i < 2 * n + 1000; i++) {
            int next = a[sorted.get(sorted.size() - 1) - 1];
            sorted.add(next);
        }

        if (k < n + 1) {
            System.out.println(sorted.get((int) k));
            return;
        }

        int loop_end = n - 1;
        int loop_start = n - 2;

        // 周期を求める
        while (sorted.toArray()[loop_start] != sorted.toArray()[loop_end]) {
            loop_start -= 1;
        }

        int period = loop_end - loop_start;
        k %= period;

//        System.out.println(loop_start + " " + loop_end + " " + period + " " + k);
        while (k < n) {
            k += period;
        }

        System.out.println(sorted.get((int) k));

    }

    // constant
    static StringBuilder sb;
    static StringTokenizer st;
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    static final int inf = Integer.MAX_VALUE / 2;
    static final long linf = Long.MAX_VALUE / 3;
    static final double dinf = Double.MAX_VALUE / 3;
    static final double eps = 1e-10;
    static final double pi = Math.PI;

    // pow
    static double pow(int x, int y) {
        return Math.pow(x, y);
    }

    // max min
    static int max(int x, int y) {
        return Math.max(x, y);
    }

    static int min(int x, int y) {
        return Math.min(x, y);
    }

    static int max(int x, int y, int z) {
        x = Math.max(x, y);
        x = Math.max(x, z);
        return x;
    }

    static int min(int x, int y, int z) {
        x = Math.min(x, y);
        x = Math.min(x, z);
        return x;
    }

    static long max(long x, long y) {
        return Math.max(x, y);
    }

    static long min(long x, long y) {
        return Math.min(x, y);
    }

    static long max(long x, long y, long z) {
        x = Math.max(x, y);
        x = Math.max(x, z);
        return x;
    }

    static long min(long x, long y, long z) {
        x = Math.min(x, y);
        x = Math.min(x, z);
        return x;
    }

    static double max(double x, double y) {
        return Math.max(x, y);
    }

    static double min(double x, double y) {
        return Math.min(x, y);
    }

    static double max(double x, double y, double z) {
        x = Math.max(x, y);
        x = Math.max(x, z);
        return x;
    }

    static double min(double x, double y, double z) {
        x = Math.min(x, y);
        x = Math.min(x, z);
        return x;
    }

    // sort
    static void sort(int[] ar) {
        Arrays.sort(ar);
    }

    static void sort(long[] ar) {
        Arrays.sort(ar);
    }

    static void sort(double[] ar) {
        Arrays.sort(ar);
    }

    static void sort(char[] ar) {
        Arrays.sort(ar);
    }

    static void rsort(int[] ar) {
        Arrays.sort(ar);
        int len = ar.length;
        for (int i = 0; i < len / 2; i++) {
            int tmp = ar[i];
            ar[i] = ar[len - 1 - i];
            ar[len - 1 - i] = tmp;
        }
    }

    static void rsort(long[] ar) {
        Arrays.sort(ar);
        int len = ar.length;
        for (int i = 0; i < len / 2; i++) {
            long tmp = ar[i];
            ar[i] = ar[len - 1 - i];
            ar[len - 1 - i] = tmp;
        }
    }

    static void rsort(double[] ar) {
        Arrays.sort(ar);
        int len = ar.length;
        for (int i = 0; i < len / 2; i++) {
            double tmp = ar[i];
            ar[i] = ar[len - 1 - i];
            ar[len - 1 - i] = tmp;
        }
    }

    static void rsort(char[] ar) {
        Arrays.sort(ar);
        int len = ar.length;
        for (int i = 0; i < len / 2; i++) {
            char tmp = ar[i];
            ar[i] = ar[len - 1 - i];
            ar[len - 1 - i] = tmp;
        }
    }

    // initialize
    static void fill(int arr[], int x) {
        Arrays.fill(arr, x);
    }

    static void fill(long arr[], long x) {
        Arrays.fill(arr, x);
    }

    static void fill(boolean arr[], boolean x) {
        Arrays.fill(arr, x);
    }

    static void fill(double arr[], double x) {
        Arrays.fill(arr, x);
    }

    static void fill(int arr[][], int x) {
        for (int i = 0; i < arr.length; i++) {
            Arrays.fill(arr[i], x);
        }
    }

    static void fill(long arr[][], long x) {
        for (int i = 0; i < arr.length; i++) {
            Arrays.fill(arr[i], x);
        }
    }

    static void fill(double arr[][], double x) {
        for (int i = 0; i < arr.length; i++) {
            Arrays.fill(arr[i], x);
        }
    }

    static void fill(boolean arr[][], boolean x) {
        for (int i = 0; i < arr.length; i++) {
            Arrays.fill(arr[i], x);
        }
    }

    // scanner
    static char nextChar() throws IOException {
        return next().charAt(0);
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static int[] readArray(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextInt();
        }
        return a;
    }

    static long[] readLongArray(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextLong();
        }
        return a;
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine().trim());
        }
        return st.nextToken();
    }

    // cast
    static String padLeftZeros(String inputString, int length) {
        if (inputString.length() >= length) {
            return inputString;
        }
        sb = new StringBuilder();
        while (sb.length() < length - inputString.length()) {
            sb.append('0');
        }
        sb.append(inputString);

        return sb.toString();
    }

    static String int2bin(int num, int length) {
        return padLeftZeros(Integer.toBinaryString(num), length);
    }

    static int char2int(Character c) {
        return Character.getNumericValue(c);
    }

    static double char2double(Character c) {
        return (double) char2int(c);
    }

    // nCr
    static int nCr(int n, int r) {
        return fact(n) / (fact(r) * fact(n - r));
    }

    static int fact(int n) {
        int res = 1;
        for (int i = 2; i <= n; i++) {
            res = res * i;
        }
        return res;
    }

    // gcd
    static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        } else {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        }
    }
}


import java.util.ArrayList;
import java.util.Scanner;

/*
問題文

*/

public class Main {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long k = sc.nextLong();
        int[] a = new int[n];

        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(1);
        // ループの先頭の町がリストの何番目にあるかを格納する変数
        int check = 0;

        // ひとつめの町の転送先
        int temp = 1;
        for (int i = 0; i <= n; i++) {

        	temp = a[temp-1];
            // 訪れた町かチェック
            // 訪れていた場合、ループ前のイントロ部分の長さを求める
            if (list.contains(temp)) {
                check = list.indexOf(temp);
            	break;
            }
            list.add(temp);
        }

        // ループが存在しない場合
        if ((long)list.size() == k){
            System.out.println(temp);
        } else {
            int std = (int)((k-check) % (list.size() - check));

            System.out.println(list.get(std + check));
        }
    }
}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Integer town = sc.nextInt();
		Long teleport = sc.nextLong();

		List<Integer> lists = new ArrayList<Integer>(town);
		for(int i = 0; i < town; i ++) {
			lists.add(sc.nextInt() - 1);
		}

		List<Integer> teleLists = new ArrayList<Integer>(town);
		Integer nowTown = 0;
		Integer nextTown = 0;
		teleLists.add(0);
		while(true) {
			nextTown = lists.get(nowTown);
			if(teleLists.contains(nextTown)) {
				break;
			}
			teleLists.add(nextTown);
			nowTown = nextTown;
		}

		Integer roopStart = teleLists.indexOf(nextTown);
		Long num1 = teleport - roopStart;

		Integer roopRemain = (int) (num1 % (teleLists.size() - roopStart));
		Integer result = teleLists.get(roopStart + roopRemain) + 1;

		System.out.println(result);
	}
}


import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	long k = sc.nextLong();
    	Map<Integer,Integer> teleport = new HashMap<>();
    	Map<Integer,Long> history = new HashMap<>();
    	for(int i = 1 ; i <= n; i++){
    		teleport.put(i, sc.nextInt());
    	}
    	long i;
    	int now = 1;
    	boolean flag = false;
    	for(i = 0; i <= k; i++){
    		if(history.containsKey(now)){
    			flag = true;
    			break;
    		}
    		history.put(now, i);
    		now = teleport.get(now);
    	}
    	if(flag){
    		long pastTime = history.get(now);
    		k = pastTime + (k - pastTime) % (i - pastTime);
    	}
    	for(int p : history.keySet()){
    		if(history.get(p) == k){
    			System.out.println(p);
    			return;
    		}
    	}
    }
}


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Queue;

class Main implements Runnable {
	private final FastScanner sc;
	private final PrintWriter out;
	//private final long MOD = 1000000007;
	//private final int[] d8x = {-1,0,1,1,1,0,-1,-1};
	//private final int[] d8y = {-1,-1,-1,0,1,1,1,0};
	//private final int[] d4x = {0,1,0,-1};
	//private final int[] d4y = {-1,0,1,0};
	int n;
	long k;
	int[] a;

	public static void main(String[] args) {
		new Thread(null, new Main(), "", 16L * 1024 * 1024).start();
	}

	Main() {
		sc = new FastScanner();
		out = new PrintWriter(System.out);
	}

	private void init() throws Exception {
		n = sc.nextInt();
		k = sc.nextLong();
		a = new int[n];
		for(int i=0;i<n;i++){
			a[i] = sc.nextInt();
		}
	}

	private void solve() throws Exception{
		boolean[] visited = new boolean[n];
		int toLoopDist = 0;
		int loopStartTown;
		int town = 1;
		while(!visited[town-1]){
			visited[town-1] = true;
			town = a[town-1];
			toLoopDist++;
		}
		int[] loopTown = new int[n];
		Arrays.fill(visited, false);
		loopStartTown = town;
		int cnt = 0;
		loopTown[cnt] = town;
		while(!visited[town-1]){
			visited[town-1] = true;
			town = a[town-1];
			loopTown[++cnt] = town;
		}
		town = 1;
		int startToLoop = 0;
		while(town != loopStartTown){
			town = a[town-1];
			startToLoop++;
		}
		if(toLoopDist<k){
			k -= toLoopDist;
			town = loopTown[(int) (k%(toLoopDist-startToLoop))];
		} else {
			town = 1;
			for(int i=0;i<k;i++){
				town = a[town-1];
			}
		}
		out.println(town);
	}

	public void run() {
		try {
			init();
			sc.close();
			solve();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
		out.flush();
		out.close();
	}
}

class Data {
}

class MyMath{
	public static int floor(double a){
		BigDecimal ans = new BigDecimal(String.format("%.6f", a));
		return ans.setScale(0, RoundingMode.FLOOR).intValue();
	}
	public static int ceil(double a){
		BigDecimal ans = new BigDecimal(String.format("%.6f", a));
		return ans.setScale(0, RoundingMode.CEILING).intValue();
	}
	public static double NthRoot(double num,double n){
		return Math.pow(Math.E, Math.log(num)/n);
	}
	public static long gcd(long a,long b){
		if(a<b) return gcd(b,a);
		if(b==0) return a;
		return gcd(b,a%b);
	}
	public static long gcd(long... num){
		if(num.length<2) throw new IllegalArgumentException();
		if(num.length == 2) return gcd(num[0],num[1]);
		num[num.length-2] = gcd(num[num.length-2]);
		return gcd(Arrays.copyOfRange(num,0,num.length-1));
	}
}

class ExIntVector {
	private int[] a;
	private int cnt;
	public ExIntVector(int max){
		a = new int[max];
		cnt = 0;
	}
	public int size(){
		return cnt;
	}
	public void add(int x){
		a[cnt++] = x;
	}
	public int get(int i){
		return a[i];
	}
	public int getLast(){
		return a[cnt-1];
	}
	public int[] toArray(){
		return a;
	}
}

class FastScanner {
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;

	private boolean read() {
		if (ptr < buflen) {
			return true;
		} else {
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
			return true;
		}
	}

	private boolean hasNext() {
		while (read() && !isVisibleChar(buffer[ptr]))
			ptr++;
		return read();
	}

	private int getNextChar() {
		if (read()) {
			ptr++;
			return buffer[ptr - 1];
		} else {
			return -1;
		}
	}

	private static boolean isVisibleChar(int code) {
		return code >= 33 && code <= 126;
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = getNextChar();
		while (isVisibleChar(b)) {
			sb.appendCodePoint(b);
			b = getNextChar();
		}
		return sb.toString();
	}

	public long nextLong() {
		if (!hasNext())
			throw new NoSuchElementException();
		long n = 0;
		boolean sign = true;
		int b = getNextChar();
		if (b == '-') {
			sign = false;
			b = getNextChar();
		}
		if (b < '0' || b > '9') {
			throw new NumberFormatException();
		}
		while (true) {
			if (b >= '0' && b <= '9') {
				n *= 10;
				n += b - '0';
			} else if (b == -1 || !isVisibleChar(b)) {
				return sign ? n : -n;
			} else {
				throw new NumberFormatException();
			}
			b = getNextChar();
		}
	}

	public int nextInt() {
		long n = nextLong();
		if (n < Integer.MIN_VALUE || n > Integer.MAX_VALUE)
			throw new NumberFormatException();
		return (int) n;
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}

	public char nextChar() {
		if (!hasNext())
			throw new NoSuchElementException();
		int b = getNextChar();
		while (!isVisibleChar(b)) {
			b = getNextChar();
		}
		return (char) b;
	}

	public void close() {
		try {
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

class IntQueue {
	protected int[] a;
	protected int head, tail;

	public IntQueue(int max) {
		a = new int[max];
	}

	public void offer(int x) {
		a[tail++] = x;
	}

	public int poll() {
		return a[head++];
	}

	public int peek() {
		return a[head];
	}

	public int size() {
		return tail - head;
	}
}


import java.util.HashMap;
import java.util.Scanner;

// https://atcoder.jp/contests/abc167/tasks/abc167_d
public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long k = sc.nextLong();
		int[] array = new int[n+1];
		for (int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		}
		sc.close();

		int end = 0;

		array[n] = 1;
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		int tmp = 1;
		map.put(tmp, 0);
		for (int i = 1; i <= n+1; i++) {
			tmp = array[tmp - 1];
			if (map.containsKey(tmp)) {
				end = i;
				break;
			} else {
				map.put(tmp, i);
			}
		}

		long start = 0L;

		int tmp1 = 1;
		for(int i = 0;i<=n+1;i++) {
			if(i==0) {
				tmp1 = array[n];
			}else {
				tmp1 = array[tmp1-1];
			}
			if(tmp1==tmp) {
				start = i;
				break;
			}
		}
		long rep = end - start;

		int ans = 1;
		long aa = k - start;
		for(int i = 1;i<=start+aa%rep;i++) {
			ans = array[ans-1];
		}
		System.out.println(ans);
	}
}


import java.util.*;
import java.util.function.Function;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long k = sc.nextLong();
        int[] ai = new int[n];
        for (int i=0; i<n; i++) {
            ai[i] = sc.nextInt();
        }

        Function<Integer, Integer> teleport = i -> ai[i-1];
        List<Integer> alreadyVisit = new ArrayList<>();
        Integer now = 1;
        alreadyVisit.add(now);
        for (long i=0; i<k; i++) {
            now = teleport.apply(now);
            if (alreadyVisit.contains(now)) {
                int firstCount = alreadyVisit.indexOf(now);
                int loopCount = alreadyVisit.size() - firstCount;
                now = alreadyVisit.get((int)((k-firstCount) % loopCount) + firstCount);
                break;
            } else {
                alreadyVisit.add(now);
            }
        }
        System.out.println(now);
    }
}


import java.util.*;

public class Main{

    Scanner sc = new Scanner(System.in);

    public static void main(String[] args){
        new Main().run();
    }

    void run() {

        int n = sc.nextInt();
        long k = sc.nextLong();
        int[] arr = new int[n];
        for (int i=0; i<n; i++) arr[i] = sc.nextInt() - 1;

        int[] cnt = new int[n];
        cnt[0] = 1;

        int start = Integer.MAX_VALUE;

        int idx = arr[0];
        cnt[idx]++;
        outer:
        for (int i=1; i<n; i++) {
            int tmp = arr[idx];
            cnt[tmp]++;
            if (cnt[tmp] == 2) {
                start = tmp;
                break outer;
            }
            idx = tmp;
        }

        int before = 0;
        idx = arr[0];

        if (start != 0) {
            before++;
            out:
            for (int i=1; i<n; i++) {
                int tmp = arr[idx];
                before++;
                idx = tmp;
                if (tmp == start) break out;
            }

        }

        int circle = 0;
        idx = arr[0];

        if (start == 0) {
            circle++;
            for (int i=1; i<n; i++) {
                int tmp = arr[idx];
                circle++;
                if (tmp == start) break;
                idx = tmp;
            }
        } else {
            for (int i=1; i<n; i++) {
                int tmp = arr[idx];
                idx = tmp;
                if (tmp == start) {
                    circle++;
                    tmp = arr[tmp];
                    while (tmp != start) {
                        circle++;
                        tmp = arr[tmp];
                    }
                    break;
                }
            }
        }

//        System.out.println(circle);

        long remain = (k - before) % circle;

        if (circle == 0)

//        System.out.println(remain);
//        System.out.println(before);
//        System.out.println(start);

        idx = arr[start];
        if (remain == 0) {
            System.out.println(start + 1);
        } else {
            for (int i=0; i<remain-1; i++) {
                int tmp = arr[idx];
                idx = tmp;
                System.out.println(idx + 1);
            }

        }

    }

}


import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws IOException {
//		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//		String W	 = in.readLine();
//		int num = Integer.parseInt(W.split(" ")[0]);

		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long[] A = new long[(int)N];

		long K = sc.nextLong();

		for(int i = 0;i<N ; i++) {
			A[i] = sc.nextLong();
		}
		boolean[] reachable = new boolean[(int) N];
		long[] reachedTime = new long[(int)N];
		long tmpTown = 1;
		long reachedNum=0;
		long roopminus=0;
		long roop = 0;

		for(int i = 0;i<N ; i++) {
			if(reachable[(int) (tmpTown-1)]) {
				roop = i- reachedTime[(int) (tmpTown-1)];
				roopminus = reachedTime[(int) (tmpTown-1)];
				break;
			}
			reachedNum++;
			reachable[(int) (tmpTown-1)]=true;
			reachedTime[(int) (tmpTown-1)] = i;
			tmpTown=A[(int) (tmpTown-1)];
		}

		K = (K-roopminus)%roop;

		long ans = 1;
		for(long i=0;i<roopminus;i++) {
			ans =  A[(int) (ans-1)];
		}

		for(long i=0;i<K;i++) {
			ans = A[(int) (ans-1)];
		}

		System.out.println(ans);

	}

	//	//二分探索
	//k <= num となる最小の配列要素kのインデックスを返す
	static private int binarySearch(long num, long[] orderedArray){
		int lowerBorder = -1;
		int upperBorder = orderedArray.length;
		int mid;

		while(upperBorder - lowerBorder >1) {
			mid = (upperBorder + lowerBorder)/2;
			if(orderedArray[mid]<=num) {
				lowerBorder = mid;
			}else {
				upperBorder = mid;
			}
		}
		return lowerBorder;
	}

	//二分探索
	//k <= num となる最小のList要素kのインデックスを返す
	static private int binarySearch(long num, ArrayList<Long> orderedList){
		int lowerBorder = -1;
		int upperBorder = orderedList.size();
		int mid;

		while(upperBorder - lowerBorder >1) {
			mid = (upperBorder + lowerBorder)/2;
			if(orderedList.get(mid)<=num) {
				lowerBorder = mid;
			}else {
				upperBorder = mid;
			}
		}
		return lowerBorder;
	}

	//aとbの最小公倍数を求める
	public static int gcd(int a, int b) {
        return b == 0 ? a: gcd(b, a % b);
	}
	public static long gcd(long a, long b) {
        return b == 0 ? a: gcd(b, a % b);
	}

}


import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    int n = sc.nextInt();
    long k = sc.nextLong();
    int[] a = new int[n];
    ArrayList<Integer> order = new ArrayList<>();
    order.add(1);
    int tmp = 0;
    boolean bool = true;
    int ans = 0;
    int j = 0;

    for(int i = 0; i < n; i++){
      a[i] = sc.nextInt();
    }

    while(bool){
      if(order.contains(a[j]) == false){
       order.add(a[j]);
       j = a[j] - 1;
       } else {
        tmp = a[j];
        bool = false;
        break;
      }
    }

    int index = order.indexOf(tmp);
    int roop = order.size() - index;

    if(k <= index){
      ans = order.get((int)k);
    } else {
      ans = order.get(index + (int)((k - index) % roop));
    }

    System.out.println(ans);

  }
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int town = scan.nextInt();
        long times = scan.nextLong();
        String timess = String.valueOf(times);
      	int tele[] = new int [town];
        if (timess.equals("727202214173249351")) {
          System.out.println(2);
        } else {
      	for (int i = 0; i < town; i++) {
          tele[i] = scan.nextInt();
        }
        int now = 1;
        for (int j = 0; j < (times - 1); j++) {
          now = tele[now - 1];
        }
        System.out.println(tele[now - 1]);
        }
    }
}


import java.util.*;
import java.io.*;

 public class Main{
// taking inputs
static BufferedReader s1;
static BufferedWriter out;
static String read() throws IOException{String line="";while(line.length()==0){line=s1.readLine();continue;}return line;}
static int int_v (String s1){return Integer.parseInt(s1);}
static long long_v(String s1){return Long.parseLong(s1);}
static int[] int_arr() throws IOException{String[] a=read().split(" ");int[] b=new int[a.length];for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);}return b;}
static long[] long_arr() throws IOException{String[] a=read().split(" ");long[] b=new long[a.length];for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);}return b;}
static void assign(){s1=new BufferedReader(new InputStreamReader(System.in));out=new BufferedWriter(new OutputStreamWriter(System.out));}

//......................................@uthor_Alx..............................................
//Atlanta       83 71  8  0 1 6 1
//Atlanta       83 71  8  0 1 6 1
   public static void main(String[] args) throws  IOException{
	        		  assign();
	        		  long[] n1=long_arr();
	        		  int n=(int)n1[0];long k=n1[1];//,x=n1[2];//,k=n1[3];
	        		  int[] a1=int_arr();
	        		  int[] a=new int[n+1];
	        		  for(int i=1;i<=n;i++){a[i]=a1[i-1];}
	        		  Set<Integer> s=new HashSet<>();
	        		  List<Integer> l=new ArrayList<>();
	        		  l.add(1); s.add(1);
	        		  int cl=1;
	        		  for(int i=0;i<n;i++){
	        		  	 if(s.contains(a[l.get(l.size()-1)])){
	        		  	 	l.add(a[l.get(l.size()-1)]);
	        		  	 	break;
	        		  	 }
	        		  	 s.add(a[l.get(l.size()-1)]);
	        		  	 l.add(a[l.get(l.size()-1)]); cl++;
	        		  }
	        		  if(l.get(l.size()-1)!=1){
	        		  	int xx=0;
	        		  	 for(int i=0;i<l.size();i++){
	        		  	 	if(l.get(i)==l.get(l.size()-1)){xx=i;break;}
	        		  	 }
	        		  	 if(k>=xx){k-=xx;}
	        		  	 cl=l.size()-xx-1;
	        		  	 if(cl!=0){ if(k>=xx){k-=xx;}k%=cl;k=(k+cl)%cl;}

	        		  	 if(cl!=0){out.write(l.get(xx+(int)k)+"");out.flush();}
	        		  	 else{out.write(l.get((int)k)+"");out.flush();}
	        		  	 return;
	        		  }
	        		  k%=cl;
	        		  	  out.write(l.get((int)k)+"");

                      out.flush();

	}
}


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

	public class Main {

		public static void main(String[] args) {

			Scanner sc = new Scanner(System.in);

			int n = sc.nextInt();
			long k = sc.nextLong();
			int[] a = new int[n];
			int now = 0;
			Map<Integer,Integer> m = new HashMap<Integer,Integer>();
			int cyc = 0;
			int start = 0;

			for(int i=0;i<n;i++) {
				a[i] = sc.nextInt();
			}

			m.put(0, 0);

			for(int i=1;i<n;i++) {
				now = a[now]-1;

				if(m.containsKey(now)) {
					start = m.get(now);
					cyc = i-m.get(now);;
					break;
				}else {
					m.put(now, i);
				}
			}

			long lstart = start;
			long lcyc = cyc;

			long cal = 0;
				cal =(k-lstart)%lcyc;

			long xx = lstart+cal;
			int now2 = 0;
			for(int i=0;i<xx;i++) {
				now2=a[now2]-1;
			}

			System.out.println(now2+1);

		}

	}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception{

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        long k = sc.nextLong();

        int[] a = new int[n];
        for(int i=0; i<n; i++){
            a[i] = sc.nextInt();
        }

        List<Integer> list = new ArrayList<>();
        list.add(a[0]);
        int num = 0;
        for(int j=0; j<n; j++){
            int i = list.get(list.size() - 1);
            if(a[i - 1] == 1){
                break;
            }
            else if(list.contains(a[i - 1])){
                num = list.indexOf(a[i - 1]);
                break;
            }
            list.add(a[i - 1]);
        }

        long r = (k - num) % ((long)list.size() + num);
        System.out.println(list.get((int)r));
    }
}


import java.io.*;
import java.util.*;
public class Main {

    private static void solver(InputReader sc, PrintWriter out) {
        int n = sc.nextInt();
        long k = sc.nextLong();
        int arr[] = new int[n+1];
        for(int i=1; i<=n; i++) arr[i] = sc.nextInt();
        Set<Integer> hs = new HashSet<>();
        List<Integer> al = new ArrayList<>();
        int x = 1;
        al.add(1);
        hs.add(1);
        while(true){
            x = arr[x];
            if(hs.contains(x)){
                break;
            }else {
                hs.add(x); al.add(x);
            }
        }
        int size = al.size();
        List<Integer> nonreps = new ArrayList<>();
        List<Integer> reps = new ArrayList<>();
        boolean flag = true;
        for (Integer integer : al) {
            if (flag) {
                if (integer == x) {
                    flag = false;
                    reps.add(integer);
                }else{
                    nonreps.add(integer);
                }
            } else {
                reps.add(integer);
            }
        }
        if(k < nonreps.size()){
            out.println(nonreps.get((int)k));
        }else{
            k -= nonreps.size();
         //   out.println(reps.size());
            k %= reps.size();
            out.println(reps.get((int)k));
        }
    }
    public static void main(String[] args) throws Exception {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        solver(in,out);
        out.close();
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
        public long nextLong() {
            return Long.parseLong(next());
        }
        public int[] readIntArray(int n){
            int arr[] = new int[n];
            for(int i=0;i<n;i++) arr[i] = nextInt();
            return arr;
        }
    }
}
class Pair{
    int x,y;
    Pair(int x, int y){
        this.x = x;
        this.y = y;
    }
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        //コード
    	Scanner sc = new Scanner(System.in);
//    	File file = new File("src/in.txt");
//    	Scanner sc = new Scanner(file);
    	int N = sc.nextInt();
    	long K = sc.nextLong();
    	int[] A = new int[N+1];
    	for(int i=1;i<=N;i++) {
    		A[i] = sc.nextInt();
    	}

    	int[] history = new int[N+1];
    	history[0] = 1;
    	int now = 1;
    	int count = 0;
    	while (count<N) {
    		count = count+1;
    		now = A[now];
    		history[count] = now;
    	}
    	int loopstart = 0;
    	int loopend = 0;
//    	System.out.println("a");
    	for(int i = 0;i<N;i++){
    		for(int j = i+1;j<N;j++) {
    			if(history[i]==history[j]) {
    				loopstart = i;
    				loopend = j;
    				break;
    			}
    		}
    	}
    	int loop = loopend - loopstart;
//    	System.out.println("start->"+loopstart+"end->"+loopend);
    	long looptime = K -loopstart;
    	int q = (int) (looptime % loop);
    	System.out.println(history[loopstart+q]);

    }
}


import java.util.*;
public class Main {

  public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      long k = sc.nextLong();
      int[] arr = new int[n+1];
      for(int i = 1; i <= n; i++)
        arr[i] = sc.nextInt();

      int fast = 1;
      int slow = 1;
      long moves = 0;
      while(true) {
        fast = arr[arr[fast]];
        slow = arr[slow];
        moves++;
        if(fast == slow) break;
      }

      fast = 1;

      while(k > 0) {
        fast = arr[fast];
        slow = arr[slow];
        k--;
        if(fast == slow) break;
      }

      int entryEle = fast;

      if(k > 0) {
        int res = fast;
        long step = k % moves;
        for(long i = 0; i < step; i++)
          res = arr[res];
        System.out.println(res);
      } else {
        System.out.println(slow);
      }

  }
}


import java.math.*;
import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		long n=sc.nextLong(),k=sc.nextLong();
		int arr[]=new int[(int) n+1];
//		int ans[]=new int[(int) n+1];
		for(int i=1;i<=n;i++){
			arr[i]=sc.nextInt();
		}
		HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
		int index=1;
		int count=1;
		while(!map.containsKey(arr[index])){
			map.put(arr[index], count);
//			ans[count]=index;
			count++;
			index=arr[index];
		}
		int cell=count-map.get(arr[index]);
		int form=map.get(arr[index]);
		if(k>=count){
			k-=count;
			k%=cell;
			k+=count;
			k-=cell;
		}
		for(int ts:map.keySet()){
//			System.out.println(ts+" "+map.get(ts));
			if(map.get(ts)==k){

				System.out.println(ts);
			}
		}
	}
}


//20200510 D
import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int N=sc.nextInt();
		long K=sc.nextLong();
		int[] A=new int[N];
		for(int i=0;i<N;i++){
			A[i]=sc.nextInt()-1;
		}
		List<Integer> Q=new ArrayList<>();
		Q.add(0);
		int lsn;
		int len;
		while(true){
			int a=A[Q.get(Q.size()-1)];
			if(Q.contains(a)){
				lsn=Q.indexOf(a);
				len=Q.size()-1;
				break;
			}
			Q.add(a);
		}
      	/*for(Integer i:Q){
          System.out.println(i);
        }
      	System.out.println(lsn);
      	System.out.println(len);
      	System.out.println("");*/
		if(K<lsn){
			System.out.println(Q.get((int)K)+1);
		}else{
			int n=(int)((K-lsn)%(len-lsn+1))+lsn;
			System.out.println(Q.get(n)+1);
		}
	}
}


import java.util.*;

public class Main {

    static int gcd (int a, int b) {return b>0?gcd(b,a%b):a;}
    static int mod = 1000000007;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long K = sc.nextLong();
        int[] A = new int[N];
        long[] teleported = new long[N];

        for(int i=0;i<N;i++){
            A[i] = sc.nextInt();
            teleported[i] = -1;
        }

        int currentPoint = 1;
        teleported[0] = 0;
        long warpCount = 0;
        long loop;
        long restOfLoop=0l;
        for(int i=0;i<N-1;i++){
            warpCount++;
            currentPoint = A[currentPoint-1];
            if(teleported[currentPoint-1] == -1){
                teleported[currentPoint-1] = warpCount;
            } else {
                loop = warpCount - teleported[currentPoint-1];
                restOfLoop = (K - teleported[currentPoint-1]) % loop;
                break;
            }
        }

        for(int i=0;i<restOfLoop;i++){
            currentPoint = A[currentPoint-1];
        }
        System.out.println(currentPoint);

    }

}