#include <bits/stdc++.h>
using namespace std;
int n, m[100006], t, k;
int main() {
  cin >> n;
  for (int a = 1; a <= n; a++) {
    cin >> t;
    m[++t] = a;
    k = max(k, a - min(max(m[t - 2], m[t + 1]), max(m[t - 1], m[t + 2])));
  }
  cout << k;
}
