Problem:
Score : 300 points

Problem Statement

Given are an integer X and an integer sequence of length N: p_1, \ldots, p_N.

Among the integers not contained in the sequence p_1, \ldots, p_N (not necessarily positive), find the integer nearest to X, that is, find the integer whose absolute difference with X is the minimum. If there are multiple such integers, report the smallest such integer.

Constraints

1 \leq X \leq 100

0 \leq N \leq 100

1 \leq p_i \leq 100

p_1, \ldots, p_N are all distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X N
p_1 ... p_N

Output

Print the answer.

Sample Input 1

6 5
4 7 10 6 5

Sample Output 1

8

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the one nearest to 6 is 8.

Sample Input 2

10 5
4 7 10 6 5

Sample Output 2

9

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the ones nearest to 10 are 9 and 11. We should print the smaller one, 9.

Sample Input 3

100 0

Sample Output 3

100

When N = 0, the second line in the input will be empty. Also, as seen here, X itself can be the answer.

Solution in Rust (1.42.0) (judge verdict: Runtime Error)

fn main() {
    let input: Vec<i32> = read_vec();
    let x = input[0];
    let p: Vec<i32> = read_vec();

    println!("{}", solve(&p, x));
}

fn read<T: std::str::FromStr>() -> T {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).ok();
    buf.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|s| s.parse().ok().unwrap())
        .collect()
}

fn solve(p: &[i32], x: i32) -> i32 {
    let mut new_p = Vec::from(p);
    new_p.sort();

    if let Some(i) = new_p.iter().position(|n| n == &x) {
        for j in 1_usize.. {
            if i < j {
                // 先に先頭に到達
                let h = new_p[0];
                return if new_p[1] != h + 1 { h + 1 } else { h - 1 };
            }
            if i + j >= new_p.len() {
                // 先に末尾に到達
                let l = new_p.last().unwrap();
                return if new_p[i + j - 2] != l - 1 {
                    l - 1
                } else {
                    l + 1
                };
            }

            // まだ数列の中にいる
            if new_p[i - j] != x && new_p[i - j] != x - j as i32 {
                // 先頭側が空いてる
                return new_p[i - j + 1] - 1;
            }

            if new_p[i + j] != x && new_p[i + j] != x + j as i32 {
                // 末尾側が空いている
                return new_p[i + j - 1] + 1;
            }
        }
    }

    x
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_solve_1() {
        let input = &[4, 7, 10, 6, 5];
        assert_eq!(8, solve(input, 6));
    }

    #[test]
    fn test_solve_2() {
        let input = &[4, 7, 10, 6, 5];
        assert_eq!(9, solve(input, 10));
    }

    #[test]
    fn test_solve_3() {
        let input = &[];
        assert_eq!(100, solve(input, 100));
    }
}