use std::{
    collections::HashMap,
    io::{read_to_string, stdin},
};

fn main() {
    let temp = read_to_string(stdin()).unwrap();
    let mut iter = temp
        .split_ascii_whitespace()
        .map(|x| x.parse::<usize>().unwrap());

    let m = iter.next().unwrap();
    let n = iter.next().unwrap();

    let arr = (0..n).map(|_| iter.next().unwrap()).collect::<Vec<_>>();

    let mut cnts: HashMap<usize, usize> = HashMap::new();
    let mut changes = vec![vec![]; m];
    for &e in arr.iter() {
        let (q, s) = ((e / m), (e % m));

        *cnts.entry(q).or_insert(0) += 1;
        let next_d = s + 1;
        if next_d < m {
            changes[next_d].push(q);
        }
    }

    let mut cnt = cnts.len();
    let mut best_ds = vec![];
    let mut best_cnt = cnt;

    let mut curr_d = 1;
    let mut curr_cnt = cnt;

    let mut changes_sorted = vec![];

    for d in 1..m {
        for &q in &changes[d] {
            changes_sorted.push((d, q));
        }
    }

    changes_sorted.sort_by_key(|&(d, _)| d);

    let mut idx = 0;
    while idx < changes_sorted.len() {
        let d = changes_sorted[idx].0;

        if d > curr_d {
            if curr_cnt > best_cnt {
                best_cnt = curr_cnt;
                best_ds.clear();
                best_ds.push(curr_d);
            } else if curr_cnt == best_cnt {
                best_ds.push(curr_d);
            }
        }

        while idx < changes_sorted.len() && changes_sorted[idx].0 == d {
            let q = changes_sorted[idx].1;
            let cnt_q = cnts.entry(q).or_insert(1);
            *cnt_q -= 1;
            if *cnt_q == 0 {
                cnts.remove(&q);
                curr_cnt -= 1;
            }

            let cnt_qm = cnts.entry(q - 1).or_insert(0);
            if *cnt_qm == 0 {
                curr_cnt += 1;
            }
            *cnt_qm += 1;
            idx += 1;
        }
        curr_d = d;
    }

    if curr_d <= m - 1 {
        if curr_cnt > best_cnt {
            best_cnt = curr_cnt;
            best_ds.clear();
            best_ds.push(curr_d);
        }
    }

    println!("{:?}", best_ds);
}
