use std::str::FromStr;
use std::io::*;
use std::cmp::*;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o);
    stdout().write_all(&o).unwrap();
}

fn run<R: BufRead, W: Write>(i: R, o: &mut W) {
    let mut i = AtRead::from(i);
    let (n, _t) = i.read2::<usize, usize>();
    let a = i.read_vec::<usize>(n);

    writeln!(o, "{}", solve(&a)).unwrap();
}
fn solve(a: &[usize]) -> usize {
    fn cost(ts: &[(usize, usize)], c: &mut [bool]) -> usize {
        if ts.is_empty() {
            return 0;
        }
        let t0 = ts[0];
        let ts = &ts[1..];
        if c[t0.0] || c[t0.1] {
            return cost(ts, c);
        }
        c[t0.0] = true;
        let r0 = cost(ts, c);
        c[t0.0] = false;

        c[t0.1] = true;
        let r1 = cost(ts, c);
        c[t0.1] = false;

        min(r0, r1) + 1
    }
    let mut c = vec![false; a.len()];
    let mut cost_sum = 0;
    for g in grouping(&get_trades(a), a.len()) {
        cost_sum += cost(&g, &mut c);
    }
    cost_sum
}

fn get_trades(a: &[usize]) -> Vec<(usize, usize)> {
    let mut buy_ns = vec![0];
    let mut buy_val = a[0];
    let mut max_gain = 0;
    let mut trades = Vec::new();

    for n in 1..a.len() {
        let val = a[n];
        if val < buy_val {
            buy_ns.clear();
            buy_val = val;
        }
        if val == buy_val {
            buy_ns.push(n);
        }

        let gain = val - buy_val;
        if gain > max_gain {
            trades.clear();
            max_gain = gain;
        }
        if gain == max_gain {
            for &buy_n in &buy_ns {
                trades.push((buy_n, n));
            }
        }
    }
    trades
}
fn grouping(ts: &[(usize, usize)], a_len: usize) -> Vec<Vec<(usize, usize)>> {
    let mut os = vec![Vec::new(); a_len];
    let mut vs = vec![false; a_len];
    for &t in ts {
        os[t.0].push(t.1);
        os[t.1].push(t.0);

        vs[t.0] = true;
        vs[t.1] = true;
    }

    let mut rss = Vec::new();
    let mut q = Vec::new();
    for n0 in 0..a_len {
        if !vs[n0] {
            continue;
        }
        let mut rs = Vec::new();
        q.push(n0);
        vs[n0] = false;

        while let Some(n0) = q.pop() {
            for &n1 in &os[n0] {
                if !vs[n1] {
                    continue;
                }
                q.push(n1);
                vs[n1] = false;
                rs.push((min(n0, n1), max(n0, n1)));
            }
        }
        rss.push(rs);
    }
    rss
}

mod utils {
    use super::*;

    pub struct AtRead<R: BufRead> {
        r: R,
        s: String,
    }

    macro_rules! fn_read {
        {$f:ident($($v:ident: $t:ident),*)} => {
            pub fn $f<$($t: FromStr),*>(&mut self) -> ($($t),*) {
                let i = &mut self.read_line().split(' ');
                $(
                    let $v = next(i);
                )*
                ($($v),*)
            }
        };
    }

    impl<R: BufRead> AtRead<R> {
        pub fn from(r: R) -> Self {
            AtRead {
                r: r,
                s: String::new(),
            }
        }
        pub fn read_line(&mut self) -> &str {
            self.s.clear();
            self.r.read_line(&mut self.s).unwrap();
            self.s.trim()
        }

        fn_read! { read2(v1: T1, v2: T2) }

        pub fn read_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
            self.read_line()
                .split(' ')
                .take(n)
                .map(|x| x.parse().ok().unwrap())
                .collect()
        }
    }

    fn next<'a, T: FromStr, I: std::iter::Iterator<Item = &'a str>>(i: &mut I) -> T {
        i.next().unwrap().parse().ok().unwrap()
    }
}


Measured memory_bytes: 18684