let rec make_xy ?(l=[0]) = function
  | 0 -> l
  | n ->
    let t = Scanf.scanf "%d\n" (fun x -> x) in
    make_xy ~l:((List.hd l + t) :: l) (pred n)

let rec t2 t y = function
  | [] -> t
  | h :: l' ->
    let n = Array.get t (y-h) in
    Array.set t (y-h) (succ n);
    t2 t y l'

let rec t1 t = function
  | [] -> t
  | h :: l' -> t1 (t2 t h l') l'

let rec u2 t y cnt = function
  | [] -> cnt
  | h :: l' ->
    let n = Array.get t (y-h) in
    u2 t y (cnt+n) l'

let rec u1 t ?(cnt=0) = function
  | [] -> cnt
  | h :: l' -> u1 t ~cnt:(u2 t h cnt l') l'

let rec solve () =
  let nm = Scanf.scanf "%d %d\n" (fun i j -> (i, j)) in
  match nm with
  | (0, 0) -> exit 0
  | (n, m) ->
    let y = make_xy n in
    let x = make_xy m in
    let t = Array.make ((max m n)*1000) 0 in
    let cnt = u1 (t1 t y) x in
    Printf.printf "%d\n" cnt;
    solve ()

let () = solve ()