#include <bits/stdc++.h>
using namespace std;
const double pi =
    3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;
const int N = 110;
int d[N], cr[N];
vector<int> V;
int done[N];
bool bad;
void dfs(int nr) {
  done[nr] = 1;
  V.push_back(nr);
  if (!done[cr[nr]])
    dfs(cr[nr]);
  else if (done[cr[nr]] == 1) {
    if (cr[nr] != V.front())
      bad = 1;
    else {
      for (int i = 0; i < (V.size()); ++i) d[V[i]] = V.size();
    }
  } else
    bad = 1;
  V.pop_back();
  done[nr] = 2;
}
long long nwd(long long a, long long b) { return b ? nwd(b, a % b) : a; }
long long nww(long long a, long long b) { return a * b / nwd(a, b); }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < (n); ++i) {
    scanf("%d", &cr[i]);
    cr[i]--;
  }
  for (int i = 0; i < (n); ++i)
    if (!done[i]) dfs(i);
  if (bad) {
    printf("-1\n");
    return 0;
  }
  long long res = 1;
  for (int i = 0; i < (n); ++i) res = nww(res, d[i] % 2 ? d[i] : d[i] / 2);
  printf("%lld\n", res);
  return 0;
}
