#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  int fh = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') fh = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= fh;
}
template <typename T>
void write(T x) {
  if (x < 0) x = -x, putchar('-');
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
void writeln(T x) {
  write(x);
  puts("");
}
int n, a[100005];
bool check(int mid) {
  deque<int> mx, mn;
  for (int i = 1; i <= n; i++) {
    if (!mx.empty() && mx.front() + mid - 1 < i) mx.pop_front();
    if (!mn.empty() && mn.front() + mid - 1 < i) mn.pop_front();
    while (!mx.empty() && a[mx.back()] <= a[i]) mx.pop_back();
    while (!mn.empty() && a[mn.back()] >= a[i]) mn.pop_back();
    mx.push_back(i);
    mn.push_back(i);
    if (i >= mid && a[mx.front()] - a[mn.front()] <= 1) return true;
  }
  return false;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r + 1) / 2;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  cout << l << endl;
  return 0;
}
