let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let s = Scanf.bscanf sb "%Ld " (fun s -> s) in
  let a0 = Array.make n 0 in
  let a1 = Array.make n 0 in
  let am0 = Array.make n 0 in
  let am1 = Array.make n 0 in
  let c0 = Array.make m (0, 0) in
  let c1 = Array.make m (0, 0) in
  let cnt0 = ref 0 in
  let cnt1 = ref 0 in
  let t0 = ref 0L in
  let t1 = ref 0L in
    for i = 0 to n - 1 do
      a0.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
      am0.(i) <-
	if i = 0
	then a0.(i)
	else min am0.(i - 1) a0.(i);
    done;
    for i = 0 to n - 1 do
      a1.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
      am1.(i) <-
	if i = 0
	then a1.(i)
	else min am1.(i - 1) a1.(i);
    done;
    for i = 0 to m - 1 do
      let t = Scanf.bscanf sb "%d " (fun s -> s) in
	if t = 1 then (
	  c0.(!cnt0) <- (Scanf.bscanf sb "%d " (fun s -> s), i);
	  incr cnt0;
	) else (
	  c1.(!cnt1) <- (Scanf.bscanf sb "%d " (fun s -> s), i);
	  incr cnt1;
	)
    done;
    let cnt0 = !cnt0 in
    let cnt1 = !cnt1 in
    let c0 = Array.sub c0 0 cnt0 in
    let c1 = Array.sub c1 0 cnt1 in
    let res = ref (n + 1) in
    let best0 = ref 0 in
      Array.sort compare c0;
      Array.sort compare c1;
      for i = 0 to min k cnt1 - 1 do
	t1 := !t1 +| Int64.of_int (fst c1.(i));
      done;
      for i = 0 to cnt0 do
	if k - i <= cnt1 then (
(*Printf.printf "asd %d %Ld %Ld\n" i !t0 !t1;*)
	  let l = ref (-1)
	  and r = ref n in
	    while !l < !r - 1 do
	      let m = (!l + !r) / 2 in
	      let t =
		Int64.of_int am0.(m) *| !t0 +|
		    Int64.of_int am1.(m) *| !t1
	      in
		if t <= s
		then r := m
		else l := m
	    done;
(*if n = 34 then Printf.printf "asd %d %Ld %Ld %d\n" i !t0 !t1 (!r + 1);*)
	    if !res > !r + 1 then (
	      res := !r + 1;
	      best0 := i;
	    )
	);
	if i < cnt0 then (
	  t0 := !t0 +| Int64.of_int (fst c0.(i));
	);
	if k - 1 - i < cnt1 && k - 1 - i >= 0 then (
	  t1 := !t1 -| Int64.of_int (fst c1.(k - 1 - i));
	);
      done;
      if !res > n
      then Printf.printf "%d\n" (-1)
      else (
	Printf.printf "%d\n" !res;
	let d0 = ref 0 in
	let d1 = ref 0 in
	let s' = ref 0L in
	  for i = 0 to !res - 1 do
	    if a0.(i) < a0.(!d0)
	    then d0 := i;
	    if a1.(i) < a1.(!d1)
	    then d1 := i;
	  done;
	  incr d0;
	  incr d1;
	  for i = 0 to !best0 - 1 do
	    Printf.printf "%d %d\n" (snd c0.(i) + 1) !d0;
	    s' := !s' +| Int64.of_int (fst c0.(i)) *| Int64.of_int a0.(!d0 - 1);
	  done;
	  for i = 0 to k - !best0 - 1 do
	    Printf.printf "%d %d\n" (snd c1.(i) + 1) !d1;
	    s' := !s' +| Int64.of_int (fst c1.(i)) *| Int64.of_int a1.(!d1 - 1);
	  done;
	  (*Printf.printf "qwe %Ld\n" !s';*)
	  assert (!s' <= s);
      )

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let ( ++ ) a b = Int64.add a b
let ( ** ) a b = Int64.mul a b
let ( -- ) a b = Int64.sub a b
let ( // ) a b = Int64.div a b
let long x = Int64.of_int x
let rec sum i j f ac = if i>j then ac else sum (i+1) j f (ac ++ (f i))

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let minf i j f = fold (i+1) j (fun k a -> min (f k) a) (f i)

let () =
  let n = read_int() in
  let m = read_int() in
  let k = read_int() in
  let s = long (read_int()) in

  let dcost = Array.init n (fun _ -> long (read_int())) in
  let pcost = Array.init n (fun _ -> long (read_int())) in

  let rec read_loop i plist dlist = if i=m then (plist,dlist) else
      let t = read_int () in
      let cost = long (read_int ()) in
      if t = 1
      then read_loop (i+1) plist ((cost,i)::dlist)
      else read_loop (i+1) ((cost,i)::plist) dlist
  in

  let (plist,dlist) = read_loop 0 [] [] in
  let parray = Array.of_list (List.sort compare plist) in
  let darray = Array.of_list (List.sort compare dlist) in

  (*
  let rec firstk i li ac = if i = k then List.rev ac else
      match li with
	| [] -> List.rev ac
	| h::t -> firstk (i+1) t (h::ac)
  in

  let plist = firstk 0 plist [] in
  let dlist = firstk 0 dlist [] in
  let dlist = List.rev dlist in
  *)

  let nd = min k (Array.length darray) in
  let np = Array.length parray in
  let p_start = k - nd in
  let dsum = sum 0 (nd-1) (fun i -> fst darray.(i)) 0L in
  let psum = sum 0 (p_start-1) (fun i -> fst parray.(i)) 0L in

  let is_possible d =
  (* return true if it's possible to get k gadgets in the first d days *)
  (* days are numbered 0, 1, 2, ... n-1.  so we're looking at days 0,1,..d-1 *)
    let (best_dollar, dday) = minf 0 (d-1) (fun j -> (dcost.(j), j)) in
    let (best_pound, pday) = minf 0 (d-1) (fun j -> (pcost.(j), j)) in

    let rec scan i dsum psum =
      if best_dollar ** dsum ++ best_pound ** psum <= s then i
      else if i >= nd || i + p_start >= np then -1 else
	scan (i+1) (dsum -- (fst darray.(nd-1-i))) (psum ++ (fst parray.(p_start+i)))
    in

    let goodi = scan 0 dsum psum in
    (goodi >= 0, (dday, pday, goodi))
  in

  let rec bsearch lo hi =
  (* lo is impossible, hi is possible *)
    if lo = hi-1 then hi else
      let m = (lo+hi)/2 in
      if fst (is_possible m) then bsearch lo m else bsearch m hi
  in

  if not (fst (is_possible n)) then printf "-1\n" else (
    let answer = bsearch 0 n in
    printf "%d\n" answer;

    let (_, (dday, pday, goodi)) = is_possible answer in

    for j=0 to nd-goodi-1 do
      printf "%d %d\n" (1+ snd darray.(j)) (1+dday)
    done;

    for j=0 to p_start+goodi-1 do
      printf "%d %d\n" (1+ snd parray.(j)) (1+pday)
    done

  )

let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let s = Scanf.bscanf sb "%Ld " (fun s -> s) in
  let a0 = Array.make n 0 in
  let a1 = Array.make n 0 in
  let am0 = Array.make n 0 in
  let am1 = Array.make n 0 in
  let c0 = Array.make m (0, 0) in
  let c1 = Array.make m (0, 0) in
  let cnt0 = ref 0 in
  let cnt1 = ref 0 in
  let t0 = ref 0L in
  let t1 = ref 0L in
    for i = 0 to n - 1 do
      a0.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
      am0.(i) <-
	if i = 0
	then a0.(i)
	else min am0.(i - 1) a0.(i);
    done;
    for i = 0 to n - 1 do
      a1.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
      am1.(i) <-
	if i = 0
	then a1.(i)
	else min am1.(i - 1) a1.(i);
    done;
    for i = 0 to m - 1 do
      let t = Scanf.bscanf sb "%d " (fun s -> s) in
	if t = 1 then (
	  c0.(!cnt0) <- (Scanf.bscanf sb "%d " (fun s -> s), i);
	  incr cnt0;
	) else (
	  c1.(!cnt1) <- (Scanf.bscanf sb "%d " (fun s -> s), i);
	  t1 := !t1 +| Int64.of_int (fst c1.(!cnt1));
	  incr cnt1;
	)
    done;
    let cnt0 = !cnt0 in
    let cnt1 = !cnt1 in
    let c0 = Array.sub c0 0 cnt0 in
    let c1 = Array.sub c1 0 cnt1 in
    let res = ref (n + 1) in
    let best0 = ref 0 in
      Array.sort compare c0;
      Array.sort compare c1;
      for i = 0 to cnt0 do
	if k - i <= cnt1 then (
(*Printf.printf "asd %d %Ld %Ld\n" i !t0 !t1;*)
	  let l = ref (-1)
	  and r = ref n in
	    while !l < !r - 1 do
	      let m = (!l + !r) / 2 in
	      let t =
		Int64.of_int am0.(m) *| !t0 +|
		    Int64.of_int am1.(m) *| !t1
	      in
		if t <= s
		then r := m
		else l := m
	    done;
if n = 34 then Printf.printf "asd %d %Ld %Ld %d\n" i !t0 !t1 (!r + 1);
	    if !res > !r + 1 then (
	      res := !r + 1;
	      best0 := i;
	    )
	);
	if i < cnt0 then (
	  t0 := !t0 +| Int64.of_int (fst c0.(i));
	);
	if k - 1 - i < cnt1 && k - 1 - i >= 0 then (
	  t1 := !t1 -| Int64.of_int (fst c1.(k - 1 - i));
	);
      done;
      if !res > n
      then Printf.printf "%d\n" (-1)
      else (
	Printf.printf "%d\n" !res;
	let d0 = ref 0 in
	let d1 = ref 0 in
	let s' = ref 0L in
	  for i = 0 to !res - 1 do
	    if a0.(i) < a0.(!d0)
	    then d0 := i;
	    if a1.(i) < a1.(!d1)
	    then d1 := i;
	  done;
	  incr d0;
	  incr d1;
	  for i = 0 to !best0 - 1 do
	    Printf.printf "%d %d\n" (snd c0.(i) + 1) !d0;
	    s' := !s' +| Int64.of_int (fst c0.(i)) *| Int64.of_int a0.(!d0 - 1);
	  done;
	  for i = 0 to k - !best0 - 1 do
	    Printf.printf "%d %d\n" (snd c1.(i) + 1) !d1;
	    s' := !s' +| Int64.of_int (fst c1.(i)) *| Int64.of_int a1.(!d1 - 1);
	  done;
	  (*Printf.printf "qwe %Ld\n" !s';*)
	  assert (!s' <= s);
      )

let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let s = Scanf.bscanf sb "%d " (fun s -> s) in
  let a0 = Array.make n 0 in
  let a1 = Array.make n 0 in
  let am0 = Array.make n 0 in
  let am1 = Array.make n 0 in
  let c0 = Array.make m (0, 0) in
  let c1 = Array.make m (0, 0) in
  let cnt0 = ref 0 in
  let cnt1 = ref 0 in
  let t0 = ref 0L in
  let t1 = ref 0L in
    for i = 0 to n - 1 do
      a0.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
      am0.(i) <-
	if i = 0
	then a0.(i)
	else min am0.(i - 1) a0.(i);
    done;
    for i = 0 to n - 1 do
      a1.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
      am1.(i) <-
	if i = 0
	then a1.(i)
	else min am1.(i - 1) a1.(i);
    done;
    for i = 0 to m - 1 do
      let t = Scanf.bscanf sb "%d " (fun s -> s) in
	if t = 1 then (
	  c0.(!cnt0) <- (Scanf.bscanf sb "%d " (fun s -> s), i);
	  incr cnt0;
	) else (
	  c1.(!cnt1) <- (Scanf.bscanf sb "%d " (fun s -> s), i);
	  t1 := !t1 +| Int64.of_int (fst c1.(!cnt1));
	  incr cnt1;
	)
    done;
    let cnt0 = !cnt0 in
    let cnt1 = !cnt1 in
    let c0 = Array.sub c0 0 cnt0 in
    let c1 = Array.sub c1 0 cnt1 in
    let res = ref (n + 1) in
    let best0 = ref 0 in
      Array.sort compare c0;
      Array.sort compare c1;
      for i = 0 to cnt0 do
	if k - i <= cnt1 then (
(*Printf.printf "asd %d %Ld %Ld\n" i !t0 !t1;*)
	  let l = ref (-1)
	  and r = ref n in
	    while !l < !r - 1 do
	      let m = (!l + !r) / 2 in
	      let t =
		Int64.of_int am0.(m) *| !t0 +|
		    Int64.of_int am1.(m) *| !t1
	      in
		if t <= Int64.of_int s
		then r := m
		else l := m
	    done;
	    if !res > !r + 1 then (
	      res := !r + 1;
	      best0 := i;
	    )
	);
	if i < cnt0 then (
	  t0 := !t0 +| Int64.of_int (fst c0.(i));
	);
	if k - 1 - i < cnt1 && k - 1 - i >= 0 then (
	  t1 := !t1 -| Int64.of_int (fst c1.(k - 1 - i));
	);
      done;
      if !res > n
      then Printf.printf "%d\n" (-1)
      else (
	Printf.printf "%d\n" !res;
	let d0 = ref 0 in
	let d1 = ref 0 in
	let s' = ref 0L in
	  for i = 0 to !res - 1 do
	    if a0.(i) < a0.(!d0)
	    then d0 := i;
	    if a1.(i) < a1.(!d1)
	    then d1 := i;
	  done;
	  incr d0;
	  incr d1;
	  for i = 0 to !best0 - 1 do
	    Printf.printf "%d %d\n" (snd c0.(i) + 1) !d0;
	    s' := !s' +| Int64.of_int (fst c0.(i)) *| Int64.of_int a0.(!d0 - 1);
	  done;
	  for i = 0 to k - !best0 - 1 do
	    Printf.printf "%d %d\n" (snd c1.(i) + 1) !d1;
	    s' := !s' +| Int64.of_int (fst c1.(i)) *| Int64.of_int a1.(!d1 - 1);
	  done;
	  assert (!s' <= Int64.of_int s);
      )

let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let s = Scanf.bscanf sb "%Ld " (fun s -> s) in
  let a0 = Array.make n 0 in
  let a1 = Array.make n 0 in
  let am0 = Array.make n 0 in
  let am1 = Array.make n 0 in
  let c0 = Array.make m (0, 0) in
  let c1 = Array.make m (0, 0) in
  let cnt0 = ref 0 in
  let cnt1 = ref 0 in
  let t0 = ref 0L in
  let t1 = ref 0L in
    for i = 0 to n - 1 do
      a0.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
      am0.(i) <-
	if i = 0
	then a0.(i)
	else min am0.(i - 1) a0.(i);
    done;
    for i = 0 to n - 1 do
      a1.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
      am1.(i) <-
	if i = 0
	then a1.(i)
	else min am1.(i - 1) a1.(i);
    done;
    for i = 0 to m - 1 do
      let t = Scanf.bscanf sb "%d " (fun s -> s) in
	if t = 1 then (
	  c0.(!cnt0) <- (Scanf.bscanf sb "%d " (fun s -> s), i);
	  incr cnt0;
	) else (
	  c1.(!cnt1) <- (Scanf.bscanf sb "%d " (fun s -> s), i);
	  if !cnt1 < k
	  then t1 := !t1 +| Int64.of_int (fst c1.(!cnt1));
	  incr cnt1;
	)
    done;
    let cnt0 = !cnt0 in
    let cnt1 = !cnt1 in
    let c0 = Array.sub c0 0 cnt0 in
    let c1 = Array.sub c1 0 cnt1 in
    let res = ref (n + 1) in
    let best0 = ref 0 in
      Array.sort compare c0;
      Array.sort compare c1;
      for i = 0 to cnt0 do
	if k - i <= cnt1 then (
(*Printf.printf "asd %d %Ld %Ld\n" i !t0 !t1;*)
	  let l = ref (-1)
	  and r = ref n in
	    while !l < !r - 1 do
	      let m = (!l + !r) / 2 in
	      let t =
		Int64.of_int am0.(m) *| !t0 +|
		    Int64.of_int am1.(m) *| !t1
	      in
		if t <= s
		then r := m
		else l := m
	    done;
(*if n = 34 then Printf.printf "asd %d %Ld %Ld %d\n" i !t0 !t1 (!r + 1);*)
	    if !res > !r + 1 then (
	      res := !r + 1;
	      best0 := i;
	    )
	);
	if i < cnt0 then (
	  t0 := !t0 +| Int64.of_int (fst c0.(i));
	);
	if k - 1 - i < cnt1 && k - 1 - i >= 0 then (
	  t1 := !t1 -| Int64.of_int (fst c1.(k - 1 - i));
	);
      done;
      if !res > n
      then Printf.printf "%d\n" (-1)
      else (
	Printf.printf "%d\n" !res;
	let d0 = ref 0 in
	let d1 = ref 0 in
	let s' = ref 0L in
	  for i = 0 to !res - 1 do
	    if a0.(i) < a0.(!d0)
	    then d0 := i;
	    if a1.(i) < a1.(!d1)
	    then d1 := i;
	  done;
	  incr d0;
	  incr d1;
	  for i = 0 to !best0 - 1 do
	    Printf.printf "%d %d\n" (snd c0.(i) + 1) !d0;
	    s' := !s' +| Int64.of_int (fst c0.(i)) *| Int64.of_int a0.(!d0 - 1);
	  done;
	  for i = 0 to k - !best0 - 1 do
	    Printf.printf "%d %d\n" (snd c1.(i) + 1) !d1;
	    s' := !s' +| Int64.of_int (fst c1.(i)) *| Int64.of_int a1.(!d1 - 1);
	  done;
	  (*Printf.printf "qwe %Ld\n" !s';*)
	  assert (!s' <= s);
      )

let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let s = Scanf.bscanf sb "%d " (fun s -> s) in
  let a0 = Array.make n 0 in
  let a1 = Array.make n 0 in
  let am0 = Array.make n 0 in
  let am1 = Array.make n 0 in
  let c0 = Array.make m (0, 0) in
  let c1 = Array.make m (0, 0) in
  let cnt0 = ref 0 in
  let cnt1 = ref 0 in
  let t0 = ref 0L in
  let t1 = ref 0L in
    for i = 0 to n - 1 do
      a0.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
      am0.(i) <-
	if i = 0
	then a0.(i)
	else min am0.(i - 1) a0.(i);
    done;
    for i = 0 to n - 1 do
      a1.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
      am1.(i) <-
	if i = 0
	then a1.(i)
	else min am1.(i - 1) a1.(i);
    done;
    for i = 0 to m - 1 do
      let t = Scanf.bscanf sb "%d " (fun s -> s) in
	if t = 1 then (
	  c0.(!cnt0) <- (Scanf.bscanf sb "%d " (fun s -> s), i);
	  incr cnt0;
	) else (
	  c1.(!cnt1) <- (Scanf.bscanf sb "%d " (fun s -> s), i);
	  t1 := !t1 +| Int64.of_int (fst c1.(!cnt1));
	  incr cnt1;
	)
    done;
    let cnt0 = !cnt0 in
    let cnt1 = !cnt1 in
    let c0 = Array.sub c0 0 cnt0 in
    let c1 = Array.sub c1 0 cnt1 in
    let res = ref (n + 1) in
    let best0 = ref 0 in
      Array.sort compare c0;
      Array.sort compare c1;
      for i = 0 to cnt0 do
	if k - i <= cnt1 then (
	  let l = ref (-1)
	  and r = ref n in
	    while !l < !r - 1 do
	      let m = (!l + !r) / 2 in
	      let t =
		Int64.of_int am0.(m) *| !t0 +|
		    Int64.of_int am1.(m) *| !t1
	      in
		if t <= Int64.of_int s
		then r := m
		else l := m
	    done;
	    if !res > !r + 1 then (
	      res := !r + 1;
	      best0 := i;
	    )
	);
	if i < cnt0 then (
	  t0 := !t0 +| Int64.of_int (fst c0.(i));
	);
	if k - 1 - i < cnt1 && k - 1 - i >= 0 then (
	  t1 := !t1 -| Int64.of_int (fst c1.(k - 1 - i));
	);
      done;
      if !res > n
      then Printf.printf "%d\n" (-1)
      else (
	Printf.printf "%d\n" !res;
	let d0 = ref 0 in
	let d1 = ref 0 in
	  for i = 0 to !res - 1 do
	    if a0.(i) < a0.(!d0)
	    then d0 := i;
	    if a1.(i) < a1.(!d0)
	    then d1 := i;
	  done;
	  incr d0;
	  incr d1;
	  for i = 0 to !best0 - 1 do
	    Printf.printf "%d %d\n" (snd c0.(i) + 1) !d0;
	  done;
	  for i = 0 to k - !best0 - 1 do
	    Printf.printf "%d %d\n" (snd c1.(i) + 1) !d1;
	  done;
      )


let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

type element = {mutable p : int;
		mutable rank : int;
		mutable w : int64}

type t = element array

let makeset set x =
  set.(x) <- {p = x; rank = 0; w = 0L}

(*
let rec find set x =
  if x <> set.(x).p then set.(x).p <- find set set.(x).p;
  set.(x).p
*)
let rec find set x =
  if x <> set.(x).p
  then find set set.(x).p
  else x

let link set x y w =
  if set.(x).rank > set.(y).rank then (
    set.(y).p <- x;
    set.(y).w <- w;
    x
  ) else (
    if set.(x).rank = set.(y).rank then set.(y).rank <- set.(y).rank + 1;
    set.(x).p <- y;
    set.(x).w <- w;
    y
  )

let newsets size =
  let set = Array.make size {p = -1; rank = 0; w = 0L} in
    for i = 0 to size-1 do
      makeset set i
    done;
    set

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let es = Array.make m (0L, 0, 0) in
    for i = 0 to m - 1 do
      let u = Scanf.bscanf sb "%d " (fun s -> s) in
      let v = Scanf.bscanf sb "%d " (fun s -> s) in
      let w = Scanf.bscanf sb "%Ld " (fun s -> s) in
	es.(i) <- (w, u - 1, v - 1)
    done;
    let es' = Array.copy es in
    let () = Array.sort compare es in
    let ds = newsets n in
    let c = Array.make n (-1) in
    let t = ref 0L in
      for i = 0 to m - 1 do
	let (w, u, v) = es.(i) in
	let ru = find ds u in
	let rv = find ds v in
	  if ru <> rv then (
	    ignore (link ds ru rv w);
	    t := !t +| w
	  )
      done;
      for i = 0 to m - 1 do
	let lca =
	  let (_w, u, v) = es'.(i) in
	  let u = ref u in
	  let v = ref v in
	    while ds.(!u).p <> !u do
	      c.(!u) <- i;
	      u := ds.(!u).p
	    done;
	    c.(!u) <- i;
	    while c.(!v) <> i do
	      v := ds.(!v).p
	    done;
	    !v
	in
	let (w, u, v) = es'.(i) in
	let ww = ref 0L in
	let u = ref u in
	let v = ref v in
	  while !u <> lca do
	    ww := max !ww ds.(!u).w;
	    u := ds.(!u).p
	  done;
	  while !v <> lca do
	    ww := max !ww ds.(!v).w;
	    v := ds.(!v).p
	  done;
	  let t = !t -| !ww +| w in
	    Printf.printf "%Ld\n" t
      done;

(* started 1:10 *)

open Printf
open Scanf

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let ( // ) a b = Int64.div a b
let ( %% ) a b = Int64.rem a b

let long x = Int64.of_int x
let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let rec sum i j f ac = if i>j then ac else sum (i+1) j f (ac ++ (f i))

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)

let () =

  let n = read_int() in
  let m = read_int() in

  let edges_unsorted = fold 1 m (fun i ac ->
    let u = read_int() -1 in
    let v = read_int() -1 in
    let w = long (read_int()) in
    (w,u,v)::ac) []
  in

  let edges_unsorted = List.rev edges_unsorted in

  let edges = List.sort compare edges_unsorted in

  let uf = Array.make n (-1) in
  let weight = Array.make n 0L in

  let rec find i = if uf.(i) < 0 then i else find uf.(i) in

  let union i j w =
  (* assume i and j are both canonical elements *)
    let (wi,wj) = (-uf.(i), -uf.(j)) in
    if wi < wj then (
      uf.(j) <- uf.(i) + uf.(j);
      uf.(i) <- j;
      weight.(i) <- w;
    ) else (
      uf.(i) <- uf.(i) + uf.(j);
      uf.(j) <- i;
      weight.(j) <- w;
    )
  in

  List.iter (fun (w,u,v) ->
    let (i,j) = (find u, find v) in
    if i <> j then union i j w
  ) edges;

  let path = Array.make n (-1) in

  let total_cost = sum 0 (n-1) (fun v -> weight.(v)) 0L in

  let lca u v i =
    let rec mark u = if u >= 0 then (path.(u) <- i; mark uf.(u)) in
    mark u;
    let rec fmark v = if path.(v) = i then v else fmark uf.(v) in
    fmark v
  in

  let rec max_cost_to v l ac = if v = l then ac else
      max_cost_to uf.(v) l (max ac weight.(v))
  in

  List.iteri (fun i (w,u,v) ->
    let l = lca u v i in
    let wt = max (max_cost_to u l 0L) (max_cost_to v l 0L) in
    printf "%Ld\n" (total_cost ++ w -- wt)
  ) edges_unsorted;


let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%Ld " (fun s -> s) in
  let c = ref 0L in
    for i = 1 to n do
      let k = Scanf.bscanf sb "%Ld " (fun s -> s) in
	c := !c +| k;
	Printf.printf "%Ld " (!c /| m);
	c := Int64.rem !c m;
    done;
    Printf.printf "\n"

open Printf open Scanf
module MyInt = struct let (+) = (+) let (-) = (-) let (/) = (/) let ( * ) = ( * ) let (mod) = (mod) let (%) = (mod) let (+=) r v = r := !r + v let (-=) r v = r := !r - v let ( *=) r v= r := !r * v let (/=) r v = r := !r / v end
module MyInt64 = struct
	let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * )
	let (+) p q = Int64.add p q let (-) p q = Int64.sub p q let ( * ) p q = Int64.mul p q let (/) p q = Int64.div p q let (+=) r v = r := !r + v let (-=) r v = r := !r - v let ( *=) r v= r := !r * v let (/=) r v = r := !r / v
	let labs p = if p < 0L then -1L*p else p
	let (~|) p = Int64.to_int p
	let (~~|) p = Int64.of_int p

	let input_i64_array n = Array.init (~| n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
	let get_i64 () = Scanf.scanf " %Ld" (fun v -> v) let get_2_i64 () = Scanf.scanf " %Ld %Ld" (fun u v -> u,v) let get_3_i64 () = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w) let get_4_i64 () = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x) let get_5_i64 () = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
	let print_i64_endline n = n |> Int64.to_string |> print_endline

	let (mod) m n = m - (m/n) * n let (%) = (mod)
	let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
	let lcm m n = (m*n) / gcd m n
	let rep from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_-$1 && !f do match fbod !i with | `Ok -> i := !i +$ 1; | `Ng -> f := false done
	let repi64 from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_-$1 && !f do match fbod ~~| !i with | `Ok -> i := !i +$ 1; | `Ng -> f := false done
end

open MyInt64
let () =
	let n,m = get_2_i64 ()
	in let a = input_i64_array n
	in
	Array.fold_left (fun (res,sum,cur) v ->
		let w = cur + v
		in if w >= m then (
			let q = w / m in let r = w - q * m
			in
			(q::res,sum+q,r)
			(* if sum+q >= n then (
				let q' = n - sum in
				(q'::res,n,r)
			) else (
				(q::res,sum+q,r)
			) *)
		) else (0L::res,sum,w)
	) ([],0L,0L) a
	|> (fun (r,_,_) -> r)
	|> List.rev
	|> List.iter (printf "%Ld ")

open Printf open Scanf
module MyInt = struct let (+) = (+) let (-) = (-) let (/) = (/) let ( * ) = ( * ) let (mod) = (mod) let (%) = (mod) let (+=) r v = r := !r + v let (-=) r v = r := !r - v let ( *=) r v= r := !r * v let (/=) r v = r := !r / v end
module MyInt64 = struct
	let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * )
	let (+) p q = Int64.add p q let (-) p q = Int64.sub p q let ( * ) p q = Int64.mul p q let (/) p q = Int64.div p q let (+=) r v = r := !r + v let (-=) r v = r := !r - v let ( *=) r v= r := !r * v let (/=) r v = r := !r / v
	let labs p = if p < 0L then -1L*p else p
	let (~|) p = Int64.to_int p
	let (~~|) p = Int64.of_int p

	let input_i64_array n = Array.init (~| n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
	let get_i64 () = Scanf.scanf " %Ld" (fun v -> v) let get_2_i64 () = Scanf.scanf " %Ld %Ld" (fun u v -> u,v) let get_3_i64 () = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w) let get_4_i64 () = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x) let get_5_i64 () = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
	let print_i64_endline n = n |> Int64.to_string |> print_endline

	let (mod) m n = m - (m/n) * n let (%) = (mod)
	let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
	let lcm m n = (m*n) / gcd m n
	let rep from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_-$1 && !f do match fbod !i with | `Ok -> i := !i +$ 1; | `Ng -> f := false done
	let repi64 from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_-$1 && !f do match fbod ~~| !i with | `Ok -> i := !i +$ 1; | `Ng -> f := false done
end

open MyInt64
let () =
	let n,m = get_2_i64 ()
	in let a = input_i64_array n
	in
	Array.fold_left (fun (res,sum,cur) v ->
		if sum = n then (res,sum,cur)
		else if sum > n then failwith ""
		else
		let w = cur + v
		in if w >= m then (
			let q = w / m in let r = w - q * m
			in
			if sum+q >= n then (
				let q' = n - sum in
				(q'::res,n,r)
			) else (
				(q::res,sum+q,r)
			)
		) else (0L::res,sum,w)
	) ([],0L,0L) a
	|> (fun (r,_,_) -> r)
	|> List.rev
	|> List.iter (printf "%Ld ")


module A = Array;;
module C = Char;;
module L = List;;
module S = String;;

let pf = Printf.printf;;
let sf = Scanf.scanf;;
let ssf = Scanf.sscanf;;

let (@@) f x = f x;;
let (|>) x f = f x;;

exception Error of string

let inf = 1000000000;;
let eps = 1e-11;;

type ('a, 'b) avl_data =
  | Empty
  | Node of int * ('a * 'b) * ('a, 'b) avl_data * ('a, 'b) avl_data
;;

type 'a option = Nothing | Just of 'a;;

type ('a, 'b) avl_tree =
  { set : ('a * 'b) -> unit; value : 'a -> 'b option }
;;

let new_avl_tree cmp =
  let tree = ref Empty in
  let err() = raise @@ Error "AVL-Tree Internal Error." in
  let height = function Empty -> 0 | Node (h, _, _, _) -> h in
  let make_node a l r = Node (max (height l) (height r) + 1, a, l, r) in
  let rotate = function
      Empty -> Empty
    | Node (_, a, l, r) ->
       if height l > height r + 1
       then (match l with
               Empty -> err()
             | Node (_, b, ll, lr) ->
                if height ll >= height lr
                then make_node b ll (make_node a lr r)
                else (match lr with
                        Empty -> err()
                      | Node (_, c, lrl, lrr) ->
                         make_node c (make_node b ll lrl) (make_node a lrr r)))
       else if height l + 1 < height r
       then (match r with
               Empty -> err()
             | Node (_, b, rl, rr) ->
                if height rl <= height rr
                then make_node b (make_node a l rl) rr
                else (match rl with
                        Empty -> err()
                      | Node (_, c, rll, rlr) ->
                         make_node c (make_node a l rll) (make_node b rlr rr)))
       else make_node a l r in
  let rec insert (x, y) = function
      Empty -> Node (1, (x, y), Empty, Empty)
    | Node (h, (k, v), l, r) ->
       let t = if cmp x k = 0
               then Node (h, (x, y), l, r)
               else if cmp x k < 0
               then Node (h, (k, v), insert (x, y) l, r)
               else Node (h, (k, v), l, insert (x, y) r) in
       rotate t in
  let rec find x = function
      Empty -> Nothing
    | Node (_, (k, v), l, r) ->
       if cmp x k = 0
       then Just v
       else if cmp x k < 0
       then find x l
       else find x r in
  let this() =
    { set = (fun x -> tree := insert x !tree);
      value = (fun x -> find x !tree) } in
  this();
;;

let _ =
  let str = read_line() in
  let (n, m) = ssf str "%d %d" (fun x y -> x, y) in
  let avl = new_avl_tree compare in
  for i = 1 to m do
    let s = read_line() in
    let (a, b) = ssf s "%s %s" (fun x y -> x, y) in
    let res = if String.length a <= String.length b then a else b in
    avl.set (a, res); avl.set (b, res);
  done;
  let str = read_line() in
  let slist = Str.split (Str.regexp " ") str in
  let reveal = function Nothing -> " " | Just a -> a in
  List.map (fun s -> print_string @@ reveal (avl.value s) ^ " ") slist
;;

module StringMap = Map.Make(String)

let read_int () = Scanf.scanf "%d %d " (fun n m -> (n, m))
let read_str () = Scanf.scanf "%s " (fun s -> s)

let get_short str1 str2 =
    if String.length str1 <= String.length str2
    then
        str1
    else
        str2

let fill_map m =
     let map = StringMap.empty in
     let rec loop i map =
        if i < m
        then
            let key = read_str () in
            let temp = read_str () in
            let value = get_short key temp in
            let map = StringMap.add key value map in
            loop (i + 1) map
        else
            map
    in loop 0 map

let fill_list n =
    let rec loop i list =
        if i < n
        then
            let temp = read_str () in
            loop (i + 1) (temp :: list)
        else
            List.rev list
    in loop 0 []

let input () =
    let (n, m) = read_int () in
    let words = fill_map m in
    let text = fill_list n in
    (words, text)

let solve (words, text) =
    let rec loop list acc =
        match list with
        | (head :: tail) ->
            let short = StringMap.find head words in
            loop tail (short :: acc)
        | [] -> List.rev acc
    in loop text []

let print result =
    (List.iter (Printf.printf "%s ") result;
    Printf.printf "\n")

let () = print (solve (input ()))

let () = Scanf.scanf "%d %d\n" (fun n m ->
  let h = Hashtbl.create n in
  for i = 1 to m do
    Scanf.scanf "%[^ ] %s\n" (Hashtbl.add h)
  done;
  let w = Array.init n (fun _ ->
    Scanf.scanf "%[^ \n]%_c" (fun s ->
      let t = Hashtbl.find h s in
      if String.length t < String.length s
      then t else s
    )) in
  print_endline (String.concat " " (Array.to_list w))
)


open List open Str open Int64;;
let cmp x y =
    if(nth x 0) < (nth y 0) || (((nth x 0) = (nth y 0)) && ((nth x 1) < (nth y 1)))
    then -1 else 1;;
let n = read_int ();;
let par = Array.create n [0L;0L] and x_1 :: x_2 :: [] =
    map (of_string) (split (regexp " ") (read_line ()));;
for i = 1 to n do
    let k :: b :: [] =
        map (of_string) (split (regexp " ") (read_line ())) in
            Array.set par (i - 1) [(add (mul k x_1) b); (add (mul k x_2) b)];
done;;
Array.sort cmp par;;
for i = 0 to  (Array.length par) - 2 do
    if (nth par.(i + 1) 1) < (nth par.(i) 1) then begin
        print_endline "YES";
        exit 0;
end done;;
print_endline "NO";;

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_long () = bscanf Scanning.stdib " %Ld " (fun x -> x)

let rec forall i j f = (i>j) || ((f i) && forall (i+1) j f)

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let ( // ) a b = Int64.div a b
let ( %% ) a b = Int64.rem a b

let long x = Int64.of_int x
let short x = Int64.to_int x

let () =
  let n = read_int () in
  let x1 = read_long () in
  let x2 = read_long () in

  let k = Array.make n 0L in
  let b = Array.make n 0L in

  for i=0 to n-1 do
    k.(i) <- read_long();
    b.(i) <- read_long()
  done;

  let y1 = Array.init n (fun i -> (k.(i) ** x1 ++ b.(i), i)) in
  let y2 = Array.init n (fun i -> (k.(i) ** x2 ++ b.(i), i)) in

  let y1_init = Array.copy y1 in
  let y2_init = Array.copy y2 in

  Array.sort compare y1;
  Array.sort compare y2;

  Array.sort (fun (v1,i1) (v2,i2) ->
    if v1 <> v2 then compare v1 v2
    else compare y2_init.(i1) y2_init.(i2)
  ) y1;

  Array.sort (fun (v1,i1) (v2,i2) ->
    if v1 <> v2 then compare v1 v2
    else compare y1_init.(i1) y1_init.(i2)
  ) y2;

  let answer = forall 0 (n-1) (
    fun i ->
      let (_,i1) = y1.(i) in
      let (_,i2) = y2.(i) in
      i1 = i2
  ) in

  if answer then printf "NO\n" else printf "YES\n"

let cmp x y = if x.(0) < y.(0) then -1 else 1;;
let n = read_int ();;
let ls = ref (Array.create n [|0;0|]);;
let x_1 = ref 0 and x_2 = ref 0;;
let ins = List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())) in
begin
	x_1 := List.nth ins 0;
	x_2 := List.nth ins 1;
end;;
for i = 1 to n do
	let ins = List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())) in
	let k = List.nth ins 0 and b = List.nth ins 1 in
	begin
		ls := Array.append !ls [|[|(!x_1 * k + b);(!x_2 * k + b)|]|];
	end;
done;
Array.stable_sort cmp !ls;;
let fl = ref 0;;
for i = 0 to Array.length !ls - 2 do
	if (!ls.(i).(1) > !ls.(i + 1).(1)) then fl := 1;
done;;
if !fl = 0 then print_endline "NO" else print_endline "YES";;

(*
for i = 0 to Array.length !ls - 1 do
	print_int !ls.(i).(0);
	print_string " ";
	print_int !ls.(i).(1);
	print_endline "";
done;;*)

let cmp x y = if x.(0) < y.(0) then -1 else 1;;
let n = read_int ();;
let ls = ref (Array.create n [|0;0|]);;
let x_1 = ref 0 and x_2 = ref 0;;
let ins = List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())) in
begin
	x_1 := List.nth ins 0;
	x_2 := List.nth ins 1;
end;;
for i = 1 to n do
	let ins = List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())) in
	let k = List.nth ins 0 and b = List.nth ins 1 in
	begin
        Array.set (!ls) (i - 1) [|!x_1 * k + b; !x_2 * k + b|];
	end;
done;
Array.stable_sort cmp !ls;;
let fl = ref 0;;
for i = 0 to Array.length !ls - 2 do
	if ((!ls.(i).(1) > !ls.(i + 1).(1))) && (!ls.(i).(0) != !ls.(i + 1).(0))  then fl := 1;
done;;
if !fl = 0 then print_endline "NO" else print_endline "YES";;
(*
for i = 0 to Array.length !ls - 1 do
	print_int !ls.(i).(0);
	print_string " ";
	print_int !ls.(i).(1);
	print_endline "";
done;;*)

let cmp x y = if x.(0) < y.(0) then -1 else 1;;
let n = read_int ();;
let ls = ref (Array.create n [|0;0|]);;
let x_1 = ref 0 and x_2 = ref 0;;
let ins = List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())) in
begin
	x_1 := List.nth ins 0;
	x_2 := List.nth ins 1;
end;;
for i = 1 to n do
	let ins = List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())) in
	let k = List.nth ins 0 and b = List.nth ins 1 in
	begin
        Array.set (!ls) (i - 1) [|!x_1 * k + b; !x_2 * k + b|];
	end;
done;
Array.stable_sort cmp !ls;;
let fl = ref 0;;
for i = 0 to Array.length !ls - 2 do
	if (!ls.(i).(1) > !ls.(i + 1).(1)) then fl := 1;
done;;
if !fl = 0 then print_endline "NO" else print_endline "YES";;
(*
for i = 0 to Array.length !ls - 1 do
	print_int !ls.(i).(0);
	print_string " ";
	print_int !ls.(i).(1);
	print_endline "";
done;;*)


let bs = Scanf.Scanning.stdin
let xint() = Scanf.bscanf bs " %d" (fun x -> x)
let xint32() = Scanf.bscanf bs " %ld" (fun x -> x)
let xint64() = Scanf.bscanf bs " %Ld" (fun x -> x)
let xstr() = Scanf.bscanf bs " %s" (fun x -> x)
let xreal() = Scanf.bscanf bs " %f" (fun x -> x)
let printf = Printf.printf

let ( +| ) = Int64.add
let ( -| ) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div
let ( %| ) = Int64.rem

let () =
  let n = xint() in
  let x1 = xint64() and y1 = xint64() in
  let x2 = xint64() and y2 = xint64() in
  let x = Array.make n 0L and y = Array.make n 0L in
  for i = 0 to n - 1 do
    x.(i) <- xint64();
    y.(i) <- xint64();
  done;

  let sqr x = x *| x in
  let max x y =
    if Int64.compare x y > 0 then x
    else y
  in
  let min x y =
    if Int64.compare x y < 0 then x
    else y
  in

  let ret = ref 9223372036854775807L in
  for i = -1 to n - 1 do
    let r1 = (if i = -1 then 0L else ((sqr (x.(i) -| x1)) +| (sqr (y.(i) -| y1)))) in
    let r2 = ref 0L in
    for j = 0 to n - 1 do
      let a = (sqr (x.(j) -| x1)) +| (sqr (y.(j) -| y1)) in
      let b = (sqr (x.(j) -| x2)) +| (sqr (y.(j) -| y2)) in
      if Int64.compare a r1 > 0 then (
        r2 := max !r2 b;
      );
    done;
    ret := min !ret (r1 +| !r2);
  done;
  printf "%Ld\n" !ret;

let read1 () = Scanf.scanf "%d\n" ( fun x -> x );;
let read2 () = Scanf.scanf "%d %d\n" ( fun x y -> x, y );;
let read3 () = Scanf.scanf "%d %d %d\n" ( fun x y z -> x, y, z);;
let read5 () = Scanf.scanf "%d %d %d %d %d\n" ( fun x y z a b-> x, y, z,a,b);;
let read x = (if x = 0 then Scanf.scanf "%d\n" else Scanf.scanf "%d ") ( fun x -> x );;

let (+$) x y =
  if x > max_int - y then max_int
  else x + y;;

let rec ( *$ ) x y =
  if y = 0 then 0 else
  if y mod 2 = 1 then x *$ (y-1) +$ x
  else let a = x *$ (y/2) in a +$ a;;

let (+|) = Int64.add;;
let (-|) = Int64.sub;;
let ( *| ) = Int64.mul;;
let ( /| ) = Int64.div;;
let two_64 = Int64.one +| Int64.one;;

let (n,x1,y1,x2,y2) = read5 ();;

let coord = Array.init n ( fun _ -> read2 () );;

let square a = a *| a;;

let dis (a,b) (c,d) =
  square ( Int64.of_int(a-c) ) +| ( square ( Int64.of_int(b-d) ) );;

let licz ()  =
  Array.sort ( fun a b -> compare ( dis a (x1,y1) ) (dis b (x1,y1) ) ) coord;
  let wynik = ref Int64.max_int in
  for i=0 to n do
    let r1 = if i=0 then Int64.zero else dis coord.(i-1) (x1,y1) in
    let r2 = ref Int64.zero in
    for j=i to (n-1) do
      r2 := max !r2 ( dis coord.(j) (x2,y2) )
    done;
    wynik := min !wynik ( r1 +| !r2 )
  done;
  !wynik;;

let _ =
  Printf.printf "%Ld\n" ( licz () ) ;;

let bs = Scanf.Scanning.stdin
let xint() = Scanf.bscanf bs " %d" (fun x -> x)
let xint32() = Scanf.bscanf bs " %ld" (fun x -> x)
let xint64() = Scanf.bscanf bs " %Ld" (fun x -> x)
let xstr() = Scanf.bscanf bs " %s" (fun x -> x)
let xreal() = Scanf.bscanf bs " %f" (fun x -> x)
let printf = Printf.printf

let ( +| ) = Int64.add
let ( -| ) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div
let ( %| ) = Int64.rem

let () =
  let n = xint() in
  let x1 = xint64() and y1 = xint64() in
  let x2 = xint64() and y2 = xint64() in
  let x = Array.make n 0L and y = Array.make n 0L in
  for i = 0 to n - 1 do
    x.(i) <- xint64();
    y.(i) <- xint64();
  done;

  let sqr x = x *| x in
  let max x y =
    if Int64.compare x y > 0 then x
    else y
  in
  let min x y =
    if Int64.compare x y < 0 then x
    else y
  in

  let ret = ref 9223372036854775807L in
  for i = 0 to n - 1 do
    let r1 = (sqr (x.(i) -| x1)) +| (sqr (y.(i) -| y1)) in
    let r2 = ref 0L in
    for j = 0 to n - 1 do
      let a = (sqr (x.(j) -| x1)) +| (sqr (y.(j) -| y1)) in
      let b = (sqr (x.(j) -| x2)) +| (sqr (y.(j) -| y2)) in
      if Int64.compare a r1 > 0 then (
        r2 := max !r2 b;
      );
    done;
    ret := min !ret (r1 +| !r2);
  done;
  printf "%Ld\n" !ret;


let () =
  let s = read_line() in
  let n = String.length s in
  let b = Array.init n (fun i -> if s.[i]='0' then 0 else 1) in

  let rec loop i c =
    if i=n-1 && c=0 then ()
    else if i=n-1 && c>0 then Printf.printf "%d" b.(i)
    else if b.(i)=0 && c>0 then (Printf.printf "0"; loop (i+1) c)
    else if b.(i)=0 && c=0 then loop (i+1) 1
    else (Printf.printf "1"; loop (i+1) c)
  in

    loop 0 0;
    print_newline()

(* Codeforces 257B SlonMagSquare *)

open String;;

let gr () = Scanf.scanf " %d" (fun i -> i);;
let grf () = Scanf.scanf " %f" (fun i -> i);;
let grl () = Scanf.scanf " %Ld" (fun i -> i);;
let rdln() = input_line Pervasives.stdin;;
(* let spli s = Str.split (Str.regexp " ") s;; *)
let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;
let printarri ai = Array.iter (fun i -> (print_int i; print_string " ")) ai;;
let debug = false;;

let remove_first_zero s =
	let ln = String.length s in
	if not (String.contains s '0') then String.sub s 1 (ln-1)
	else
		let idxz = String.index s '0' in
		let a = String.sub s 0 idxz in
		let b = String.sub s (idxz+1) (ln-idxz-1) in
		a ^ b;;

let main () =
	let s = rdln() in
	let os = remove_first_zero s in
	begin
		print_string os;
		print_newline()
	end;;

main();;


open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let () =
  let n = read_int () in
  let m = read_int () in
  let ne = m-(n-1) in
  let nt = n-1 in

  let e = Array.make ne (0,0) in
  let t = Array.make nt (0,0) in

  let rec read_loop it ie = if it+ie = m then () else
    let a = (read_int(), it+ie) in
    let b = read_int() in
    if b=1 then (
      t.(it) <- a;
      read_loop (it+1) ie
    ) else (
      e.(ie) <- a;
      read_loop it (ie+1)
    )
  in

  read_loop 0 0;

  Array.sort compare t;
  Array.sort compare e;

  let rec gen_pair_list (i,j) k ac = if k = ne then List.rev ac else
      let ac = (i,j)::ac in
      let next = if i+1 = j then (0,j+1) else (i+1,j) in
      gen_pair_list next (k+1) ac
  in

  let pair_array = Array.of_list (gen_pair_list (0,1) 0 []) in

  let rec build_answer i ac = if i=ne then Some (List.rev ac) else
      let (ii,jj) = pair_array.(i) in
      let elength = fst e.(i) in
      if elength < fst t.(jj) then None else
	let ac = (ii,jj)::ac in
	build_answer (i+1) ac
  in

  match build_answer 0 [] with
    | None -> printf "-1\n"
    | Some li ->
      let po = Array.make m (0,0) in
      List.iteri (fun i (ii,jj) ->
	po.(snd e.(i)) <- (ii+2, jj+2)
      ) li;
      for i=0 to nt-1 do
	po.(snd t.(i)) <- (1, i+2)
      done;
      for i=0 to m-1 do
	printf "%d %d\n" (fst po.(i)) (snd po.(i))
      done

let read_int () = Scanf.scanf " %d " (fun x -> x)

let () =
  let n = read_int () in
  let m = read_int () in
  let a = Array.init m (fun i -> let a = read_int () in let b = read_int () in (a, b, i)) in
  let cmp (x1, y1, _) (x2, y2, _) =
    if x1 = x2 then y2 - y1
    else x1 - x2
  in
  let _ = Array.sort cmp a in
  let ans = Array.make m (0, 0) in

  let rec add_extra (extra, extra_cnt) x cur =
    if extra_cnt >= m || x = cur then (extra, extra_cnt)
    else add_extra ((x, cur)::extra, extra_cnt + 1) (x+1) cur
  in

  let process_edge (extra, extra_cnt, cur) (a, b, i) =
    if extra_cnt = (-1) then (extra, extra_cnt, cur)
    else if b = 0 then
      begin
        match extra with
        | [] -> (extra, (-1), cur)
        | hd::tl -> ans.(i) <- hd; (tl, extra_cnt-1, cur)
      end
    else
      begin
        ans.(i) <- (0, cur);
        let (extra, extra_cnt) = add_extra (extra, extra_cnt) 1 cur in
        (extra, extra_cnt, cur + 1)
      end
  in

  let (_, extra_cnt, _) = Array.fold_left process_edge ([], 0, 1) a in
  if extra_cnt = (-1) then Printf.printf "-1\n"
  else Array.iter (fun (a, b) -> Printf.printf "%d %d\n" (a+1) (b+1)) ans

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let () =
  let n = read_int () in
  let m = read_int () in
  let ne = m-(n-1) in
  let nt = n-1 in

  let e = Array.make ne (0,0) in
  let t = Array.make nt (0,0) in

  let rec read_loop it ie = if it+ie = m then () else
    let a = (read_int(), it+ie) in
    let b = read_int() in
    if b=1 then (
      t.(it) <- a;
      read_loop (it+1) ie
    ) else (
      e.(ie) <- a;
      read_loop it (ie+1)
    )
  in

  read_loop 0 0;

  Array.sort compare t;
  Array.sort compare e;

  let rec gen_pair_list (i,j) k ac = if k = ne then ac else
      let ac = (i,j)::ac in
      let next = if i+1 = j then (0,j+1) else (i+1,j) in
      gen_pair_list next (k+1) ac
  in

  let pair_array = Array.of_list (gen_pair_list (0,1) 0 []) in

  let rec build_answer i ac = if i=ne then Some (List.rev ac) else
      let (ii,jj) = pair_array.(i) in
      let elength = fst e.(i) in
      if elength < fst t.(jj) then None else
	let ac = (ii,jj)::ac in
	build_answer (i+1) ac
  in

  match build_answer 0 [] with
    | None -> printf "-1\n"
    | Some li ->
      let po = Array.make m (0,0) in
      List.iteri (fun i (ii,jj) ->
	po.(snd e.(i)) <- (ii+2, jj+2)
      ) li;
      for i=0 to nt-1 do
	po.(snd t.(i)) <- (1, i+2)
      done;
      for i=0 to m-1 do
	printf "%d %d\n" (fst po.(i)) (snd po.(i))
      done

let read_int () = Scanf.scanf " %d " (fun x -> x)

let () =
  let n = read_int () in
  let m = read_int () in
  let a = Array.init m (fun i -> let a = read_int () in let b = read_int () in (a, b, i)) in
  let _ = Array.sort (fun (x1, y1, _) (x2, y2, _) -> (x1*2+1-y1) - (x2*2+1-y2)) a in
  let ans = Array.make m (0, 0) in

  let rec add_extra (extra, extra_cnt) x cur =
    if extra_cnt >= m || x = cur then (extra, extra_cnt)
    else add_extra ((x, cur)::extra, extra_cnt + 1) (x+1) cur
  in

  let process_edge (extra, extra_cnt, cur) (a, b, i) =
    if extra_cnt = (-1) then (extra, extra_cnt, cur)
    else if b = 0 then
      begin
        match extra with
        | [] -> (extra, (-1), cur)
        | hd::tl -> ans.(i) <- hd; (tl, extra_cnt-1, cur)
      end
    else
      begin
        ans.(i) <- (0, cur);
        let (extra, extra_cnt) = add_extra (extra, extra_cnt) 1 cur in
        (extra, extra_cnt, cur + 1)
      end
  in

  let (_, extra_cnt, _) = Array.fold_left process_edge ([], 0, 1) a in
  if extra_cnt = (-1) then Printf.printf "-1\n"
  else Array.iter (fun (a, b) -> Printf.printf "%d %d\n" (a+1) (b+1)) ans

let read_int () = Scanf.scanf " %d " (fun x -> x)

let () =
  let n = read_int () in
  let m = read_int () in
  let a = Array.init m (fun i -> let a = read_int () in let b = read_int () in (a, b, i)) in
  let cmp (x1, y1, _) (x2, y2, _) =
    if x1 = x2 then y1 - y2
    else x1 - x2
  in
  let _ = Array.sort cmp a in
  let ans = Array.make m (0, 0) in

  let rec add_extra (extra, extra_cnt) x cur =
    if extra_cnt >= m || x = cur then (extra, extra_cnt)
    else add_extra ((x, cur)::extra, extra_cnt + 1) (x+1) cur
  in

  let process_edge (extra, extra_cnt, cur) (a, b, i) =
    if extra_cnt = (-1) then (extra, extra_cnt, cur)
    else if b = 0 then
      begin
        match extra with
        | [] -> (extra, (-1), cur)
        | hd::tl -> ans.(i) <- hd; (tl, extra_cnt-1, cur)
      end
    else
      begin
        ans.(i) <- (0, cur);
        let (extra, extra_cnt) = add_extra (extra, extra_cnt) 1 cur in
        (extra, extra_cnt, cur + 1)
      end
  in

  let (_, extra_cnt, _) = Array.fold_left process_edge ([], 0, 1) a in
  if extra_cnt = (-1) then Printf.printf "-1\n"
  else Array.iter (fun (a, b) -> Printf.printf "%d %d\n" (a+1) (b+1)) ans

let read_int () = Scanf.scanf " %d " (fun x -> x)

let () =
  let n = read_int () in
  let m = read_int () in
  let a = Array.init m (fun i -> let a = read_int () in let b = read_int () in (a, b, i)) in
  let _ = Array.sort (fun (x1, _, _) (x2, _, _) -> x1 - x2) a in
  let ans = Array.make m (0, 0) in

  let rec add_extra (extra, extra_cnt) x cur =
    if extra_cnt >= m || x = cur then (extra, extra_cnt)
    else add_extra ((x, cur)::extra, extra_cnt + 1) (x+1) cur
  in

  let process_edge (extra, extra_cnt, cur) (a, b, i) =
    if extra_cnt = (-1) then (extra, extra_cnt, cur)
    else if b = 0 then
      begin
        match extra with
        | [] -> (extra, (-1), cur)
        | hd::tl -> ans.(i) <- hd; (tl, extra_cnt-1, cur)
      end
    else
      begin
        ans.(i) <- (0, cur);
        let (extra, extra_cnt) = add_extra (extra, extra_cnt) 1 cur in
        (extra, extra_cnt, cur + 1)
      end
  in

  let (_, extra_cnt, _) = Array.fold_left process_edge ([], 0, 1) a in
  if extra_cnt = (-1) then Printf.printf "-1\n"
  else Array.iter (fun (a, b) -> Printf.printf "%d %d\n" (a+1) (b+1)) ans


(*************** Ocaml stdio functions **********************)
let stdin_stream = Stream.of_channel stdin;;
let stdout_buffer = Buffer.create 65536;;
let rec gi() =
  match Stream.next stdin_stream with
    '-' ->  - (ri 0)
  | '+' ->  ri 0
  | '0'..'9' as c -> ri ((int_of_char c) - 48)
  | _ -> gi ()
and ri x =
  match Stream.next stdin_stream with
    '0'..'9' as c -> ri (((int_of_char c) - 48) + (x * 10))
  | _ -> x
;;
let flushout () = Buffer.output_buffer stdout stdout_buffer; Buffer.reset stdout_buffer;;
let putchar c =
  if (Buffer.length stdout_buffer) >= 65536 then flushout() else ();
  Buffer.add_char stdout_buffer c;;
let rec ppi i = if i < 10 then putchar (char_of_int (48 + i)) else (ppi (i / 10); putchar (char_of_int (48 + (i mod 10)))) ;;
let pi i = if i >= 0 then ppi i else (putchar ('-'); ppi (-i));;
let pa a = let n = Array.length a in Array.iteri (fun i j -> pi j; putchar (if i == (n-1) then '\n' else ' ')) a;;
(*************** Ocaml solution ******************************)
open Array
let main() =
  let n = gi() in
  let a = init n (fun i -> gi()) in
  let b = make (n+1) 0 in
  iter (fun i -> if (i >= 1 && i <= n) then b.(i) <- 1 else ()) a;
  let s = fold_left (+) 0 b in
  Printf.printf "%d\n" (n - s)
  ;;
main();;
flushout ()

let getint () = Scanf.bscanf Scanf.Scanning.stdin " %d" (fun i->i)
let getstr () = Scanf.bscanf Scanf.Scanning.stdin " %s" (fun s-> s)
let getchar () = Scanf.bscanf Scanf.Scanning.stdin " %c" (fun i->i)

let inf = 1000000007

let _ =
  let n = getint () in
  let a = Array.make n 0 in
  for i = 0 to n-1 do
    let x = getint () - 1 in
    if x<n && x>=0 then a.(x) <- a.(x) + 1;
  done;
  let ans = ref 0 in
  for i = 0 to n-1 do
    if a.(i) = 0 then incr ans
  done;
  Printf.printf "%d\n" !ans

let read_int () = Scanf.scanf " %d " (fun x -> x);;

let n = read_int ();;

let data = Array.init n (fun i -> read_int ());;

Array.sort compare data;;

let rec solve ac i data =
     if i>=n || data.(i)>n then ac
     else if i = 0 && data.(i) >= 1 && data.(i) <= n then solve (ac+1) (i+1) data
     else if i=0 && (data.(i)<1 || data.(i)>n) then solve ac (i+1) data
     else if data.(i) > n then ac
     else if data.(i) < 1 then solve ac (i+1) data
     else if data.(i) != data.(i-1) then solve (ac+1) (i+1) data
     else solve ac (i+1) data;;

Printf.printf "%d\n" (n-(solve 0 0 data));;

let () =
	let n = read_int() in
	let l = ref [] in
	(for i = 1 to n do
		let x = Scanf.scanf " %d" (fun i->i) in
		if x >= 1 && x <= n && not (List.mem x !l) then
			l := x::(!l);
	done; print_int (n-(List.length !l)))
;;

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make 5001 0 in
  let b = ref 0 in
  let c = ref 0 in
    for i = 0 to n - 1 do
      let k = Scanf.bscanf sb "%d " (fun s -> s) in
	a.(k) <- a.(k) + 1
    done;
    for i = 1 to n do
      if a.(i) = 0
      then incr c
      else if a.(i) > 1
      then b := !b + a.(i) - 1
    done;
    for i = n + 1 to 5000 do
      if a.(i) > 0
      then incr b
    done;
    let res = max !b !c in
      Printf.printf "%d\n" res;

let read_int () = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x)

let n = read_int()

let there = Array.make n false

let () =
  for i=0 to n-1 do
    let k = (read_int()) -1 in
      if k>=0 && k<n then there.(k) <- true
  done

let count = ref 0

let () =
  for i=0 to n-1 do
    count := !count + (if there.(i) then 0 else 1)
  done
;;

Printf.printf "%d\n" !count


module RangeSet = Map.Make(
    struct
        type t = int * int (* pairs of indices *)
        let compare (l1, r1) (l2, r2) =
            if r1 < l2 then -1
            else if r2 < l1 then 1
            else 0
    end)

let add elt s = RangeSet.add elt elt s

let () = Scanf.scanf "%d %d\n" (fun n m ->
    let s = Scanf.scanf "%s\n" (fun s -> s) in

    (* build a non-overlapping interval set *)
    let t, _ = List.fold_left (fun (acc, pos) hd ->
        match hd with
        | Str.Text s -> (acc, (pos + String.length s))
        | Str.Delim d ->
                let e = pos + String.length d in
                (add (pos, e - 1) acc, e))
        (RangeSet.empty, 1) (Str.full_split (Str.regexp "\\.+") s) in

    (* calculate the answer to the initial query *)
    let ans = ref (RangeSet.fold (fun _ (l, r) acc -> acc + (r - l)) t 0) in

    (* a query can have the following possible effects:
        * merge two intervals (or extend an interval)
        * split an interval
        * no effect on intervals *)
    let rec loop t i =
        if i <> m then
                Scanf.scanf "%d %c\n" (fun x c ->
                    let t' = match c with
                    | '.' ->
                            (* if this char is in an interval, no effect *)
                            if RangeSet.mem (x, x) t then t else

                            (* join two intervals *)
                            if RangeSet.mem (x-1,x-1) t && RangeSet.mem (x+1, x+1) t then begin
                                let l, _ = RangeSet.find (x-1, x-1) t
                                and _, r = RangeSet.find (x+1, x+1) t in
                                (* calculate new answer *)
                                ans := !ans + 2;
                                (* create new tree *)
                                add (l, r) (RangeSet.remove (x-1, x-1) (RangeSet.remove (x+1, x+1) t))
                            end else

                            (* extend left interval *)
                            if RangeSet.mem (x-1, x-1) t then begin
                                let l, _ = RangeSet.find (x-1, x-1) t in
                                incr ans;
                                add (l, x) (RangeSet.remove (x-1, x-1) t)
                            end else

                            (* extend right interval *)
                            if RangeSet.mem (x+1, x+1) t then begin
                                let _, r = RangeSet.find (x+1, x+1) t in
                                incr ans;
                                add (x, r) (RangeSet.remove (x+1, x+1) t)
                            end else

                            (* create new interval *)
                            add (x, x) t
                    | _ ->
                            (* if this char is in an interval, split it *)
                            if RangeSet.mem (x, x) t then begin
                                let l, r = RangeSet.find (x, x) t
                                and t = RangeSet.remove (x, x) t in
                                (* we take the interval (l, r) to (l,x-1) and (x+1,r)
                                 * check these new intervals are valid and subtract 1 from ans
                                 * for each valid new interval *)
                                let t =
                                    if l < x then begin
                                        decr ans;
                                        add (l, x-1) t
                                    end else t
                                in
                                if r > x then begin
                                    decr ans;
                                    add (x+1, r) t
                                end else t
                            (* otherwise nothing to do *)
                            end else t
                    in
                    Printf.printf "%d\n" !ans;
                    loop t' (i + 1))
    in loop t 0)

let () = Scanf.scanf "%d %d\n" (fun n m ->
    let s = Scanf.scanf "%s\n" (fun s -> s) in
    (* count number of groups in s *)
    let a = Array.init (n + 2) (fun i -> if i = 0 || i = n + 1 then false else s.[i-1] = '.') in

    let rec aux (groups, count) i =
        if i = n then (groups, count)
        else aux (if s.[i] = '.' then
            (groups + (if i = 0 || s.[i - 1] <> '.' then 1 else 0), count + 1)
            else (groups, count))
            (i + 1)
    in

    let g, c = aux (0, 0) 0 in

    let groups = ref g
    and count = ref c in

    for i = 1 to m do
        Scanf.scanf "%d %c\n" (fun x c ->
            (* only do something if we're changing the state *)
            if a.(x) <> (c = '.') then begin
                if c = '.' then begin
                    incr count;

                    (* merge groups *)
                    if a.(x - 1) && a.(x + 1)
                    then decr groups;

                    (* create a new group *)
                    if not (a.(x - 1) || a.(x + 1))
                    then incr groups

                (* check whether we merge or split a group *)
                end else begin
                    decr count;

                    (* split a group *)
                    if a.(x - 1) && a.(x + 1)
                    then incr groups;

                    (* remove a single point *)
                    if not (a.(x - 1) || a.(x + 1))
                    then decr groups
                end;

                a.(x) <- c = '.'
            end;

            Printf.printf "%d\n" (!count - !groups))
    done)


(* took 1:15 to get this *)

(* Invert the permutation, and call it x.  Now the operation is to
   pick an element from the outside and put it somewhere in the
   middle.  Make a set that contains i if x.(i) and x.(i+1) are
   inverted.  Add 0 and n into the set.  Now take the biggest gap
   between neighbors in this set.  n minus this is the answer.

   This is a lower and upper bound on the number of moves needed.
   It's an upper bound because you can clearly do it by building up
   the longest sorted region (i.e. biggest gap) in that many steps.
   It's a lower bound because for each neighboring pair that are
   inverted we must reach (from one end or the other) one of those
   elements.  The answer computed above gives the most efficient way
   to do this.
*)

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let () =
  let n = read_int () in
  let p = Array.init n (fun _ -> read_int() -1) in
  let x = Array.make n 0 in
  for i=0 to n-1 do
    x.(p.(i)) <- i
  done;

  let rec makel i ac = if i=n then n::ac else
      let ac = if x.(i-1) > x.(i) then (i::ac) else ac in
      makel (i+1) ac
  in
  let lg = Array.of_list (List.rev (makel 1 [0])) in
  let nn = Array.length lg in
  let answer = n - maxf 0 (nn-2) (fun i -> lg.(i+1) - lg.(i)) in
  printf "%d\n" answer

let n = read_int() and ind = Array.make 100001 0 and ans = ref 1;;
let a = Array.of_list (List.map (int_of_string) (Str.split (Str.regexp " ") (read_line())));;
for i = 0 to n - 1 do
	if a.(i) ==  1 then Array.set ind 1 1 else
	begin
		Array.set ind a.(i) (ind.(a.(i) - 1) + 1);
		ans := max !ans ind.(a.(i))
end done;;
print_int (n - !ans)

let read_int () = Scanf.scanf " %d " (fun x -> x)

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int ()) in
  let bio = Array.make (n+1) 0 in
  let _ = Array.iter (fun x -> bio.(x) <- bio.(x-1) + 1) a in
  let ans = n - (Array.fold_left max 0 bio) in
  Printf.printf "%d\n" ans

(*
#load "str.cma";;
#load "nums.cma";;
*)
let n = read_int  () and ans = ref 0 and m = ref 99999999999L;;
let a = Array.of_list (List.map (Int64.of_string) (Str.split (Str.regexp " ") (read_line ())));;
for i = n - 1 downto 0 do
	if Int64.compare a.(i) !m < 0 then m := a.(i);
	if Int64.compare a.(i) !m > 0 then ans := !ans + 1;
done;;
print_int !ans;;

let n = read_int  () and ans = ref 0 and m = ref 9999999;;
let a = Array.of_list (List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())));;
for i = n - 1 downto 0 do
	if a.(i) < !m then m := a.(i);
	if a.(i) > !m then ans := !ans + 1;
done;;
print_int !ans

let n = read_int  () and ans = ref 0 and m = ref 999999999L;;
let a = Array.of_list (List.map (Int64.of_string) (Str.split (Str.regexp " ") (read_line ())));;
for i = n - 1 downto 0 do
	if a.(i) < !m then m := a.(i);
	if a.(i) > !m then ans := !ans + 1;
done;;
print_int !ans


let read1 () = Scanf.scanf "%d\n" ( fun x -> x );;
let read2 () = Scanf.scanf "%d %d\n" ( fun x y -> x, y );;
let read3 () = Scanf.scanf "%d %d %d\n" ( fun x y z -> x, y, z);;
let read x = (if x = 0 then Scanf.scanf "%d\n" else Scanf.scanf "%d ") ( fun x -> x );;

let (+$) x y =
  if x > max_int - y then max_int
  else x + y;;

let rec ( *$ ) x y =
  if y = 0 then 0 else
  if y mod 2 = 1 then x *$ (y-1) +$ x
  else let a = x *$ (y/2) in a +$ a;;

let (+|) = Int64.add;;
let (-|) = Int64.sub;;
let ( *| ) = Int64.mul;;
let ( /| ) = Int64.div;;
let two_64 = Int64.one +| Int64.one;;

let licz x = (x-|Int64.one)/|two_64;;

let (n,m) = read2 ();;

let ruch x y = [ (x+1,y); (x-1,y); (x,y+1); (x,y-1) ];;

let czy_dobry x y tab =
  if x < 0 || x >= n || y < 0 || y >= m then false
  else if tab.(x).(y) = '*' then false
  else true;;

let rec bfs ax ay k tab vis fuf size =
  let kol = Queue.create () in
  Queue.add (ax,ay) kol;
  vis.(ax).(ay) <- true;
  while not (Queue.is_empty kol) do
    let (x,y) = Queue.take kol in
    fuf.(x).(y) <- k;
    match k with
    | (a,b) -> size.(a).(b) <- size.(a).(b) + 1;
    List.iter ( fun (a,b) -> if (czy_dobry a b tab && not vis.(a).(b)) then begin vis.(a).(b) <- true; Queue.add (a,b) kol end ) ( ruch x y )
  done;;

let wynik x y tab fuf size =
  let ruchy = List.filter ( fun (x,y) -> czy_dobry x y tab ) (ruch x y) in
  List.fold_left (fun a (x,y) -> a + size.(x).(y)) 0
  (
    List.fold_left (fun a (x,y) -> if
      (List.fold_left (fun a b -> if b = fuf.(x).(y) then false else a ) true a )
      then fuf.(x).(y)::a else a ) [] ruchy
  );;

let _ =
  let tab = Array.make_matrix n m '.'
  and vis = Array.make_matrix n m false
  and fuf = Array.make_matrix n m (0,0)
  and size = Array.make_matrix n m 0 in
  for i=0 to (n-1) do
    let s = Scanf.scanf "%s\n" (fun x -> x) in
    for j=0 to (m-1) do
      tab.(i).(j) <- s.[j];
      fuf.(i).(j) <- (i,j)
    done
  done;
  for i =0 to (n-1) do
    for j=0 to (m-1) do
      if czy_dobry i j tab && not vis.(i).(j) then
      bfs i j (i,j) tab vis fuf size
    done
  done;
  for i=0 to (n-1) do
    for j=0 to (m-1) do
      if tab.(i).(j) = '.' then
        Printf.printf "."
      else Printf.printf "%d" ( ( wynik i j tab fuf size + 1) mod 10 )
    done;
    Printf.printf "\n"
  done;;

let xstr() = Scanf.scanf " %s" (fun x -> x)
let xint() = Scanf.scanf " %d" (fun x -> x)

let () =
  let n = xint() and m = xint() in
  let upp = n * m / 2 + 1 in
  let mark = Array.make_matrix n m ~-1 in
  let cnt = Array.make upp 0 in
  let s = Array.make n "a" in
  for i = 0 to n - 1 do
    s.(i) <- xstr();
  done;
  let qx = Array.make (n * m) 0 in
  let qy = Array.make (n * m) 0 in
  let dx = Array.make 4 0 and dy = Array.make 4 0 in
  dx.(2) <- 1;
  dx.(3) <- ~-1;
  dy.(0) <- 1;
  dy.(1) <- ~-1;
  let idx = ref 0 in
  let bfs sx sy =
    let h = ref 0 and t = ref 1 in
    qx.(0) <- sx;
    qy.(0) <- sy;
    mark.(sx).(sy) <- !idx;
    while !h < !t do
      let x = qx.(!h) and y = qy.(!h) in
      h := !h + 1;
      for i = 0 to 3 do
        let xx = x + dx.(i) in
        let yy = y + dy.(i) in
        if xx >= 0 && xx < n && yy >= 0 && yy < m then (
          if s.(xx).[yy] = '.' && mark.(xx).(yy) = -1 then (
            mark.(xx).(yy) <- !idx;
            qx.(!t) <- xx;
            qy.(!t) <- yy;
            t := !t + 1;
          )
        )
      done;
    done;
    cnt.(!idx) <- !t;
  in

  let gao x y =
    let a = Array.make 4 ~-1 in
    for i = 0 to 3 do
      let xx = x + dx.(i) and yy = y + dy.(i) in
      if xx >= 0 && xx < n && yy >= 0 && yy < m then (
        a.(i) <- mark.(xx).(yy)
      )
    done;
    Array.sort (fun x y -> if x < y then -1 else (if x > y then 1 else 0)) a;
    let sum = ref 1 in
    for i = 0 to 3 do
      if a.(i) != ~-1 then (
        if i = 0 || a.(i) != a.(i - 1) then (
          sum := !sum + cnt.(a.(i))
        )
      )
    done;
    sum := !sum mod 10;
    Char.chr (48 + !sum)
  in

  for i = 0 to n - 1 do
    for j = 0 to m - 1 do
      if s.(i).[j] = '.' && mark.(i).(j) = -1 then (
        bfs i j;
        idx := !idx + 1;
      );
    done;
  done;
  for i = 0 to n - 1 do
    for j = 0 to m - 1 do
      s.(i).[j] <- (if s.(i).[j] = '*' then (gao i j) else '.');
    done;
    Printf.printf "%s\n" s.(i);
  done;

let read1 () = Scanf.scanf "%d\n" ( fun x -> x );;
let read2 () = Scanf.scanf "%d %d\n" ( fun x y -> x, y );;
let read3 () = Scanf.scanf "%d %d %d\n" ( fun x y z -> x, y, z);;
let read x = (if x = 0 then Scanf.scanf "%d\n" else Scanf.scanf "%d ") ( fun x -> x );;

let (+$) x y =
  if x > max_int - y then max_int
  else x + y;;

let rec ( *$ ) x y =
  if y = 0 then 0 else
  if y mod 2 = 1 then x *$ (y-1) +$ x
  else let a = x *$ (y/2) in a +$ a;;

let (+|) = Int64.add;;
let (-|) = Int64.sub;;
let ( *| ) = Int64.mul;;
let ( /| ) = Int64.div;;
let two_64 = Int64.one +| Int64.one;;

let licz x = (x-|Int64.one)/|two_64;;

let (n,m) = read2 ();;

let ruch x y = [ (x+1,y); (x-1,y); (x,y+1); (x,y-1) ];;

let czy_dobry x y tab =
  if x < 0 || x >= n || y < 0 || y >= m then false
  else if tab.(x).(y) = '*' then false
  else true;;

let rec dfs x y k tab vis fuf size =
  if not (czy_dobry x y tab) || vis.(x).(y) then ()
  else begin
  vis.(x).(y) <- true;
  fuf.(x).(y) <- k;
  match k with
  | (a,b) -> size.(a).(b) <- size.(a).(b) + 1;
  List.iter ( fun (a,b) -> dfs a b k tab vis fuf size ) ( ruch x y ) end;;

let wynik x y tab fuf size =
  let ruchy = List.filter ( fun (x,y) -> czy_dobry x y tab ) (ruch x y) in
  List.fold_left (fun a (x,y) -> a + size.(x).(y)) 0
  (
    List.fold_left (fun a (x,y) -> if
      (List.fold_left (fun a b -> if b = fuf.(x).(y) then false else a ) true a )
      then fuf.(x).(y)::a else a ) [] ruchy
  );;

let _ =
  let tab = Array.make_matrix n m '.'
  and vis = Array.make_matrix n m false
  and fuf = Array.make_matrix n m (0,0)
  and size = Array.make_matrix n m 0 in
  for i=0 to (n-1) do
    let s = Scanf.scanf "%s\n" (fun x -> x) in
    for j=0 to (m-1) do
      tab.(i).(j) <- s.[j];
      fuf.(i).(j) <- (i,j)
    done
  done;
  for i =0 to (n-1) do
    for j=0 to (m-1) do
      dfs i j (i,j) tab vis fuf size
    done
  done;
  for i=0 to (n-1) do
    for j=0 to (m-1) do
      if tab.(i).(j) = '.' then
        Printf.printf "."
      else Printf.printf "%d" ( wynik i j tab fuf size + 1)
    done;
    Printf.printf "\n"
  done;;


open Printf open Scanf
module MyInt = struct let (+) = (+) let (-) = (-) let (/) = (/) let ( * ) = ( * ) let (mod) = (mod) let (%) = (mod) let (+=) r v = r := !r + v let (-=) r v = r := !r - v let ( *=) r v= r := !r * v let (/=) r v = r := !r / v let abs v = abs v let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) = (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) let min_int,max_int = min_int,max_int end
module MyInt64 = struct
	let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (+) p q = Int64.add p q let (-) p q = Int64.sub p q let ( * ) p q = Int64.mul p q let (/) p q = Int64.div p q let (+=) r v = r := !r + v let (-=) r v = r := !r - v let ( *=) r v= r := !r * v let (/=) r v = r := !r / v let labs p = if p < 0L then -1L*p else p let (~|) p = Int64.to_int p let (~~|) p = Int64.of_int p let input_i64_array n = Array.init (~| n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v)) let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v) let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v) let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w) let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x) let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y) let print_i64_endline n = n |> Int64.to_string |> print_endline let (mod) m n = m - (m/n) * n let (%) = (mod) let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n) let lcm m n = (m*n) / gcd m n let rep from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_ && !f do match fbod ~~| !i with | `Break -> f := false | _ -> i := !i +$ 1; done let repb from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_ && !f do match fbod (~~| !i) with | `Break -> f := false | _ -> i := !i +$ 1 done; !f let repm from_ to_ m_init fbod = let i,f,m = ref ~|from_,ref true,ref m_init in while !i <= ~|to_ && !f do match fbod !m (~~| !i) with | `Break -> f := false | `Break_m m' -> (f := false; m := m') | `Ok m' -> (i := !i +$ 1; m := m') done; !m let repmb from_ to_ m_init fbod = let i,f,m = ref ~|from_,ref true,ref m_init in while !i <= ~|to_ && !f do match fbod (~~| !i) !m with | `Break -> f := false | `Break_m m' -> (f := false; m := m') | `Ok m' -> (i := !i +$ 1; m := m') done; (!m,!f) let string_of_list ?(separator=" ") f ls = let rec f0 a s = match a with | [] -> s | [h] -> s ^ (f h) ^ separator | h::t -> f0 t (s ^ (f h) ^ separator) in f0 ls "" let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) [] let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
	let (@@@) = (@) let (@) a i = a.(~|i) let (<@) a (i,v) = a.(~|i) <- v
	let ceildiv m n = (m+n-1L) / n
	let abs v = Int64.abs v let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) = let open Int64 in (logand),(logor),(logxor),lognot,(fun u v -> shift_left u (~|v)),(fun u v -> shift_right u (~|v)),(fun u v -> shift_right_logical u (~|v)) let min_int,max_int = Int64.min_int,Int64.max_int let get_string _ = scanf " %s" (fun v -> v)
	let i32,i64 = Int64.to_int,Int64.of_int let alen a = i64 @@ Array.length a
	let dump_array a = a |> Array.to_list |> string_of_list Int64.to_string |> print_endline let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y) let repi from_ to_ fbod = let i,f = ref from_,ref true in while !i <= to_ && !f do match fbod !i with | `Break -> f := false | _ -> i := !i +$ 1 done
end open MyInt64

let repm from_ to_ ?(stride=1) m_init fbod =
	let i,f,m = ref ~|from_,ref true,ref m_init in
	while !i <= ~|to_ && !f do
		match fbod !m (~~| !i) with
		| `Break -> f := false
		| `Break_m m' -> (f := false; m := m')
		| `Ok m' -> (i := !i +$ 1; m := m')
	done; !m

let () =
	let n,m,k = get_3_i64 0 in
	(
		(* (0,0),(p,q),(r,s) -> 2S = |ps-qr| ∈ Z *)
		if 2L*n*m mod k <> 0L then printf "NO\n"
		else (
			printf "YES\n0 0\n";
			let x,y = (
				let n,m,k = ref (2L*n),ref m,ref k in
				while !k <> 1L do
					let g = gcd !n !k in if g > 1L then (
						n := !n / g;
						k := !k / g);
					let g = gcd !m !k in if g > 1L then (
						m := !m / g;
						k := !k / g)
				done; (!n,!m)
			) in
			if x > n && x mod 2L = 0L && y * 2L <= m then (
				printf "%Ld 0\n0 %Ld\n" (x/2L) (y*2L)
			) else printf "%Ld 0\n0 %Ld\n" x y
		)
	)

open Printf open Scanf
module MyInt = struct let (+) = (+) let (-) = (-) let (/) = (/) let ( * ) = ( * ) let (mod) = (mod) let (%) = (mod) let (+=) r v = r := !r + v let (-=) r v = r := !r - v let ( *=) r v= r := !r * v let (/=) r v = r := !r / v let abs v = abs v let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) = (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) let min_int,max_int = min_int,max_int end
module MyInt64 = struct
	let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (+) p q = Int64.add p q let (-) p q = Int64.sub p q let ( * ) p q = Int64.mul p q let (/) p q = Int64.div p q let (+=) r v = r := !r + v let (-=) r v = r := !r - v let ( *=) r v= r := !r * v let (/=) r v = r := !r / v let labs p = if p < 0L then -1L*p else p let (~|) p = Int64.to_int p let (~~|) p = Int64.of_int p let input_i64_array n = Array.init (~| n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v)) let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v) let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v) let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w) let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x) let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y) let print_i64_endline n = n |> Int64.to_string |> print_endline let (mod) m n = m - (m/n) * n let (%) = (mod) let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n) let lcm m n = (m*n) / gcd m n let rep from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_ && !f do match fbod ~~| !i with | `Break -> f := false | _ -> i := !i +$ 1; done let repb from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_ && !f do match fbod (~~| !i) with | `Break -> f := false | _ -> i := !i +$ 1 done; !f let repm from_ to_ m_init fbod = let i,f,m = ref ~|from_,ref true,ref m_init in while !i <= ~|to_ && !f do match fbod !m (~~| !i) with | `Break -> f := false | `Break_m m' -> (f := false; m := m') | `Ok m' -> (i := !i +$ 1; m := m') done; !m let repmb from_ to_ m_init fbod = let i,f,m = ref ~|from_,ref true,ref m_init in while !i <= ~|to_ && !f do match fbod (~~| !i) !m with | `Break -> f := false | `Break_m m' -> (f := false; m := m') | `Ok m' -> (i := !i +$ 1; m := m') done; (!m,!f) let string_of_list ?(separator=" ") f ls = let rec f0 a s = match a with | [] -> s | [h] -> s ^ (f h) ^ separator | h::t -> f0 t (s ^ (f h) ^ separator) in f0 ls "" let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) [] let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
	let (@@@) = (@) let (@) a i = a.(~|i) let (<@) a (i,v) = a.(~|i) <- v
	let ceildiv m n = (m+n-1L) / n
	let abs v = Int64.abs v let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) = let open Int64 in (logand),(logor),(logxor),lognot,(fun u v -> shift_left u (~|v)),(fun u v -> shift_right u (~|v)),(fun u v -> shift_right_logical u (~|v)) let min_int,max_int = Int64.min_int,Int64.max_int let get_string _ = scanf " %s" (fun v -> v)
	let i32,i64 = Int64.to_int,Int64.of_int let alen a = i64 @@ Array.length a
	let dump_array a = a |> Array.to_list |> string_of_list Int64.to_string |> print_endline let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y) let repi from_ to_ fbod = let i,f = ref from_,ref true in while !i <= to_ && !f do match fbod !i with | `Break -> f := false | _ -> i := !i +$ 1 done
end open MyInt64

let repm from_ to_ ?(stride=1) m_init fbod =
	let i,f,m = ref ~|from_,ref true,ref m_init in
	while !i <= ~|to_ && !f do
		match fbod !m (~~| !i) with
		| `Break -> f := false
		| `Break_m m' -> (f := false; m := m')
		| `Ok m' -> (i := !i +$ 1; m := m')
	done; !m

let () =
	let n,m,k = get_3_i64 0 in
	(
		(* (0,0),(p,q),(r,s) -> 2S = |ps-qr|  Z *)
		if 2L*n*m mod k <> 0L then printf "NO\n"
		else (
			printf "YES\n0 0\n";
			let x,y = (
				let n,m,k = ref (2L*n),ref m,ref k in
				while !k <> 1L do
					let g = gcd !n !k in if g > 1L then (
						n := !n / g;
						k := !k / g);
					let g = gcd !m !k in if g > 1L then (
						m := !m / g;
						k := !k / g)
				done; (!n,!m)
			) in
			if x > n && x mod 2L = 0L && y * 2L <= m then (
				printf "%Ld 0\n0 %Ld\n" (x/2L) (y*2L)
			) else printf "%Ld 0\n0 %Ld\n" x y
		)
	)