using System;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using Debug = System.Diagnostics.Debug;
using StringBuilder = System.Text.StringBuilder;
using System.Numerics;
namespace Program
{
///
/// 嘘貪欲(落としたい)
///
public class Solver
{
public void Solve()
{
var n = sc.Integer();
var a = sc.Integer(n);
var dec = new PriorityQueue>();
var inc = new PriorityQueue>();
inc.Enqueue(new Triplet(0, 1 << 30, -100000));
dec.Enqueue(new Triplet(0, -100000, 1 << 30));
foreach (var x in a)
{
var push_dec = new List>();
var push_inc = new List>();
for (int i = 0; i < 25 && dec.Any(); i++)
{
var p = dec.Dequeue();
if (p.J < x && p.K != x)
push_inc.Add(Triplet.Create(p.I + 1, x, p.J));
push_dec.Add(p);
}
for (int i = 0; i < 25 && inc.Any(); i++)
{
var p = inc.Dequeue();
if (p.J > x && p.K != x)
push_dec.Add(Triplet.Create(p.I + 1, x, p.J));
push_inc.Add(p);
}
foreach (var i in push_inc.Distinct())
inc.Enqueue(i);
foreach (var d in push_dec.Distinct())
dec.Enqueue(d);
while (inc.Count > 25) inc.Dequeue();
while (dec.Count > 25) dec.Dequeue();
}
var ans = 0;
while (inc.Any()) ans = Math.Max(ans, inc.Dequeue().I);
while (dec.Any()) ans = Math.Max(ans, dec.Dequeue().I);
if (ans < 3) ans = 0;
IO.Printer.Out.WriteLine(ans);
}
public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput());
static T[] Enumerate(int n, Func f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(i); return a; }
static public void Swap(ref T a, ref T b) { var tmp = a; a = b; b = tmp; }
}
}
#region main
static class Ex
{
static public string AsString(this IEnumerable ie) { return new string(System.Linq.Enumerable.ToArray(ie)); }
static public string AsJoinedString(this IEnumerable ie, string st = " ") { return string.Join(st, ie); }
static public void Main()
{
var solver = new Program.Solver();
solver.Solve();
Program.IO.Printer.Out.Flush();
}
}
#endregion
#region Ex
namespace Program.IO
{
using System.IO;
using System.Text;
using System.Globalization;
public class Printer : StreamWriter
{
static Printer() { Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false }; }
public static Printer Out { get; set; }
public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { }
public void Write(string format, T[] source) { base.Write(format, source.OfType