#include <bits/stdc++.h>
using namespace std;
int N;
int C[105];
bool Vis[105];
set<int> Cyc;
int MaxCnt[105];
int main() {
  cin >> N;
  for (int i = 1; i <= N; i++) cin >> C[i];
  for (int Len, Now, i = 1; i <= N; i++) {
    if (Vis[i]) continue;
    Len = 0, Now = i;
    while (!Vis[Now]) {
      Vis[Now] = 1;
      Now = C[Now];
      Len++;
    }
    Cyc.insert(Len);
    if (Now != i) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i : Cyc) {
    for (int j = 2; j * j <= i; j++) {
      int Cnt = 0;
      while (i % j == 0) i /= j, Cnt++;
      MaxCnt[j] = max(Cnt, MaxCnt[j]);
    }
    MaxCnt[i] = max(1, MaxCnt[i]);
  }
  long long Ans = 1LL << max(0, MaxCnt[2] - 1);
  for (int i = 3; i <= 100; i++)
    if (MaxCnt[i]) Ans *= pow(i, MaxCnt[i]);
  cout << Ans << endl;
}
