Skip to content

Instantly share code, notes, and snippets.

@mi6112ogit
Last active June 25, 2017 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mi6112ogit/f3d83ee288dff9511f06b5efdba0fb0d to your computer and use it in GitHub Desktop.
Save mi6112ogit/f3d83ee288dff9511f06b5efdba0fb0d to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define FORr(i, j, k) for(int i = j; i >= k; --i)
#define repr(i, j) FOR(i, j, 0)
#define INF (1 << 30)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> Pi;
const int MOD = 1e9 + 7;
const int dy[]={0, 0, 1, -1};
const int dx[]={1, -1, 0, 0};
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }
int a[100001];
bool went[100001];
int main() {
int N;
scanf("%d", &N);
rep(i, N) scanf("%d", &a[i]);
int now = 1, cnt = 0;
while(1) {
if(went[now]) {
printf("-1\n");
return 0;
}
if(now == 2) break;
went[now] = true;
now = a[now - 1];
++cnt;
}
printf("%d\n", cnt);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment