#include <bits/stdc++.h>
using namespace std;
const bool print = false;
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 const N = 1e5 + 9;
int n, m, k;
int a[1000009], b[1000009], c[1000009];
int solve() {
  scanf("%d", &n);
  int ans = 0, cnt = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    if (i > 1) {
      if (a[i] < a[i - 1])
        cnt = min(i - b[a[i] + 2], i - b[a[i] - 1]);
      else if (a[i] > a[i - 1])
        cnt = min(i - b[a[i] - 2], i - b[a[i] + 1]);
      else
        cnt++;
    } else
      cnt = 1;
    b[a[i]] = i;
    cerr << cnt << ' ';
    umax(ans, cnt);
  }
  printf("%d\n", ans);
  return 0;
}
void preprocess() {}
int main() {
  preprocess();
  int t = 1;
  while (t--) {
    solve();
  }
}
