#include <bits/stdc++.h>
using namespace std;
const long long N = 1e3 + 10;
long long n, l, r;
long long a[N];
void solve() {
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  for (long long i = 1; i <= n; i++) {
    if (a[i] == i)
      continue;
    else {
      long long x = a[i];
      if (a[x] == i) {
        if (l == 0) l = min(i, x);
        r = max(i, x);
      } else {
        cout << 0 << " " << 0 << endl;
        exit(0);
      }
    }
  }
  for (long long i = l + 1; i <= r; i++) {
    if (a[i] < a[i - 1])
      continue;
    else {
      cout << 0 << " " << 0 << endl;
      exit(0);
    }
  }
  cout << l << " " << r;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
