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

public class Main {
	static int MAXN = 1001, mod = (int) (1e9 + 7);
	static long comb[][];

	public static void main(String[] args) {
		InputReader s = new InputReader(System.in);
		PrintWriter out = new PrintWriter(System.out);
		/*My code starts here.*/
		int n = s.nextInt();
		boolean arr[] = new boolean[n+1];
		int inp[] = new int[n+1];
		for(int i=1;i<=n;i++)
			inp[i] = s.nextInt();
		int flg=0,count=0;
		int i=1;
		while(true){
			int k = inp[i];
			if(arr[k]){
				flg = -1;
				break;
			}
			arr[i] = true;
			count++;
			if(inp[i]==2){
				break;
			}
			i = inp[i];
		}
		if(flg==-1)
			out.println("-1");
		else
			out.println(count);
		/*My code ends here.*/
		out.close();
	}

	static class Node implements Comparable<Node> {
		int num, freq, idx;

		public Node(int u, int v, int idx) {
			this.num = u;
			this.freq = v;
			this.idx = idx;
		}

		public int compareTo(Node n) {
			if (this.freq == n.freq)
				return Integer.compare(this.num, n.num);
			return Integer.compare(-this.freq, -n.freq);
		}
	}

	static void preCalcComb() {
		comb = new long[MAXN][MAXN];
		comb[0][0] = 1;
		for (int i = 1; i < MAXN; i++) {
			comb[i][0] = 1;
			for (int j = 1; j <= i; j++) {
				comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;
			}
		}
	}

	static long PowerMod(long a, long b, long m) {
		long tempo;
		if (b == 0)
			tempo = 1;
		else if (b == 1)
			tempo = a;
		else {
			long temp = PowerMod(a, b / 2, m);
			if (b % 2 == 0)
				tempo = (temp * temp) % m;
			else
				tempo = ((temp * temp) % m) * a % m;
		}
		return tempo;
	}

	long no_of_primes(long m, long n, long k) {
		long count = 0, i, j;
		int primes[] = new int[(int) (n - m + 2)];
		if (m == 1)
			primes[0] = 1;
		for (i = 2; i <= Math.sqrt(n); i++) {
			j = (m / i);
			j *= i;
			if (j < m)
				j += i;
			for (; j <= n; j += i) {
				if (j != i)
					primes[(int) (j - m)] = 1;
			}
		}
		for (i = 0; i <= n - m; i++)
			if (primes[(int) i] == 0 && (i - 1) % k == 0)
				count++;
		return count;
	}

	static void shortestPaths(List<Edge>[] edges, int s, int[] prio, int[] pred) {
		Arrays.fill(pred, -1);
		Arrays.fill(prio, Integer.MAX_VALUE);
		prio[s] = 0;
		PriorityQueue<Long> q = new PriorityQueue<>();
		q.add((long) s);
		while (!q.isEmpty()) {
			long cur = q.remove();
			int curu = (int) cur;
			if (cur >>> 32 != prio[curu])
				continue;
			for (Edge e : edges[curu]) {
				int v = e.t;
				int nprio = prio[curu] + e.cost;
				if (prio[v] > nprio) {
					prio[v] = nprio;
					pred[v] = curu;
					q.add(((long) nprio << 32) + v);
				}
			}
		}
	}

	static class Edge {
		int t, cost;

		public Edge(int t, int cost) {
			this.t = t;
			this.cost = cost;
		}
	}

	static class InputReader {

		private InputStream stream;
		private byte[] buf = new byte[8192];
		private int curChar, snumChars;
		private SpaceCharFilter filter;

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

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

		public int nextInt() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public long nextLong() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

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

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

		public boolean isSpaceChar(int c) {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
	}
} import java.util.*; util.* java import java.io.*; io.* java public class Main {
	static int MAXN = 1001, mod = (int) (1e9 + 7);
	static long comb[][];

	public static void main(String[] args) {
		InputReader s = new InputReader(System.in);
		PrintWriter out = new PrintWriter(System.out);
		/*My code starts here.*/
		int n = s.nextInt();
		boolean arr[] = new boolean[n+1];
		int inp[] = new int[n+1];
		for(int i=1;i<=n;i++)
			inp[i] = s.nextInt();
		int flg=0,count=0;
		int i=1;
		while(true){
			int k = inp[i];
			if(arr[k]){
				flg = -1;
				break;
			}
			arr[i] = true;
			count++;
			if(inp[i]==2){
				break;
			}
			i = inp[i];
		}
		if(flg==-1)
			out.println("-1");
		else
			out.println(count);
		/*My code ends here.*/
		out.close();
	}

	static class Node implements Comparable<Node> {
		int num, freq, idx;

		public Node(int u, int v, int idx) {
			this.num = u;
			this.freq = v;
			this.idx = idx;
		}

		public int compareTo(Node n) {
			if (this.freq == n.freq)
				return Integer.compare(this.num, n.num);
			return Integer.compare(-this.freq, -n.freq);
		}
	}

	static void preCalcComb() {
		comb = new long[MAXN][MAXN];
		comb[0][0] = 1;
		for (int i = 1; i < MAXN; i++) {
			comb[i][0] = 1;
			for (int j = 1; j <= i; j++) {
				comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;
			}
		}
	}

	static long PowerMod(long a, long b, long m) {
		long tempo;
		if (b == 0)
			tempo = 1;
		else if (b == 1)
			tempo = a;
		else {
			long temp = PowerMod(a, b / 2, m);
			if (b % 2 == 0)
				tempo = (temp * temp) % m;
			else
				tempo = ((temp * temp) % m) * a % m;
		}
		return tempo;
	}

	long no_of_primes(long m, long n, long k) {
		long count = 0, i, j;
		int primes[] = new int[(int) (n - m + 2)];
		if (m == 1)
			primes[0] = 1;
		for (i = 2; i <= Math.sqrt(n); i++) {
			j = (m / i);
			j *= i;
			if (j < m)
				j += i;
			for (; j <= n; j += i) {
				if (j != i)
					primes[(int) (j - m)] = 1;
			}
		}
		for (i = 0; i <= n - m; i++)
			if (primes[(int) i] == 0 && (i - 1) % k == 0)
				count++;
		return count;
	}

	static void shortestPaths(List<Edge>[] edges, int s, int[] prio, int[] pred) {
		Arrays.fill(pred, -1);
		Arrays.fill(prio, Integer.MAX_VALUE);
		prio[s] = 0;
		PriorityQueue<Long> q = new PriorityQueue<>();
		q.add((long) s);
		while (!q.isEmpty()) {
			long cur = q.remove();
			int curu = (int) cur;
			if (cur >>> 32 != prio[curu])
				continue;
			for (Edge e : edges[curu]) {
				int v = e.t;
				int nprio = prio[curu] + e.cost;
				if (prio[v] > nprio) {
					prio[v] = nprio;
					pred[v] = curu;
					q.add(((long) nprio << 32) + v);
				}
			}
		}
	}

	static class Edge {
		int t, cost;

		public Edge(int t, int cost) {
			this.t = t;
			this.cost = cost;
		}
	}

	static class InputReader {

		private InputStream stream;
		private byte[] buf = new byte[8192];
		private int curChar, snumChars;
		private SpaceCharFilter filter;

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

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

		public int nextInt() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public long nextLong() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

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

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

		public boolean isSpaceChar(int c) {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
	}
} Main static int MAXN = 1001 MAXN 1001 mod = (int) (1e9 + 7); mod (int) (1e9 + 7) 1e9 7 static long comb[][]; comb public static void main(String[] args) {
		InputReader s = new InputReader(System.in);
		PrintWriter out = new PrintWriter(System.out);
		/*My code starts here.*/
		int n = s.nextInt();
		boolean arr[] = new boolean[n+1];
		int inp[] = new int[n+1];
		for(int i=1;i<=n;i++)
			inp[i] = s.nextInt();
		int flg=0,count=0;
		int i=1;
		while(true){
			int k = inp[i];
			if(arr[k]){
				flg = -1;
				break;
			}
			arr[i] = true;
			count++;
			if(inp[i]==2){
				break;
			}
			i = inp[i];
		}
		if(flg==-1)
			out.println("-1");
		else
			out.println(count);
		/*My code ends here.*/
		out.close();
	} main {
		InputReader s = new InputReader(System.in);
		PrintWriter out = new PrintWriter(System.out);
		/*My code starts here.*/
		int n = s.nextInt();
		boolean arr[] = new boolean[n+1];
		int inp[] = new int[n+1];
		for(int i=1;i<=n;i++)
			inp[i] = s.nextInt();
		int flg=0,count=0;
		int i=1;
		while(true){
			int k = inp[i];
			if(arr[k]){
				flg = -1;
				break;
			}
			arr[i] = true;
			count++;
			if(inp[i]==2){
				break;
			}
			i = inp[i];
		}
		if(flg==-1)
			out.println("-1");
		else
			out.println(count);
		/*My code ends here.*/
		out.close();
	} InputReader s = new InputReader(System.in); s new InputReader(System.in) PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) int n = s.nextInt(); n s.nextInt() s.nextInt s boolean arr[] = new boolean[n+1]; arr new boolean[n+1] n+1 n 1 int inp[] = new int[n+1]; inp new int[n+1] n+1 n 1 for(int i=1;i<=n;i++)
			inp[i] = s.nextInt(); int i=1; int i=1; i 1 i<=n i n i++ i++ i inp[i] = s.nextInt(); inp[i] = s.nextInt() inp[i] inp i s.nextInt() s.nextInt s int flg=0 flg 0 count=0; count 0 int i=1; i 1 while(true){
			int k = inp[i];
			if(arr[k]){
				flg = -1;
				break;
			}
			arr[i] = true;
			count++;
			if(inp[i]==2){
				break;
			}
			i = inp[i];
		} true {
			int k = inp[i];
			if(arr[k]){
				flg = -1;
				break;
			}
			arr[i] = true;
			count++;
			if(inp[i]==2){
				break;
			}
			i = inp[i];
		} int k = inp[i]; k inp[i] inp i if(arr[k]){
				flg = -1;
				break;
			} arr[k] arr k {
				flg = -1;
				break;
			} flg = -1 flg -1 1 break; arr[i] = true arr[i] arr i true count++ count if(inp[i]==2){
				break;
			} inp[i]==2 inp[i] inp i 2 {
				break;
			} break; i = inp[i] i inp[i] inp i if(flg==-1)
			out.println("-1");
		else
			out.println(count); flg==-1 flg -1 1 out.println("-1") out.println out "-1" out.println(count) out.println out count out.close() out.close out String[] args args static void preCalcComb() {
		comb = new long[MAXN][MAXN];
		comb[0][0] = 1;
		for (int i = 1; i < MAXN; i++) {
			comb[i][0] = 1;
			for (int j = 1; j <= i; j++) {
				comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;
			}
		}
	} preCalcComb {
		comb = new long[MAXN][MAXN];
		comb[0][0] = 1;
		for (int i = 1; i < MAXN; i++) {
			comb[i][0] = 1;
			for (int j = 1; j <= i; j++) {
				comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;
			}
		}
	} comb = new long[MAXN][MAXN] comb new long[MAXN][MAXN] MAXN MAXN comb[0][0] = 1 comb[0][0] comb[0] comb 0 0 1 for (int i = 1; i < MAXN; i++) {
			comb[i][0] = 1;
			for (int j = 1; j <= i; j++) {
				comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;
			}
		} int i = 1; int i = 1; i 1 i < MAXN i MAXN i++ i++ i {
			comb[i][0] = 1;
			for (int j = 1; j <= i; j++) {
				comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;
			}
		} {
			comb[i][0] = 1;
			for (int j = 1; j <= i; j++) {
				comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;
			}
		} comb[i][0] = 1 comb[i][0] comb[i] comb i 0 1 for (int j = 1; j <= i; j++) {
				comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;
			} int j = 1; int j = 1; j 1 j <= i j i j++ j++ j {
				comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;
			} {
				comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;
			} comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod comb[i][j] comb[i] comb i j (comb[i - 1][j] + comb[i - 1][j - 1]) % mod (comb[i - 1][j] + comb[i - 1][j - 1]) comb[i - 1][j] comb[i - 1] comb i - 1 i 1 j comb[i - 1][j - 1] comb[i - 1] comb i - 1 i 1 j - 1 j 1 mod static long PowerMod(long a, long b, long m) {
		long tempo;
		if (b == 0)
			tempo = 1;
		else if (b == 1)
			tempo = a;
		else {
			long temp = PowerMod(a, b / 2, m);
			if (b % 2 == 0)
				tempo = (temp * temp) % m;
			else
				tempo = ((temp * temp) % m) * a % m;
		}
		return tempo;
	} PowerMod {
		long tempo;
		if (b == 0)
			tempo = 1;
		else if (b == 1)
			tempo = a;
		else {
			long temp = PowerMod(a, b / 2, m);
			if (b % 2 == 0)
				tempo = (temp * temp) % m;
			else
				tempo = ((temp * temp) % m) * a % m;
		}
		return tempo;
	} long tempo; tempo if (b == 0)
			tempo = 1;
		else if (b == 1)
			tempo = a;
		else {
			long temp = PowerMod(a, b / 2, m);
			if (b % 2 == 0)
				tempo = (temp * temp) % m;
			else
				tempo = ((temp * temp) % m) * a % m;
		} b == 0 b 0 tempo = 1 tempo 1 if (b == 1)
			tempo = a;
		else {
			long temp = PowerMod(a, b / 2, m);
			if (b % 2 == 0)
				tempo = (temp * temp) % m;
			else
				tempo = ((temp * temp) % m) * a % m;
		} b == 1 b 1 tempo = a tempo a {
			long temp = PowerMod(a, b / 2, m);
			if (b % 2 == 0)
				tempo = (temp * temp) % m;
			else
				tempo = ((temp * temp) % m) * a % m;
		} long temp = PowerMod(a, b / 2, m); temp PowerMod(a, b / 2, m) PowerMod a b / 2 b 2 m if (b % 2 == 0)
				tempo = (temp * temp) % m;
			else
				tempo = ((temp * temp) % m) * a % m; b % 2 == 0 b % 2 b 2 0 tempo = (temp * temp) % m tempo (temp * temp) % m (temp * temp) temp temp m tempo = ((temp * temp) % m) * a % m tempo ((temp * temp) % m) * a % m ((temp * temp) % m) * a ((temp * temp) % m) (temp * temp) temp temp m a m return tempo; tempo long a a long b b long m m long no_of_primes(long m, long n, long k) {
		long count = 0, i, j;
		int primes[] = new int[(int) (n - m + 2)];
		if (m == 1)
			primes[0] = 1;
		for (i = 2; i <= Math.sqrt(n); i++) {
			j = (m / i);
			j *= i;
			if (j < m)
				j += i;
			for (; j <= n; j += i) {
				if (j != i)
					primes[(int) (j - m)] = 1;
			}
		}
		for (i = 0; i <= n - m; i++)
			if (primes[(int) i] == 0 && (i - 1) % k == 0)
				count++;
		return count;
	} no_of_primes {
		long count = 0, i, j;
		int primes[] = new int[(int) (n - m + 2)];
		if (m == 1)
			primes[0] = 1;
		for (i = 2; i <= Math.sqrt(n); i++) {
			j = (m / i);
			j *= i;
			if (j < m)
				j += i;
			for (; j <= n; j += i) {
				if (j != i)
					primes[(int) (j - m)] = 1;
			}
		}
		for (i = 0; i <= n - m; i++)
			if (primes[(int) i] == 0 && (i - 1) % k == 0)
				count++;
		return count;
	} long count = 0 count 0 i i j; j int primes[] = new int[(int) (n - m + 2)]; primes new int[(int) (n - m + 2)] (int) (n - m + 2) n - m n m 2 if (m == 1)
			primes[0] = 1; m == 1 m 1 primes[0] = 1 primes[0] primes 0 1 for (i = 2; i <= Math.sqrt(n); i++) {
			j = (m / i);
			j *= i;
			if (j < m)
				j += i;
			for (; j <= n; j += i) {
				if (j != i)
					primes[(int) (j - m)] = 1;
			}
		} i = 2; i = 2 i 2 i <= Math.sqrt(n) i Math.sqrt(n) Math.sqrt Math n i++ i++ i {
			j = (m / i);
			j *= i;
			if (j < m)
				j += i;
			for (; j <= n; j += i) {
				if (j != i)
					primes[(int) (j - m)] = 1;
			}
		} {
			j = (m / i);
			j *= i;
			if (j < m)
				j += i;
			for (; j <= n; j += i) {
				if (j != i)
					primes[(int) (j - m)] = 1;
			}
		} j = (m / i) j (m / i) m i j *= i j i if (j < m)
				j += i; j < m j m j += i j i for (; j <= n; j += i) {
				if (j != i)
					primes[(int) (j - m)] = 1;
			} ; j <= n j n j += i j += i j i {
				if (j != i)
					primes[(int) (j - m)] = 1;
			} {
				if (j != i)
					primes[(int) (j - m)] = 1;
			} if (j != i)
					primes[(int) (j - m)] = 1; j != i j i primes[(int) (j - m)] = 1 primes[(int) (j - m)] primes (int) (j - m) j m 1 for (i = 0; i <= n - m; i++)
			if (primes[(int) i] == 0 && (i - 1) % k == 0)
				count++; i = 0; i = 0 i 0 i <= n - m i n - m n m i++ i++ i if (primes[(int) i] == 0 && (i - 1) % k == 0)
				count++; if (primes[(int) i] == 0 && (i - 1) % k == 0)
				count++; primes[(int) i] == 0 && (i - 1) % k == 0 primes[(int) i] == 0 primes[(int) i] primes (int) i 0 (i - 1) % k == 0 (i - 1) % k (i - 1) i 1 k 0 count++ count return count; count long m m long n n long k k static void shortestPaths(List<Edge>[] edges, int s, int[] prio, int[] pred) {
		Arrays.fill(pred, -1);
		Arrays.fill(prio, Integer.MAX_VALUE);
		prio[s] = 0;
		PriorityQueue<Long> q = new PriorityQueue<>();
		q.add((long) s);
		while (!q.isEmpty()) {
			long cur = q.remove();
			int curu = (int) cur;
			if (cur >>> 32 != prio[curu])
				continue;
			for (Edge e : edges[curu]) {
				int v = e.t;
				int nprio = prio[curu] + e.cost;
				if (prio[v] > nprio) {
					prio[v] = nprio;
					pred[v] = curu;
					q.add(((long) nprio << 32) + v);
				}
			}
		}
	} shortestPaths {
		Arrays.fill(pred, -1);
		Arrays.fill(prio, Integer.MAX_VALUE);
		prio[s] = 0;
		PriorityQueue<Long> q = new PriorityQueue<>();
		q.add((long) s);
		while (!q.isEmpty()) {
			long cur = q.remove();
			int curu = (int) cur;
			if (cur >>> 32 != prio[curu])
				continue;
			for (Edge e : edges[curu]) {
				int v = e.t;
				int nprio = prio[curu] + e.cost;
				if (prio[v] > nprio) {
					prio[v] = nprio;
					pred[v] = curu;
					q.add(((long) nprio << 32) + v);
				}
			}
		}
	} Arrays.fill(pred, -1) Arrays.fill Arrays pred -1 1 Arrays.fill(prio, Integer.MAX_VALUE) Arrays.fill Arrays prio Integer.MAX_VALUE Integer Integer.MAX_VALUE prio[s] = 0 prio[s] prio s 0 PriorityQueue<Long> q = new PriorityQueue<>(); q new PriorityQueue<>() q.add((long) s) q.add q (long) s while (!q.isEmpty()) {
			long cur = q.remove();
			int curu = (int) cur;
			if (cur >>> 32 != prio[curu])
				continue;
			for (Edge e : edges[curu]) {
				int v = e.t;
				int nprio = prio[curu] + e.cost;
				if (prio[v] > nprio) {
					prio[v] = nprio;
					pred[v] = curu;
					q.add(((long) nprio << 32) + v);
				}
			}
		} !q.isEmpty() q.isEmpty() q.isEmpty q {
			long cur = q.remove();
			int curu = (int) cur;
			if (cur >>> 32 != prio[curu])
				continue;
			for (Edge e : edges[curu]) {
				int v = e.t;
				int nprio = prio[curu] + e.cost;
				if (prio[v] > nprio) {
					prio[v] = nprio;
					pred[v] = curu;
					q.add(((long) nprio << 32) + v);
				}
			}
		} long cur = q.remove(); cur q.remove() q.remove q int curu = (int) cur; curu (int) cur if (cur >>> 32 != prio[curu])
				continue; cur >>> 32 != prio[curu] cur >>> 32 cur 32 prio[curu] prio curu continue; for (Edge e : edges[curu]) {
				int v = e.t;
				int nprio = prio[curu] + e.cost;
				if (prio[v] > nprio) {
					prio[v] = nprio;
					pred[v] = curu;
					q.add(((long) nprio << 32) + v);
				}
			} Edge e edges[curu] edges curu {
				int v = e.t;
				int nprio = prio[curu] + e.cost;
				if (prio[v] > nprio) {
					prio[v] = nprio;
					pred[v] = curu;
					q.add(((long) nprio << 32) + v);
				}
			} {
				int v = e.t;
				int nprio = prio[curu] + e.cost;
				if (prio[v] > nprio) {
					prio[v] = nprio;
					pred[v] = curu;
					q.add(((long) nprio << 32) + v);
				}
			} int v = e.t; v e.t e e.t int nprio = prio[curu] + e.cost; nprio prio[curu] + e.cost prio[curu] prio curu e.cost e e.cost if (prio[v] > nprio) {
					prio[v] = nprio;
					pred[v] = curu;
					q.add(((long) nprio << 32) + v);
				} prio[v] > nprio prio[v] prio v nprio {
					prio[v] = nprio;
					pred[v] = curu;
					q.add(((long) nprio << 32) + v);
				} prio[v] = nprio prio[v] prio v nprio pred[v] = curu pred[v] pred v curu q.add(((long) nprio << 32) + v) q.add q ((long) nprio << 32) + v ((long) nprio << 32) (long) nprio 32 v List<Edge>[] edges edges int s s int[] prio prio int[] pred pred static class Node implements Comparable<Node> {
		int num, freq, idx;

		public Node(int u, int v, int idx) {
			this.num = u;
			this.freq = v;
			this.idx = idx;
		}

		public int compareTo(Node n) {
			if (this.freq == n.freq)
				return Integer.compare(this.num, n.num);
			return Integer.compare(-this.freq, -n.freq);
		}
	} Node int num num freq freq idx; idx public Node(int u, int v, int idx) {
			this.num = u;
			this.freq = v;
			this.idx = idx;
		} Node {
			this.num = u;
			this.freq = v;
			this.idx = idx;
		} this.num = u this.num this this.num u this.freq = v this.freq this this.freq v this.idx = idx this.idx this this.idx idx int u u int v v int idx idx public int compareTo(Node n) {
			if (this.freq == n.freq)
				return Integer.compare(this.num, n.num);
			return Integer.compare(-this.freq, -n.freq);
		} compareTo {
			if (this.freq == n.freq)
				return Integer.compare(this.num, n.num);
			return Integer.compare(-this.freq, -n.freq);
		} if (this.freq == n.freq)
				return Integer.compare(this.num, n.num); this.freq == n.freq this.freq this this.freq n.freq n n.freq return Integer.compare(this.num, n.num); Integer.compare(this.num, n.num) Integer.compare Integer this.num this this.num n.num n n.num return Integer.compare(-this.freq, -n.freq); Integer.compare(-this.freq, -n.freq) Integer.compare Integer -this.freq this.freq this this.freq -n.freq n.freq n n.freq Node n n Comparable static class Edge {
		int t, cost;

		public Edge(int t, int cost) {
			this.t = t;
			this.cost = cost;
		}
	} Edge int t t cost; cost public Edge(int t, int cost) {
			this.t = t;
			this.cost = cost;
		} Edge {
			this.t = t;
			this.cost = cost;
		} this.t = t this.t this this.t t this.cost = cost this.cost this this.cost cost int t t int cost cost static class InputReader {

		private InputStream stream;
		private byte[] buf = new byte[8192];
		private int curChar, snumChars;
		private SpaceCharFilter filter;

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

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

		public int nextInt() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public long nextLong() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

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

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

		public boolean isSpaceChar(int c) {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
	} InputReader private InputStream stream; stream private byte[] buf = new byte[8192]; buf new byte[8192] 8192 private int curChar curChar snumChars; snumChars private SpaceCharFilter filter; filter public InputReader(InputStream stream) {
			this.stream = stream;
		} InputReader {
			this.stream = stream;
		} this.stream = stream this.stream this this.stream stream InputStream stream stream public int snext() {
			if (snumChars == -1)
				throw new InputMismatchException();
			if (curChar >= snumChars) {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (snumChars <= 0)
					return -1;
			}
			return buf[curChar++];
		} snext {
			if (snumChars == -1)
				throw new InputMismatchException();
			if (curChar >= snumChars) {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (snumChars <= 0)
					return -1;
			}
			return buf[curChar++];
		} if (snumChars == -1)
				throw new InputMismatchException(); snumChars == -1 snumChars -1 1 throw new InputMismatchException(); new InputMismatchException() if (curChar >= snumChars) {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (snumChars <= 0)
					return -1;
			} curChar >= snumChars curChar snumChars {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (snumChars <= 0)
					return -1;
			} curChar = 0 curChar 0 try {
					snumChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				} catch (IOException e) {
					throw new InputMismatchException();
				} IOException e {
					throw new InputMismatchException();
				} throw new InputMismatchException(); new InputMismatchException() {
					snumChars = stream.read(buf);
				} snumChars = stream.read(buf) snumChars stream.read(buf) stream.read stream buf if (snumChars <= 0)
					return -1; snumChars <= 0 snumChars 0 return -1; -1 1 return buf[curChar++]; buf[curChar++] buf curChar++ curChar public int nextInt() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		} nextInt {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		} int c = snext(); c snext() snext while (isSpaceChar(c))
				c = snext(); isSpaceChar(c) isSpaceChar c c = snext() c snext() snext int sgn = 1; sgn 1 if (c == '-') {
				sgn = -1;
				c = snext();
			} c == '-' c '-' {
				sgn = -1;
				c = snext();
			} sgn = -1 sgn -1 1 c = snext() c snext() snext int res = 0; res 0 do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c)); !isSpaceChar(c) isSpaceChar(c) isSpaceChar c {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} if (c < '0' || c > '9')
					throw new InputMismatchException(); c < '0' || c > '9' c < '0' c '0' c > '9' c '9' throw new InputMismatchException(); new InputMismatchException() res *= 10 res 10 res += c - '0' res c - '0' c '0' c = snext() c snext() snext return res * sgn; res * sgn res sgn public long nextLong() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		} nextLong {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		} int c = snext(); c snext() snext while (isSpaceChar(c))
				c = snext(); isSpaceChar(c) isSpaceChar c c = snext() c snext() snext int sgn = 1; sgn 1 if (c == '-') {
				sgn = -1;
				c = snext();
			} c == '-' c '-' {
				sgn = -1;
				c = snext();
			} sgn = -1 sgn -1 1 c = snext() c snext() snext long res = 0; res 0 do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c)); !isSpaceChar(c) isSpaceChar(c) isSpaceChar c {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} if (c < '0' || c > '9')
					throw new InputMismatchException(); c < '0' || c > '9' c < '0' c '0' c > '9' c '9' throw new InputMismatchException(); new InputMismatchException() res *= 10 res 10 res += c - '0' res c - '0' c '0' c = snext() c snext() snext return res * sgn; res * sgn res sgn public int[] nextIntArray(int n) {
			int a[] = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		} nextIntArray {
			int a[] = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		} int a[] = new int[n]; a new int[n] n for (int i = 0; i < n; i++)
				a[i] = nextInt(); int i = 0; int i = 0; i 0 i < n i n i++ i++ i a[i] = nextInt(); a[i] = nextInt() a[i] a i nextInt() nextInt return a; a int n n public String readString() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = snext();
			} while (!isSpaceChar(c));
			return res.toString();
		} readString {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = snext();
			} while (!isSpaceChar(c));
			return res.toString();
		} int c = snext(); c snext() snext while (isSpaceChar(c))
				c = snext(); isSpaceChar(c) isSpaceChar c c = snext() c snext() snext StringBuilder res = new StringBuilder(); res new StringBuilder() do {
				res.appendCodePoint(c);
				c = snext();
			} while (!isSpaceChar(c)); !isSpaceChar(c) isSpaceChar(c) isSpaceChar c {
				res.appendCodePoint(c);
				c = snext();
			} res.appendCodePoint(c) res.appendCodePoint res c c = snext() c snext() snext return res.toString(); res.toString() res.toString res public boolean isSpaceChar(int c) {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		} isSpaceChar {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		} if (filter != null)
				return filter.isSpaceChar(c); filter != null filter null return filter.isSpaceChar(c); filter.isSpaceChar(c) filter.isSpaceChar filter c return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1 c == '\t' c == '\r' c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1 c == ' ' c ' ' c == '\n' c '\n' c == '\r' c '\r' c == '\t' c '\t' c == -1 c -1 1 int c c public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		} SpaceCharFilter public boolean isSpaceChar(int ch); isSpaceChar int ch ch public class Main {
	static int MAXN = 1001, mod = (int) (1e9 + 7);
	static long comb[][];

	public static void main(String[] args) {
		InputReader s = new InputReader(System.in);
		PrintWriter out = new PrintWriter(System.out);
		/*My code starts here.*/
		int n = s.nextInt();
		boolean arr[] = new boolean[n+1];
		int inp[] = new int[n+1];
		for(int i=1;i<=n;i++)
			inp[i] = s.nextInt();
		int flg=0,count=0;
		int i=1;
		while(true){
			int k = inp[i];
			if(arr[k]){
				flg = -1;
				break;
			}
			arr[i] = true;
			count++;
			if(inp[i]==2){
				break;
			}
			i = inp[i];
		}
		if(flg==-1)
			out.println("-1");
		else
			out.println(count);
		/*My code ends here.*/
		out.close();
	}

	static class Node implements Comparable<Node> {
		int num, freq, idx;

		public Node(int u, int v, int idx) {
			this.num = u;
			this.freq = v;
			this.idx = idx;
		}

		public int compareTo(Node n) {
			if (this.freq == n.freq)
				return Integer.compare(this.num, n.num);
			return Integer.compare(-this.freq, -n.freq);
		}
	}

	static void preCalcComb() {
		comb = new long[MAXN][MAXN];
		comb[0][0] = 1;
		for (int i = 1; i < MAXN; i++) {
			comb[i][0] = 1;
			for (int j = 1; j <= i; j++) {
				comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;
			}
		}
	}

	static long PowerMod(long a, long b, long m) {
		long tempo;
		if (b == 0)
			tempo = 1;
		else if (b == 1)
			tempo = a;
		else {
			long temp = PowerMod(a, b / 2, m);
			if (b % 2 == 0)
				tempo = (temp * temp) % m;
			else
				tempo = ((temp * temp) % m) * a % m;
		}
		return tempo;
	}

	long no_of_primes(long m, long n, long k) {
		long count = 0, i, j;
		int primes[] = new int[(int) (n - m + 2)];
		if (m == 1)
			primes[0] = 1;
		for (i = 2; i <= Math.sqrt(n); i++) {
			j = (m / i);
			j *= i;
			if (j < m)
				j += i;
			for (; j <= n; j += i) {
				if (j != i)
					primes[(int) (j - m)] = 1;
			}
		}
		for (i = 0; i <= n - m; i++)
			if (primes[(int) i] == 0 && (i - 1) % k == 0)
				count++;
		return count;
	}

	static void shortestPaths(List<Edge>[] edges, int s, int[] prio, int[] pred) {
		Arrays.fill(pred, -1);
		Arrays.fill(prio, Integer.MAX_VALUE);
		prio[s] = 0;
		PriorityQueue<Long> q = new PriorityQueue<>();
		q.add((long) s);
		while (!q.isEmpty()) {
			long cur = q.remove();
			int curu = (int) cur;
			if (cur >>> 32 != prio[curu])
				continue;
			for (Edge e : edges[curu]) {
				int v = e.t;
				int nprio = prio[curu] + e.cost;
				if (prio[v] > nprio) {
					prio[v] = nprio;
					pred[v] = curu;
					q.add(((long) nprio << 32) + v);
				}
			}
		}
	}

	static class Edge {
		int t, cost;

		public Edge(int t, int cost) {
			this.t = t;
			this.cost = cost;
		}
	}

	static class InputReader {

		private InputStream stream;
		private byte[] buf = new byte[8192];
		private int curChar, snumChars;
		private SpaceCharFilter filter;

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

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

		public int nextInt() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public long nextLong() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

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

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

		public boolean isSpaceChar(int c) {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
	}
} public class Main {
	static int MAXN = 1001, mod = (int) (1e9 + 7);
	static long comb[][];

	public static void main(String[] args) {
		InputReader s = new InputReader(System.in);
		PrintWriter out = new PrintWriter(System.out);
		/*My code starts here.*/
		int n = s.nextInt();
		boolean arr[] = new boolean[n+1];
		int inp[] = new int[n+1];
		for(int i=1;i<=n;i++)
			inp[i] = s.nextInt();
		int flg=0,count=0;
		int i=1;
		while(true){
			int k = inp[i];
			if(arr[k]){
				flg = -1;
				break;
			}
			arr[i] = true;
			count++;
			if(inp[i]==2){
				break;
			}
			i = inp[i];
		}
		if(flg==-1)
			out.println("-1");
		else
			out.println(count);
		/*My code ends here.*/
		out.close();
	}

	static class Node implements Comparable<Node> {
		int num, freq, idx;

		public Node(int u, int v, int idx) {
			this.num = u;
			this.freq = v;
			this.idx = idx;
		}

		public int compareTo(Node n) {
			if (this.freq == n.freq)
				return Integer.compare(this.num, n.num);
			return Integer.compare(-this.freq, -n.freq);
		}
	}

	static void preCalcComb() {
		comb = new long[MAXN][MAXN];
		comb[0][0] = 1;
		for (int i = 1; i < MAXN; i++) {
			comb[i][0] = 1;
			for (int j = 1; j <= i; j++) {
				comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;
			}
		}
	}

	static long PowerMod(long a, long b, long m) {
		long tempo;
		if (b == 0)
			tempo = 1;
		else if (b == 1)
			tempo = a;
		else {
			long temp = PowerMod(a, b / 2, m);
			if (b % 2 == 0)
				tempo = (temp * temp) % m;
			else
				tempo = ((temp * temp) % m) * a % m;
		}
		return tempo;
	}

	long no_of_primes(long m, long n, long k) {
		long count = 0, i, j;
		int primes[] = new int[(int) (n - m + 2)];
		if (m == 1)
			primes[0] = 1;
		for (i = 2; i <= Math.sqrt(n); i++) {
			j = (m / i);
			j *= i;
			if (j < m)
				j += i;
			for (; j <= n; j += i) {
				if (j != i)
					primes[(int) (j - m)] = 1;
			}
		}
		for (i = 0; i <= n - m; i++)
			if (primes[(int) i] == 0 && (i - 1) % k == 0)
				count++;
		return count;
	}

	static void shortestPaths(List<Edge>[] edges, int s, int[] prio, int[] pred) {
		Arrays.fill(pred, -1);
		Arrays.fill(prio, Integer.MAX_VALUE);
		prio[s] = 0;
		PriorityQueue<Long> q = new PriorityQueue<>();
		q.add((long) s);
		while (!q.isEmpty()) {
			long cur = q.remove();
			int curu = (int) cur;
			if (cur >>> 32 != prio[curu])
				continue;
			for (Edge e : edges[curu]) {
				int v = e.t;
				int nprio = prio[curu] + e.cost;
				if (prio[v] > nprio) {
					prio[v] = nprio;
					pred[v] = curu;
					q.add(((long) nprio << 32) + v);
				}
			}
		}
	}

	static class Edge {
		int t, cost;

		public Edge(int t, int cost) {
			this.t = t;
			this.cost = cost;
		}
	}

	static class InputReader {

		private InputStream stream;
		private byte[] buf = new byte[8192];
		private int curChar, snumChars;
		private SpaceCharFilter filter;

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

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

		public int nextInt() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public long nextLong() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

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

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

		public boolean isSpaceChar(int c) {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
	}
} Main

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

public class Main {
	static int MAXN = 1001, mod = (int) (1e9 + 7);
	static long comb[][];

	public static void main(String[] args) {
		InputReader s = new InputReader(System.in);
		PrintWriter out = new PrintWriter(System.out);
		/*My code starts here.*/
		int n = s.nextInt();
		boolean arr[] = new boolean[n+1];
		int inp[] = new int[n+1];
		for(int i=1;i<=n;i++)
			inp[i] = s.nextInt();
		int flg=0,count=0;
		int i=1;
		while(true){
			int k = inp[i];
			if(arr[k]){
				flg = -1;
				break;
			}
			arr[i] = true;
			count++;
			if(inp[i]==2){
				break;
			}
			i = inp[i];
		}
		if(flg==-1)
			out.println("-1");
		else
			out.println(count);
		/*My code ends here.*/
		out.close();
	}

	static class Node implements Comparable<Node> {
		int num, freq, idx;

		public Node(int u, int v, int idx) {
			this.num = u;
			this.freq = v;
			this.idx = idx;
		}

		public int compareTo(Node n) {
			if (this.freq == n.freq)
				return Integer.compare(this.num, n.num);
			return Integer.compare(-this.freq, -n.freq);
		}
	}

	static void preCalcComb() {
		comb = new long[MAXN][MAXN];
		comb[0][0] = 1;
		for (int i = 1; i < MAXN; i++) {
			comb[i][0] = 1;
			for (int j = 1; j <= i; j++) {
				comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;
			}
		}
	}

	static long PowerMod(long a, long b, long m) {
		long tempo;
		if (b == 0)
			tempo = 1;
		else if (b == 1)
			tempo = a;
		else {
			long temp = PowerMod(a, b / 2, m);
			if (b % 2 == 0)
				tempo = (temp * temp) % m;
			else
				tempo = ((temp * temp) % m) * a % m;
		}
		return tempo;
	}

	long no_of_primes(long m, long n, long k) {
		long count = 0, i, j;
		int primes[] = new int[(int) (n - m + 2)];
		if (m == 1)
			primes[0] = 1;
		for (i = 2; i <= Math.sqrt(n); i++) {
			j = (m / i);
			j *= i;
			if (j < m)
				j += i;
			for (; j <= n; j += i) {
				if (j != i)
					primes[(int) (j - m)] = 1;
			}
		}
		for (i = 0; i <= n - m; i++)
			if (primes[(int) i] == 0 && (i - 1) % k == 0)
				count++;
		return count;
	}

	static void shortestPaths(List<Edge>[] edges, int s, int[] prio, int[] pred) {
		Arrays.fill(pred, -1);
		Arrays.fill(prio, Integer.MAX_VALUE);
		prio[s] = 0;
		PriorityQueue<Long> q = new PriorityQueue<>();
		q.add((long) s);
		while (!q.isEmpty()) {
			long cur = q.remove();
			int curu = (int) cur;
			if (cur >>> 32 != prio[curu])
				continue;
			for (Edge e : edges[curu]) {
				int v = e.t;
				int nprio = prio[curu] + e.cost;
				if (prio[v] > nprio) {
					prio[v] = nprio;
					pred[v] = curu;
					q.add(((long) nprio << 32) + v);
				}
			}
		}
	}

	static class Edge {
		int t, cost;

		public Edge(int t, int cost) {
			this.t = t;
			this.cost = cost;
		}
	}

	static class InputReader {

		private InputStream stream;
		private byte[] buf = new byte[8192];
		private int curChar, snumChars;
		private SpaceCharFilter filter;

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

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

		public int nextInt() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public long nextLong() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

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

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

		public boolean isSpaceChar(int c) {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
	}
}
