One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple.Vasya noticed that the yard is a rectangular n × m field. The squares have coordinates (x, y) (1 ≤ x ≤ n, 1 ≤ y ≤ m), where x is the index of the row and y is the index of the column.Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps).A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step.Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made.

The first input line contains two integers n and m (1 ≤ n, m ≤ 109) — the yard's sizes. The second line contains integers xc and yc — the initial square's coordinates (1 ≤ xc ≤ n, 1 ≤ yc ≤ m). The third line contains an integer k (1 ≤ k ≤ 104) — the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≤ 109, |dx| + |dy| ≥ 1).

Print the single number — the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.

NoteIn the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0,  - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps.In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard.

<?php
error_reporting(0);

function steps($n, $N, $vn){
	if ($vn < 0) {
		//$vn = -1 * $vn;
		$vn = abs($vn);
		//return (int)($n - 1) / $vn;
		return (int)(($n - 1) / $vn);
	//} elseif ($vn = 0) {
	} else {
		return (int)(($N - $n) / $vn);
	}
}

function solve(){
	$Scanner = new Scanner("php://stdin");
	//$Scanner = new Scanner("input.txt");

	$inp_n = (int)$Scanner->Scan();
	$inp_m = (int)$Scanner->Scan();
	$inp_xc = (int)$Scanner->Scan();
	$inp_yc = (int)$Scanner->Scan();
	$inp_k = (int)$Scanner->Scan();

	$x = $inp_xc;
	$y = $inp_yc;
	$steps = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_vx = (int)$Scanner->Scan();
		$inp_vy = (int)$Scanner->Scan();

		$x_step = PHP_INT_MAX;//int(2147483647)
		$y_step = PHP_INT_MAX;//int(2147483647)
		if ($inp_vx !== 0)$x_step = steps($x, $inp_n, $inp_vx);
		if ($inp_vy !== 0)$y_step = steps($y, $inp_m, $inp_vy);

		$s = min($x_step, $y_step);
		$steps += $s;
		$x = $x + $inp_vx * $s;
		$y = $y + $inp_vy * $s;
	}
	//printf("%d", $steps);
	printf("%.0f", $steps);
	$Scanner->close();
}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link or ''php://stdin' err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		//err $c_row = array("\n", "\r", "\r\n");
		$c_row = array("\r\n", "\r", "\n");
		$contents = str_replace($c_row, " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
/*
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}
*/
/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php
error_reporting(0);

function is_inner($x, $y, $X, $Y){
	if ((1 <= $x) && ($x <= $X) && (1 <= $y) && ($y <= $Y)) {
		return true;
	} else {
		return false;
	}
}

function steps($n, $N, $vn){
	if ($vn < 0) {
		$vn = -1 * $vn;
		//return (int)($n - 1) / $vn;
		return (int)(($n - 1) / $vn);
	//} elseif ($vn = 0) {
	} elseif ($vn === 0) {
		return -1;
	} else {
		return (int)(($N - $n) / $vn);
	}
}

function solve(){
	$Scanner = new Scanner("php://stdin");
	//$Scanner = new Scanner("input.txt");

	$inp_n = (int)$Scanner->Scan();
	$inp_m = (int)$Scanner->Scan();
	$inp_xc = (int)$Scanner->Scan();
	$inp_yc = (int)$Scanner->Scan();
	$inp_k = (int)$Scanner->Scan();

	$x = $inp_xc;
	$y = $inp_yc;
	$steps = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_vx = (int)$Scanner->Scan();
		$inp_vy = (int)$Scanner->Scan();

		//if(!is_inner($x, $y, $inp_n, $inp_m)) break;
		$x_step = steps($x, $inp_n, $inp_vx);
		$y_step = steps($y, $inp_m, $inp_vy);

		if (($x_step === -1)&&($y_step === -1))continue;

		if ($x_step === -1) {
			$steps += $y_step;
			$y = $y + $inp_vy * $y_step;
		} elseif ($y_step === -1) {
			$steps += $x_step;
			$x = $x + $inp_vx * $x_step;
		} else {
			//$steps += min($x_step, $y_step);
			//$x = $x + $inp_vx * $steps;
			//$y = $y + $inp_vy * $steps;
			$s = min($x_step, $y_step);
			$steps += $s;
			$x = $x + $inp_vx * $s;
			$y = $y + $inp_vy * $s;
		}
	}

	printf("%d", $steps);

	$Scanner->close();

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link or ''php://stdin' err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		//err $c_row = array("\n", "\r", "\r\n");
		$c_row = array("\r\n", "\r", "\n");
		$contents = str_replace($c_row, " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
/*
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}
*/
/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php
error_reporting(0);

function steps($n, $N, $vn){
	if ($vn < 0) {
		$vn = -1 * $vn;
		//return (int)($n - 1) / $vn;
		return (int)(($n - 1) / $vn);
	//} elseif ($vn = 0) {
	} else {
		return (int)(($N - $n) / $vn);
	}
}

function solve(){
	$Scanner = new Scanner("php://stdin");
	//$Scanner = new Scanner("input.txt");

	$inp_n = (int)$Scanner->Scan();
	$inp_m = (int)$Scanner->Scan();
	$inp_xc = (int)$Scanner->Scan();
	$inp_yc = (int)$Scanner->Scan();
	$inp_k = (int)$Scanner->Scan();

	$x = $inp_xc;
	$y = $inp_yc;
	$steps = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_vx = (int)$Scanner->Scan();
		$inp_vy = (int)$Scanner->Scan();

		$x_step = PHP_INT_MAX;
		$y_step = PHP_INT_MAX;;
		//if(!is_inner($x, $y, $inp_n, $inp_m)) break;
		if ($inp_vx !== 0)$x_step = steps($x, $inp_n, $inp_vx);
		if ($inp_vy !== 0)$y_step = steps($y, $inp_m, $inp_vy);

		$s = min($x_step, $y_step);
		$steps += $s;
		$x = $x + $inp_vx * $s;
		$y = $y + $inp_vy * $s;
	}

	printf("%d", $steps);

	$Scanner->close();

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link or ''php://stdin' err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		//err $c_row = array("\n", "\r", "\r\n");
		$c_row = array("\r\n", "\r", "\n");
		$contents = str_replace($c_row, " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
/*
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}
*/
/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php
error_reporting(0);

function steps($n, $N, $vn){
	if ($vn < 0) {
		//$vn = -1 * $vn;
		$vn = abs($vn);
		//return (int)($n - 1) / $vn;
		return (int)(($n - 1) / $vn);
	//} elseif ($vn = 0) {
	} else {
		return (int)(($N - $n) / $vn);
	}
}

function solve(){
	$Scanner = new Scanner("php://stdin");
	//$Scanner = new Scanner("input.txt");

	$inp_n = (int)$Scanner->Scan();
	$inp_m = (int)$Scanner->Scan();
	$inp_xc = (int)$Scanner->Scan();
	$inp_yc = (int)$Scanner->Scan();
	$inp_k = (int)$Scanner->Scan();

	$x = $inp_xc;
	$y = $inp_yc;
	$steps = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$s = 0;
		$inp_vx = (int)$Scanner->Scan();
		$inp_vy = (int)$Scanner->Scan();

		$x_step = PHP_INT_MAX;//int(2147483647)
		$y_step = PHP_INT_MAX;//int(2147483647)
		if ($inp_vx !== 0)$x_step = steps($x, $inp_n, $inp_vx);
		if ($inp_vy !== 0)$y_step = steps($y, $inp_m, $inp_vy);

		$s = min($x_step, $y_step);
		$steps += $s;
		$x = $x + $inp_vx * $s;
		$y = $y + $inp_vy * $s;
	}

	printf("%d", $steps);

	$Scanner->close();

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link or ''php://stdin' err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		//err $c_row = array("\n", "\r", "\r\n");
		$c_row = array("\r\n", "\r", "\n");
		$contents = str_replace($c_row, " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
/*
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}
*/
/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php
error_reporting(0);

function steps($n, $N, $vn){
	if ($vn < 0) {
		//$vn = -1 * $vn;
		$vn = abs($vn);
		//return (int)($n - 1) / $vn;
		return (int)(($n - 1) / $vn);
	//} elseif ($vn = 0) {
	} else {
		return (int)(($N - $n) / $vn);
	}
}

function solve(){
	$Scanner = new Scanner("php://stdin");
	//$Scanner = new Scanner("input.txt");

	$inp_n = (int)$Scanner->Scan();
	$inp_m = (int)$Scanner->Scan();
	$inp_xc = (int)$Scanner->Scan();
	$inp_yc = (int)$Scanner->Scan();
	$inp_k = (int)$Scanner->Scan();

	$x = $inp_xc;
	$y = $inp_yc;
	$steps = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_vx = (int)$Scanner->Scan();
		$inp_vy = (int)$Scanner->Scan();

		$x_step = PHP_INT_MAX;//int(2147483647)
		$y_step = PHP_INT_MAX;//int(2147483647)
		if ($inp_vx !== 0)$x_step = steps($x, $inp_n, $inp_vx);
		if ($inp_vy !== 0)$y_step = steps($y, $inp_m, $inp_vy);

		if ($steps < 0) print $inp_vx;
		$s = min($x_step, $y_step);
		$steps += $s;
		$x = $x + $inp_vx * $s;
		$y = $y + $inp_vy * $s;
	}

	printf("%d", $steps);

	$Scanner->close();

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link or ''php://stdin' err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		//err $c_row = array("\n", "\r", "\r\n");
		$c_row = array("\r\n", "\r", "\n");
		$contents = str_replace($c_row, " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
/*
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}
*/
/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php
error_reporting(0);

function steps($n, $N, $vn){
	if ($vn < 0) {
		//$vn = -1 * $vn;
		$vn = abs($vn);
		//return (int)($n - 1) / $vn;
		return (int)(($n - 1) / $vn);
	//} elseif ($vn = 0) {
	} else {
		return (int)(($N - $n) / $vn);
	}
}

function solve(){
	//$Scanner = new Scanner("php://stdin");
	$Scanner = new Scanner("input.txt");

	$inp_n = (int)$Scanner->Scan();
	$inp_m = (int)$Scanner->Scan();
	$inp_xc = (int)$Scanner->Scan();
	$inp_yc = (int)$Scanner->Scan();
	$inp_k = (int)$Scanner->Scan();

	$x = $inp_xc;
	$y = $inp_yc;
	$steps = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_vx = (int)$Scanner->Scan();
		$inp_vy = (int)$Scanner->Scan();

		$x_step = PHP_INT_MAX;//int(2147483647)
		$y_step = PHP_INT_MAX;//int(2147483647)
		if ($inp_vx !== 0)$x_step = steps($x, $inp_n, $inp_vx);
		if ($inp_vy !== 0)$y_step = steps($y, $inp_m, $inp_vy);

		$s = min($x_step, $y_step);
		$steps += $s;
		$x = $x + $inp_vx * $s;
		$y = $y + $inp_vy * $s;
	}
	//printf("%d", $steps);
	printf("%.0f", $steps);
	$Scanner->close();
}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link or ''php://stdin' err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		//err $c_row = array("\n", "\r", "\r\n");
		$c_row = array("\r\n", "\r", "\n");
		$contents = str_replace($c_row, " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
/*
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}
*/
/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php
error_reporting(0);

function steps($n, $N, $vn){
	if ($vn < 0) {
		$vn = -1 * $vn;
		//return (int)($n - 1) / $vn;
		return (int)(($n - 1) / $vn);
	//} elseif ($vn = 0) {
	} else {
		return (int)(($N - $n) / $vn);
	}
}

function solve(){
	//$Scanner = new Scanner("php://stdin");
	$Scanner = new Scanner("input.txt");

	$inp_n = (int)$Scanner->Scan();
	$inp_m = (int)$Scanner->Scan();
	$inp_xc = (int)$Scanner->Scan();
	$inp_yc = (int)$Scanner->Scan();
	$inp_k = (int)$Scanner->Scan();

	$x = $inp_xc;
	$y = $inp_yc;
	$steps = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_vx = (int)$Scanner->Scan();
		$inp_vy = (int)$Scanner->Scan();

		$x_step = PHP_INT_MAX;
		$y_step = PHP_INT_MAX;;
		//if(!is_inner($x, $y, $inp_n, $inp_m)) break;
		if ($inp_vx !== 0)$x_step = steps($x, $inp_n, $inp_vx);
		if ($inp_vy !== 0)$y_step = steps($y, $inp_m, $inp_vy);

		$s = min($x_step, $y_step);
		$steps += $s;
		$x = $x + $inp_vx * $s;
		$y = $y + $inp_vy * $s;
	}

	printf("%d", $steps);

	$Scanner->close();

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link or ''php://stdin' err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		//err $c_row = array("\n", "\r", "\r\n");
		$c_row = array("\r\n", "\r", "\n");
		$contents = str_replace($c_row, " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
/*
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}
*/
/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php
error_reporting(0);

function steps($n, $N, $vn){
	if ($vn < 0) {
		//$vn = -1 * $vn;
		$vn = abs($vn);
		//return (int)($n - 1) / $vn;
		return (int)(($n - 1) / $vn);
	//} elseif ($vn = 0) {
	} else {
		return (int)(($N - $n) / $vn);
	}
}

function solve(){
	//$Scanner = new Scanner("php://stdin");
	$Scanner = new Scanner("input.txt");

	$inp_n = (int)$Scanner->Scan();
	$inp_m = (int)$Scanner->Scan();
	$inp_xc = (int)$Scanner->Scan();
	$inp_yc = (int)$Scanner->Scan();
	$inp_k = (int)$Scanner->Scan();

	$x = $inp_xc;
	$y = $inp_yc;
	$steps = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_vx = (int)$Scanner->Scan();
		$inp_vy = (int)$Scanner->Scan();

		$x_step = PHP_INT_MAX;//int(2147483647)
		$y_step = PHP_INT_MAX;//int(2147483647)
		if ($inp_vx !== 0)$x_step = steps($x, $inp_n, $inp_vx);
		if ($inp_vy !== 0)$y_step = steps($y, $inp_m, $inp_vy);

		$s = min($x_step, $y_step);
		$steps += $s;
		$x = $x + $inp_vx * $s;
		$y = $y + $inp_vy * $s;
	}

	printf("%d", $steps);

	$Scanner->close();

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link or ''php://stdin' err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		//err $c_row = array("\n", "\r", "\r\n");
		$c_row = array("\r\n", "\r", "\n");
		$contents = str_replace($c_row, " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
/*
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}
*/
/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php
error_reporting(0);

function is_inner($x, $y, $X, $Y){
	if ((1 <= $x) && ($x <= $X) && (1 <= $y) && ($y <= $Y)) {
		return true;
	} else {
		return false;
	}
}

function steps($n, $N, $vn){
	if ($vn < 0) {
		$vn = -1 * $vn;
		//return (int)($n - 1) / $vn;
		return (int)(($n - 1) / $vn);
	} elseif ($vn === 0) {
		return -1;
	} else {
		return (int)(($N - $n) / $vn);
	}
}

function solve(){
	$Scanner = new Scanner("php://stdin");
	//$Scanner = new Scanner("input.txt");

	$inp_n = (int)$Scanner->Scan();
	$inp_m = (int)$Scanner->Scan();
	$inp_xc = (int)$Scanner->Scan();
	$inp_yc = (int)$Scanner->Scan();
	$inp_k = (int)$Scanner->Scan();

	$x = $inp_xc;
	$y = $inp_yc;
	$steps = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_vx = (int)$Scanner->Scan();
		$inp_vy = (int)$Scanner->Scan();

		//if(!is_inner($x, $y, $inp_n, $inp_m)) break;
		$x_step = steps($x, $inp_n, $inp_vx);
		$y_step = steps($y, $inp_m, $inp_vy);

		if ($x_step === -1) {
			$steps += $y_step;
			$y = $y + $inp_vy * $y_step;
		} elseif ($y_step === -1) {
			$steps += $x_step;
			$x = $x + $inp_vx * $x_step;
		} else {
			//$steps += min($x_step, $y_step);
			//$x = $x + $inp_vx * $steps;
			//$y = $y + $inp_vy * $steps;
			$s = min($x_step, $y_step);
			$steps += $s;
			$x = $x + $inp_vx * $s;
			$y = $y + $inp_vy * $s;
		}
	}

	printf("%d", $steps);

	$Scanner->close();

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link or ''php://stdin' err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		//err $c_row = array("\n", "\r", "\r\n");
		$c_row = array("\r\n", "\r", "\n");
		$contents = str_replace($c_row, " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
/*
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}
*/
/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php
error_reporting(0);

function steps($n, $N, $vn){
	if ($vn < 0) {
		$vn = -1 * $vn;
		//return (int)($n - 1) / $vn;
		return (int)(($n - 1) / $vn);
	//} elseif ($vn = 0) {
	} else {
		return (int)(($N - $n) / $vn);
	}
}

function solve(){
	$Scanner = new Scanner("php://stdin");
	//$Scanner = new Scanner("input.txt");

	$inp_n = (int)$Scanner->Scan();
	$inp_m = (int)$Scanner->Scan();
	$inp_xc = (int)$Scanner->Scan();
	$inp_yc = (int)$Scanner->Scan();
	$inp_k = (int)$Scanner->Scan();

	$x = $inp_xc;
	$y = $inp_yc;
	$steps = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_vx = (int)$Scanner->Scan();
		$inp_vy = (int)$Scanner->Scan();

		$x_step = -1;
		$y_step = -1;
		//if(!is_inner($x, $y, $inp_n, $inp_m)) break;
		if ($inp_vx !== 0)$x_step = steps($x, $inp_n, $inp_vx);
		if ($inp_vy !== 0)$y_step = steps($y, $inp_m, $inp_vy);

		//if (($x_step === -1)&&($y_step === -1))continue;

		if ($x_step === -1) {
			$steps += $y_step;
			$y = $y + $inp_vy * $y_step;
		} elseif ($y_step === -1) {
			$steps += $x_step;
			$x = $x + $inp_vx * $x_step;
		} else {
			//$steps += min($x_step, $y_step);
			//$x = $x + $inp_vx * $steps;
			//$y = $y + $inp_vy * $steps;
			$s = min($x_step, $y_step);
			$steps += $s;
			$x = $x + $inp_vx * $s;
			$y = $y + $inp_vy * $s;
		}
	}

	printf("%d", $steps);

	$Scanner->close();

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link or ''php://stdin' err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		//err $c_row = array("\n", "\r", "\r\n");
		$c_row = array("\r\n", "\r", "\n");
		$contents = str_replace($c_row, " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
/*
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}
*/
/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php
error_reporting(0);

function steps($n, $N, $vn){
	if ($vn < 0) {
		//$vn = -1 * $vn;
		$vn = abs($vn);
		//return (int)($n - 1) / $vn;
		return (int)(($n - 1) / $vn);
	//} elseif ($vn = 0) {
	} else {
		return (int)(($N - $n) / $vn);
	}
}

function solve(){
	$Scanner = new Scanner("php://stdin");
	//$Scanner = new Scanner("input.txt");

	$inp_n = (int)$Scanner->Scan();
	$inp_m = (int)$Scanner->Scan();
	$inp_xc = (int)$Scanner->Scan();
	$inp_yc = (int)$Scanner->Scan();
	$inp_k = (int)$Scanner->Scan();

	$x = $inp_xc;
	$y = $inp_yc;
	$steps = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_vx = (int)$Scanner->Scan();
		$inp_vy = (int)$Scanner->Scan();

		$x_step = PHP_INT_MAX;//int(2147483647)
		$y_step = PHP_INT_MAX;//int(2147483647)
		if ($inp_vx !== 0)$x_step = steps($x, $inp_n, $inp_vx);
		if ($inp_vy !== 0)$y_step = steps($y, $inp_m, $inp_vy);

		$s = min($x_step, $y_step);
		$steps += $s;
		$x = $x + $inp_vx * $s;
		$y = $y + $inp_vy * $s;
	}

	printf("%d", $steps);

	$Scanner->close();

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link or ''php://stdin' err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		//err $c_row = array("\n", "\r", "\r\n");
		$c_row = array("\r\n", "\r", "\n");
		$contents = str_replace($c_row, " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
/*
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}
*/
/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php
error_reporting(0);

function is_inner($x, $y, $X, $Y){
	if ((1 <= $x) && ($x <= $X) && (1 <= $y) && ($y <= $Y)) {
		return true;
	} else {
		return false;
	}
}

function steps($n, $N, $vn){
	if ($vn < 0) {
		$vn = -1 * $vn;
		//return (int)($n - 1) / $vn;
		return (int)(($n - 1) / $vn);
	//} elseif ($vn = 0) {
	} elseif ($vn === 0) {
		return -1;
	} else {
		return (int)(($N - $n) / $vn);
	}
}

function solve(){
	$Scanner = new Scanner("php://stdin");
	//$Scanner = new Scanner("input.txt");

	$inp_n = (int)$Scanner->Scan();
	$inp_m = (int)$Scanner->Scan();
	$inp_xc = (int)$Scanner->Scan();
	$inp_yc = (int)$Scanner->Scan();
	$inp_k = (int)$Scanner->Scan();

	$x = $inp_xc;
	$y = $inp_yc;
	$steps = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_vx = (int)$Scanner->Scan();
		$inp_vy = (int)$Scanner->Scan();

		if(!is_inner($x, $y, $inp_n, $inp_m)) break;
		$x_step = steps($x, $inp_n, $inp_vx);
		$y_step = steps($y, $inp_m, $inp_vy);

		if ($x_step === -1) {
			$steps += $y_step;
			$y = $y + $inp_vy * $y_step;
		} elseif ($y_step === -1) {
			$steps += $x_step;
			$x = $x + $inp_vx * $x_step;
		} else {
			//$steps += min($x_step, $y_step);
			//$x = $x + $inp_vx * $steps;
			//$y = $y + $inp_vy * $steps;
			$s = min($x_step, $y_step);
			$steps += $s;
			$x = $x + $inp_vx * $s;
			$y = $y + $inp_vy * $s;
		}
	}

	printf("%d", $steps);

	$Scanner->close();

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link or ''php://stdin' err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		//err $c_row = array("\n", "\r", "\r\n");
		$c_row = array("\r\n", "\r", "\n");
		$contents = str_replace($c_row, " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
/*
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}
*/
/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php
error_reporting(0);

function steps($n, $N, $vn){
	if ($vn < 0) {
		//$vn = -1 * $vn;
		$vn = abs($vn);
		//return (int)($n - 1) / $vn;
		return (int)(($n - 1) / $vn);
	//} elseif ($vn = 0) {
	} else {
		return (int)(($N - $n) / $vn);
	}
}

function solve(){
	$Scanner = new Scanner("php://stdin");
	//$Scanner = new Scanner("input.txt");

	$inp_n = (int)$Scanner->Scan();
	$inp_m = (int)$Scanner->Scan();
	$inp_xc = (int)$Scanner->Scan();
	$inp_yc = (int)$Scanner->Scan();
	$inp_k = (int)$Scanner->Scan();

	$x = $inp_xc;
	$y = $inp_yc;
	$steps = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_vx = (int)$Scanner->Scan();
		$inp_vy = (int)$Scanner->Scan();

		$x_step = PHP_INT_MAX;
		$y_step = PHP_INT_MAX;;
		//if(!is_inner($x, $y, $inp_n, $inp_m)) break;
		if ($inp_vx !== 0)$x_step = steps($x, $inp_n, $inp_vx);
		if ($inp_vy !== 0)$y_step = steps($y, $inp_m, $inp_vy);

		$s = min($x_step, $y_step);
		$steps += $s;
		$x = $x + $inp_vx * $s;
		$y = $y + $inp_vy * $s;
	}

	printf("%d", $steps);

	$Scanner->close();

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link or ''php://stdin' err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		//err $c_row = array("\n", "\r", "\r\n");
		$c_row = array("\r\n", "\r", "\n");
		$contents = str_replace($c_row, " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
/*
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}
*/
/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php
error_reporting(0);

function is_inner($x, $y, $X, $Y){
	if ((1 <= $x) && ($x <= $X) && (1 <= $y) && ($y <= $Y)) {
		return true;
	} else {
		return false;
	}
}

function steps($n, $N, $vn){
	if ($vn < 0) {
		$vn = -1 * $vn;
		//return (int)($n - 1) / $vn;
		return (int)(($n - 1) / $vn);
	} elseif ($vn === 0) {
		return -1;
	} else {
		return (int)(($N - $n) / $vn);
	}
}

function solve(){
	//$Scanner = new Scanner("php://stdin");
	$Scanner = new Scanner("input.txt");

	$inp_n = (int)$Scanner->Scan();
	$inp_m = (int)$Scanner->Scan();
	$inp_xc = (int)$Scanner->Scan();
	$inp_yc = (int)$Scanner->Scan();
	$inp_k = (int)$Scanner->Scan();

	$x = $inp_xc;
	$y = $inp_yc;
	$steps = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_vx = (int)$Scanner->Scan();
		$inp_vy = (int)$Scanner->Scan();

		//if(!is_inner($x, $y, $inp_n, $inp_m)) break;
		$x_step = steps($x, $inp_n, $inp_vx);
		$y_step = steps($y, $inp_m, $inp_vy);

		if ($x_step === -1) {
			$steps += $y_step;
			$y = $y + $inp_vy * $y_step;
		} elseif ($y_step === -1) {
			$steps += $x_step;
			$x = $x + $inp_vx * $x_step;
		} else {
			//$steps += min($x_step, $y_step);
			//$x = $x + $inp_vx * $steps;
			//$y = $y + $inp_vy * $steps;
			$s = min($x_step, $y_step);
			$steps += $s;
			$x = $x + $inp_vx * $s;
			$y = $y + $inp_vy * $s;
		}
	}

	printf("%d", $steps);

	$Scanner->close();

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link or ''php://stdin' err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		//err $c_row = array("\n", "\r", "\r\n");
		$c_row = array("\r\n", "\r", "\n");
		$contents = str_replace($c_row, " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
/*
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}
*/
/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php
error_reporting(0);

function steps($n, $N, $vn){
	if ($vn < 0) {
		//$vn = -1 * $vn;
		$vn = abs($vn);
		//return (int)($n - 1) / $vn;
		return (int)(($n - 1) / $vn);
	//} elseif ($vn = 0) {
	} else {
		return (int)(($N - $n) / $vn);
	}
}

function solve(){
	$Scanner = new Scanner("php://stdin");
	//$Scanner = new Scanner("input.txt");

	$inp_n = (int)$Scanner->Scan();
	$inp_m = (int)$Scanner->Scan();
	$inp_xc = (int)$Scanner->Scan();
	$inp_yc = (int)$Scanner->Scan();
	$inp_k = (int)$Scanner->Scan();

	$x = $inp_xc;
	$y = $inp_yc;
	$steps = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_vx = (int)$Scanner->Scan();
		$inp_vy = (int)$Scanner->Scan();

		$x_step = PHP_INT_MAX;//int(2147483647)
		$y_step = PHP_INT_MAX;//int(2147483647)
		if ($inp_vx !== 0)$x_step = steps($x, $inp_n, $inp_vx);
		if ($inp_vy !== 0)$y_step = steps($y, $inp_m, $inp_vy);

		$s = min($x_step, $y_step);
		$steps += $s;
		if ($steps < 0) print $inp_vx;
		$x = $x + $inp_vx * $s;
		$y = $y + $inp_vy * $s;
	}

	printf("%d", $steps);

	$Scanner->close();

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link or ''php://stdin' err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		//err $c_row = array("\n", "\r", "\r\n");
		$c_row = array("\r\n", "\r", "\n");
		$contents = str_replace($c_row, " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
/*
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}
*/
/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php
error_reporting(0);

function is_inner($x, $y, $X, $Y){
	if ((1 <= $x) && ($x <= $X) && (1 <= $y) && ($y <= $Y)) {
		return true;
	} else {
		return false;
	}
}

function solve(){
	//$Scanner = new Scanner("php://stdin");
	$Scanner = new Scanner("input.txt");

	$inp_n = $Scanner->Scan();
	$inp_m = $Scanner->Scan();
	$inp_xc = $Scanner->Scan();
	$inp_yc = $Scanner->Scan();
	$inp_k = $Scanner->Scan();

	$x = $inp_xc;
	$y = $inp_yc;
	$steps = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_vx = $Scanner->Scan();
		$inp_vy = $Scanner->Scan();

		while (is_inner($x, $y, $inp_n, $inp_m)) {
			$x += $inp_vx;
			$y += $inp_vy;
			$steps++;
		}
		$x -= $inp_vx;
		$y -= $inp_vy;
		$steps--;
	}

	printf("%d", $steps);

	$Scanner->close();

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link or ''php://stdin' err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		//err $c_row = array("\n", "\r", "\r\n");
		$c_row = array("\r\n", "\r", "\n");
		$contents = str_replace($c_row, " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
/*
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if((!is_file($opend) && !is_link($opend)) && $opend !== "php://stdin"){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}
*/
/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>


A class of students wrote a multiple-choice test.There are $$$n$$$ students in the class. The test had $$$m$$$ questions, each of them had $$$5$$$ possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question $$$i$$$ worth $$$a_i$$$ points. Incorrect answers are graded with zero points.The students remember what answers they gave on the exam, but they don't know what are the correct answers. They are very optimistic, so they want to know what is the maximum possible total score of all students in the class.

The first line contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) — the number of students in the class and the number of questions in the test. Each of the next $$$n$$$ lines contains string $$$s_i$$$ ($$$|s_i| = m$$$), describing an answer of the $$$i$$$-th student. The $$$j$$$-th character represents the student answer (A, B, C, D or E) on the $$$j$$$-th question. The last line contains $$$m$$$ integers $$$a_1, a_2, \ldots, a_m$$$ ($$$1 \le a_i \le 1000$$$) — the number of points for the correct answer for every question.

Print a single integer — the maximum possible total score of the class.

NoteIn the first example, one of the most optimal test answers is "ABCD", this way the total number of points will be $$$16$$$.In the second example, one of the most optimal test answers is "CCC", this way each question will be answered by exactly one student and the total number of points is $$$5 + 4 + 12 = 21$$$.

<?php

    $line = fgets(STDIN);
    $line = preg_split('/\s+/', $line);

    $n = $line[0];
    $m = $line[1];

    $answers = [];
    for ($i=0; $i<$n; $i++) {
        $answers[$i] = fgets(STDIN);
    }
    $marks = fgets(STDIN);
    $marks = preg_split('/\s+/', $marks);

    $max = 0;
    for ($j=0; $j<$m; $j++) {
        $similar_answers = [
            'A' => 0,
            'B' => 0,
            'C' => 0,
            'D' => 0,
            'E' => 0
        ];
        for ($i=0; $i<$n; $i++) {
            $similar_answers[$answers[$i][$j]]++;
        }
        $max += max($similar_answers) * $marks[$j];
    }

    print($max);

<?php
list($n, $m) = explode(' ', trim(fgets(STDIN)));
$aArray = [];
for($i = 0; $i<$n; $i++){
    $aArray[$i] = trim(fgets(STDIN));
}
$aAnswer = explode(" ", trim(fgets(STDIN)));
$iCount = 0;
$aTest = [];

for($j = 0; $j< $m; $j++){
    for($i = 0; $i<$n; $i++){
        if(!isset($aTest[$j][$aArray[$i][$j]])){
            $aTest[$j][$aArray[$i][$j]] = 1;
        } else {
            $aTest[$j][$aArray[$i][$j]] = $aTest[$j][$aArray[$i][$j]] + 1;
        }
    }
}
$i = 0;
foreach ($aTest as $sK => $aV){
    $iCount += max($aV) * $aAnswer[$i];
    $i++;
    if($i == $m){
        break;
    }
}
echo $iCount;

<?php
fscanf(STDIN,"%d %d",$students,$questions);
for ($i=0; $i <$students ; $i++)
 {
	$answers[$i]=trim(fgets(STDIN));
 }
$marks=explode(" ",trim(fgets(STDIN)));
$sum=0;
for ($i=0; $i <$questions ; $i++)
 {
    $A=0;
    $B=0;
    $C=0;
    $D=0;
    $E=0;

  for ($j=0; $j <$students ; $j++)
    {
	  if($answers[$j][$i]=='A')
	  	   $A++;
	  elseif($answers[$j][$i]=='B')
	  	   $B++;
	  elseif ($answers[$j][$i]=='C')
	  	    $C++;
	  elseif ($answers[$j][$i]=='D')
	  	    $D++;
	  else
	  	    $E++;

	}
	$sum+=$marks[$i]*max($A,$B,$C,$D,$E);

}
echo "$sum\n";

<?php

list($n, $m) = explode(' ', trim(fgets(STDIN)));

$results = [];
$corrects = [];

for ($i = 0; $i < $n; $i++) {
    $results[$i] = trim(fgets(STDIN));
}

$corrects = explode(' ', trim(fgets(STDIN)));

// Solution

$ansCount = [];

for($j = 0; $j < $m; $j++) {
    for($i = 0; $i < $n; $i++) {
        if(!isset($ansCount[$j][$results[$i][$j]])){
            $ansCount[$j][$results[$i][$j]] = 1;
        } else {
            $ansCount[$j][$results[$i][$j]]++;
        }
    }
}

$return = 0;
foreach ($ansCount as $k => $ans) {
    $return += $corrects[$k] * max($ans);
}

echo $return . PHP_EOL;

<?php
list($n, $m) = explode(' ', trim(fgets(STDIN)));
$aArray = [];
for($i = 0; $i<$n; $i++){
    $aArray[$i] = trim(fgets(STDIN));
}
$aAnswer = explode(" ", trim(fgets(STDIN)));
$iCount = 0;
for($i = 1; $i<$n; $i++){
    for($j = 0; $j< $m; $j++){
        if($aArray[0][$j] == $aArray[$i][$j]){
            $iCount += $aAnswer[$j];
        }
    }
}

echo $iCount + array_sum($aAnswer);

<?php
fscanf(STDIN,"%d %d",$students,$questions);
for ($i=0; $i <$students ; $i++)
 {
	$answers[$i]=trim(fgets(STDIN));
 }
$marks=explode(" ",trim(fgets(STDIN)));
$sum=0;
for ($i=0; $i <$questions ; $i++)
 {
    $A=0;
    $B=0;
    $C=0;
    $D=0;
    $E=0;

  for ($j=0; $j <$students ; $j++)
    {
	  if($answers[$j][$i]=='A')
	  	   $A++;
	  elseif($answers[$j][$i]=='B')
	  	   $B++;
	  elseif ($answers[$j][$i]=='C')
	  	    $C++;
	  elseif ($answers[$j][$i]=='D')
	  	    $D++;
	  else
	  	    $E++;

	}
	$sum+=$marks[$i]*max($A,$B,$C,$D);

}
echo "$sum\n";

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n, m;
	cin >> n >> m;
	string s[1005];
	for(int i = 1; i <= n; i++){
		cin >> s[i];
	}
	int a[1005], b[6];
	for(int i = 0; i < m; i++){
		cin >> a[i];
	}
	int ans = 0;
	for(int i = 0; i < m; i++){
		memset(b, 0, sizeof(b));
		for(int j = 1; j <= n; j++){
			b[s[j][i]-'A']++;
		}
		sort(b, b+5);
		ans = ans + b[4] * a[i];
	}
	cout << ans <<endl;
	return 0;
}

<?php

list($n, $m) = explode(' ', readline());

$results = [];
$corrects = [];

for ($i = 0; $i < $n; $i++) {
    $results[] = str_split(readline());
}

$corrects = explode(' ', readline());

// Solution

$ansCount = [];

foreach ($results as $result) {
    foreach ($result as $k => $let) {
        if (empty($ansCount[$k][$let])) {
            $ansCount[$k][$let] = 0;
            $ansCount[$k][$let]++;
        } else {
            $ansCount[$k][$let]++;
        }
    }
}

$return = 0;
foreach ($ansCount as $k => $ans) {
    sort($ans);

    $return += $corrects[$k] * $ans[0];
}

echo $return . PHP_EOL;