#include <bits/stdc++.h>
using namespace std;
int n;
int a[1000005];
deque<int> q1, q2;
inline bool Ok(int x) {
  int i;
  q1.clear();
  q2.clear();
  for (i = 1; i <= n; ++i) {
    while (!q1.empty() && q1.front() <= i - x) q1.pop_front();
    while (!q2.empty() && q2.front() <= i - x) q2.pop_front();
    while (!q1.empty() && a[q1.back()] >= a[i]) q1.pop_back();
    q1.push_back(i);
    while (!q2.empty() && a[q2.back()] <= a[i]) q2.pop_back();
    q2.push_back(i);
    if (i >= x && a[q2.front()] - a[q1.front()] <= 1) return true;
  }
  return false;
}
int main() {
  int i, j;
  cin.sync_with_stdio(0);
  cin >> n;
  for (i = 1; i <= n; ++i) cin >> a[i];
  int st = 0, dr = n, mij, sol;
  while (st <= dr) {
    mij = ((st + dr) >> 1);
    if (Ok(mij)) {
      sol = mij;
      st = mij + 1;
    } else
      dr = mij - 1;
  }
  cout << sol;
  return 0;
}
