#include <bits/stdc++.h>
using namespace std;
template <class T>
bool umin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool umax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
int a, b, c, d[100007], n, m, rr[100007 * 2], ans;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> d[i];
  a = 0, b = 1000000009;
  for (int i = 1; i <= n; i++) {
    int x1 = a, x2 = b;
    a = max(a, d[i]);
    b = min(b, d[i]);
    if (a - b > 1) {
      if (x1 != a) {
        ans = max(ans, i - c - 1);
        c = rr[b];
        b = d[rr[b] + 1];
      } else if (x2 != b) {
        ans = max(ans, i - c - 1);
        c = rr[a];
        a = d[rr[a] + 1];
      }
    }
    rr[d[i]] = i;
  }
  if (a - b < 2) ans = max(ans, n - c);
  cout << ans << endl;
  return 0;
}
