#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 7;
list<long long> adj[N];
long long n, cnt;
bool visited[N], b[N];
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long lcm(long long a, long long b) {
  long long ans = a / gcd(a, b);
  ans *= b;
  return ans;
}
void dfs(long long v) {
  visited[v] = 1;
  cnt++;
  for (auto i : adj[v])
    if (!visited[i]) dfs(i);
}
void func() {
  cin >> n;
  long long u;
  for (long long i = 1; i < n + 1; i++) {
    cin >> u;
    adj[i].push_back(u);
    b[u] = 1;
  }
  for (long long i = 1; i < n + 1; i++) {
    if (b[i] == 0) {
      cout << "-1\n";
      return;
    }
  }
  long long ans = 1;
  for (long long i = 1; i < n + 1; i++) {
    if (!visited[i]) {
      cnt = 0;
      dfs(i);
      if (cnt % 2 != 0)
        ans = lcm(ans, cnt);
      else
        ans = lcm(ans, cnt / 2);
    }
  }
  cout << ans << endl;
}
int main() {
  {
    ios ::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
  long long q = 1;
  while (q) {
    func();
    q--;
  }
  return 0;
}
