#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int l = -1, r = -1;
  for (int i = 0; i < n; ++i) {
    if (a[i] == i + 1)
      continue;
    else {
      l = i;
      break;
    }
  }
  for (int i = n - 1; i > 0; --i) {
    if (i + 1 == a[i])
      continue;
    else {
      r = i;
      break;
    }
  }
  if (l == -1) {
    cout << "0 0" << endl;
    return 0;
  }
  for (int i = l; i < r; ++i) {
    if (a[i] != a[i + 1] + 1) {
      cout << "0 0" << endl;
      return 0;
    }
  }
  cout << l + 1 << " " << r + 1 << endl;
}
