#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, t, m;
  vector<int> v;
  cin >> n;
  int flag = 0;
  int arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 0; i < n; i++) {
    if (arr[i] != i + 1) {
      flag = 1;
      t = i;
      break;
    }
  }
  if (flag == 0) {
    cout << "0 0";
    return 0;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (arr[i] != i + 1) {
      m = i;
      break;
    }
  }
  for (int i = t; i <= m; i++) v.push_back(arr[i]);
  int count = 0;
  for (int i = 0; i < v.size() - 1; i++) {
    if (v[i] - 1 == v[i + 1]) count++;
  }
  if (count + 1 != v.size()) {
    cout << "0 0";
    return 0;
  }
  flag = 1;
  for (int i = 0; i < n; i++) {
    if (i < t || i > m) {
      if (arr[i] != i + 1) flag = 0;
    }
  }
  if (flag == 0) {
    cout << "0 0";
    return 0;
  } else
    cout << t + 1 << " " << m + 1;
  return 0;
}
