#include <bits/stdc++.h>
using namespace std;
const int maxn = 6e5 + 7;
int tree[maxn];
int tree1[maxn];
int lazy1[maxn];
int lazy[maxn];
void down(int p, int left, int right) {
  lazy[2 * p + 1] += lazy[p];
  lazy[2 * p] += lazy[p];
  tree[2 * p] += (lazy[p]);
  tree[2 * p + 1] += lazy[p];
  lazy[p] = 0;
  lazy1[2 * p + 1] += lazy1[p];
  lazy1[2 * p] += lazy1[p];
  tree1[2 * p] += (lazy1[p]);
  tree1[2 * p + 1] += lazy1[p];
  lazy1[p] = 0;
}
void up(int p) {
  tree[p] = max(tree[2 * p], tree[2 * p + 1]);
  tree1[p] = min(tree1[2 * p], tree1[2 * p + 1]);
}
void build(int p, int left, int right) {
  lazy[p] = 0;
  lazy1[p] = 0;
  if (left == right) {
    scanf("%d", &tree[p]);
    tree1[p] = tree[p];
    return;
  }
  int mid = (left + right) / 2;
  build(2 * p, left, mid);
  build(2 * p + 1, mid + 1, right);
  up(p);
}
int find_max(int p, int left, int right, int l, int r) {
  if (l <= left && r >= right) return tree[p];
  int mid = (left + right) / 2;
  int ans = 0;
  down(p, left, right);
  if (l <= mid) ans = max(ans, find_max(2 * p, left, mid, l, r));
  if (r > mid) ans = max(ans, find_max(2 * p + 1, mid + 1, right, l, r));
  return ans;
  up(p);
}
int find_min(int p, int left, int right, int l, int r) {
  if (l <= left && r >= right) return tree1[p];
  int mid = (left + right) / 2;
  int ans = 1222220;
  down(p, left, right);
  if (l <= mid) ans = min(ans, find_min(2 * p, left, mid, l, r));
  if (r > mid) ans = min(ans, find_min(2 * p + 1, mid + 1, right, l, r));
  return ans;
  up(p);
}
int a, b, c, d;
int main() {
  scanf("%d", &a);
  build(1, 1, a);
  int left, right;
  int len = 0;
  int ma, mi;
  int ans = 0;
  int j = 1;
  for (int i = 1; i <= a; i++) {
    int ma = find_max(1, 1, a, j, i);
    int mi = find_min(1, 1, a, j, i);
    if (ma - mi <= 1) {
      len = i - j + 1;
      ans = max(ans, len);
    } else {
      ans = max(ans, len);
      while (1) {
        j++;
        ma = find_max(1, 1, a, i, j);
        mi = find_min(1, 1, a, i, j);
        if (ma - mi > 1) {
          ;
        } else {
          len = i - j + 1;
          break;
        }
      }
    }
    ans = max(ans, len);
  }
  cout << ans << endl;
  return 0;
}
