type 'a t = Empty | Node of (int * 'a * 'a t * 'a t)

let rec merge q1 q2 =
    match q1, q2 with
    | Empty, _ -> q2
    | _, Empty -> q1
    | Node(n1, v1, l1, r1), Node(n2, v2, l2, r2) ->
        if v1 < v2
        then Node(n1 + n2, v2, q1, merge l2 r2)
        else Node(n1 + n2, v1, merge l1 r1, q2)

let pop q =
    match q with
    | Empty -> failwith "pop"
    | Node(_, _, l, r) ->
        match l, r with
        | Empty, Empty -> Empty
        | Node(_, _, _, _), Empty -> l
        | Node(nl, vl, ll, rl), Node(nr, vr, lr, rr) ->
            if vl < vr
            then Node(nl + nr, vr, l, merge lr rr)
            else Node(nl + nr, vl, merge ll rl, r)
        | _, _ -> failwith "pop"

let rec push x q =
    match q with
    | Empty -> Node(1, x, Empty, Empty)
    | Node(_, v, l, r) ->
        let large = max x v in
        let small = min x v in
        match l, r with
        | Empty, Empty         -> Node(2, large, Node(1, small, Empty, Empty), Empty)
        | Node(n, _, _, _), Empty -> Node(n + 2, large, l, Node(1, small, Empty, Empty))
        | Node(nl, _, _, _), Node(nr, _, _, _) ->
            if nl > nr
            then Node(nl + nr + 2, large, l, push small r)
            else Node(nl + nr + 2, large, push small l, r)
        | _, _ -> failwith "push"

let top q =
    match q with
    | Empty -> failwith "top"
    | Node(_, v, _, _) -> v

let rec insert_while i ws q =
    match ws with
    | w :: ws' when fst w = i ->
        insert_while i ws' (push (snd w) q)
    | _ -> (ws, q)

let rec solve ws m q i sum =
    if i > m then sum
    else
        let (ws', q') = insert_while i ws q in
        if q' = Empty
        then solve ws' m q' (i + 1) sum
        else solve ws' m (pop q') (i + 1) (sum + top q')

let rec input n =
    if n = 0 then []
    else Scanf.scanf "%d %d\n" @@ fun a b ->
        let ws = input (n - 1) in
        (a, b) :: ws

let () =
    Scanf.scanf "%d %d\n" @@ fun n m ->
    let ws = input n in
    let ws' = List.sort (fun a b -> compare (fst a) (fst b)) ws in
    Printf.printf "%d\n" @@ solve ws' m Empty 1 0
