#include <bits/stdc++.h>
using namespace std;
int n, k;
int a, la, fa, b, lb, fb, last;
int ma;
int main() {
  int M = 1000007;
  ma = 0;
  cin >> n;
  a = M;
  b = M;
  la = M;
  lb = M;
  fa = M;
  fb = M;
  for (int i = 0; i < n; i++) {
    cin >> k;
    ma = max(ma, i - min(fa, fb));
    if (k == a) {
      lb = M;
      fa = min(fa, i);
      la = min(la, i);
    } else if (k == b) {
      la = M;
      fb = min(fb, i);
      lb = min(lb, i);
    } else if (k == a + 1 || k == a - 1) {
      b = k;
      fb = i;
      lb = i;
      fa = la;
      la = M;
    } else if (k == b + 1 || k == b - 1) {
      a = k;
      fa = i;
      la = i;
      fb = lb;
      lb = M;
    } else {
      a = k;
      fa = i;
      la = i;
      b = M;
      fb = M;
      lb = M;
    }
    last = k;
  }
  ma = max(ma, n - min(fa, fb));
  cout << ma;
}
