#[allow(unused_imports)]
use std::cmp::{min, max};

#[allow(dead_code)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(dead_code)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(dead_code)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input! {
        h: i64,
        n: usize,
        mut magic: [(i64, i64); n],
    }

    magic.sort_by(|&(la, lb), &(ra, rb)| {
        use std::cmp::Ordering::*;
        let lv = (la as f64) / (lb as f64);
        let rv = (ra as f64) / (rb as f64);
        // ダメージ効率が高い順
        match rv.partial_cmp(&lv).expect("error: l: ({}, {}), r: ({}, {})") {
            Equal => {
                // ダメージ効率が同じなら、コストが低い順
                rb.cmp(&lb)
            },
            others => others
        }
    });
    // println!("ordered magic: {:?}", magic);

    // // 動的計画法前にボコボコにする（いらないきがする）
    // let max_power = magic.iter().map(|&(dmg, _cost)| dmg).max().unwrap();
    // println!("over");
    // let (dmg1, mp1) = magic[0];
    // let k = max((h - max_power) / dmg1, 0);
    // let used = (mp1 * k) as i64;
    // h -= dmg1 * k;
    // println!("k: {}, used: {}, h: {}", k, used, h);

    // DP
    // とりあえずでかい数
    let mut dp = vec![1_000_000_000; 1 + h as usize];
    // ダメージ0でいいならMP0
    dp[0] = 0;
    for v in 1..(h + 1) {
        // let mut mv = vec![1_000_000_000; n + 2];
        let mut min_mp = 1_000_000_000;
        // i番目の魔法を使うときの最小MPをresultにもとめて、それがvダメージ与える最小MPならmin_mpを更新する
        for i in 0..n {
            let current_magic = magic[i];
            let (cma, cmb) = (current_magic.0, current_magic.1);
            // let before = mv[i];
            // let mut vmut = v;
            // let mut mp = 0;
            let others = if v > cma {
                dp[(v - cma) as usize]
            } else {
                0
            };
            min_mp = min(min_mp, cmb + others);
        }
        // let result = mv.into_iter().min().unwrap();
        dp[v as usize] = min_mp;
    }

    // DEBUG
    // for (i, &d) in dp.iter().enumerate() {
    //     println!("i: {:3}, d: {:6}", i, d);
    // }
    // DEBUG END

    // println!("{}", used + dp[h as usize]);
    println!("{}", dp[h as usize]);
}


Measured memory_bytes: 4352