<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
$e = $c[0];
$f = $d[0];
$g = array_slice($c, 1);
$h = array_slice($d, 1);
$i = range(1, $a);
$j = range(1, $b);
for($x = 0; $x <= 10000; $x++)
{
    $k = $x % $a;
    $l = $x % $b;
    $m = in_array($k, $g);
    $n = in_array($l, $h);
    if(($m == TRUE) && ($n == FALSE))
    {
        array_push($h, $l);
    }
    elseif(($m == FALSE) && ($n == TRUE))
    {
        array_push($g, $k);
    }
}
if((count($g) == $a) && (count($h) == $b))
{
    print "Yes";
}
else
{
    print "No";
}
?>

<?php
//$file = fopen('input.in', 'r');
$file = STDIN;
list($bn, $gn) = explode(" ", trim(fgets($file)));
$gcd = gcd($bn, $gn);
$bArray = explode(" ", trim(fgets($file)));
$rArray = array();
for ($i = 1; $i <= $bArray[0]; $i++) {
    $rArray[$bArray[$i]%$gcd] = 1;
}
$gArray = explode(" ", trim(fgets($file)));
for ($i = 1; $i <= $gArray[0]; $i++) {
    $rArray[$gArray[$i]%$gcd] = 1;
}
$result = 1;
for ($i = 0; $i < $gcd; $i++) {
    if (!isset($rArray[$i])) {
        $result = 0;
        break;
    }
}
echo $result ? "Yes" : "No";

function gcd($a, $b)
{
    return $b == 0 ? $a : gcd($b, $a % $b);
}

<?php
//$file = fopen('input.in', 'r');
$file = STDIN;
list($bn, $gn) = explode(" ", trim(fgets($file)));
$gcd = gcd($bn, $gn);
$biArray = explode(" ", trim(fgets($file)));
$bArray = array();
for ($i = 1; $i <= $biArray[0]; $i++) {
    $bArray[$biArray[$i]%$gcd] = 1;
}
$giArray = explode(" ", trim(fgets($file)));
$gArray = array();
for ($i = 1; $i <= $giArray[0]; $i++) {
    $gArray[$giArray[$i]%$gcd] = 1;
}
$result = 1;
for ($i = 0; $i < $gcd; $i++) {
    if (!isset($bArray[$i]) && !isset($gArray[$i])) {
        $result = 0;
        break;
    }
}
echo $result ? "Yes" : "No";

function gcd($a, $b)
{
    return $b == 0 ? $a : gcd($b, $a % $b);
}

<?php
list($n, $m) = explode(" ", trim(fgets(STDIN)));
$bh = array_slice(explode(" ", trim(fgets(STDIN))),1);
$hb = array();
foreach($bh as $happyBoy) {
	$hb[$happyBoy] = 1;
}
$gh = array_slice(explode(" ", trim(fgets(STDIN))),1);
$hg = array();
foreach($gh as $happyGirl) {
	$hg[$happyGirl] = 1;
}

$b2g = array();
$lcm = lcm($m,$n);
$found = false;
for($i =0; $i < $lcm; $i++) {
	$b = $i % $n;
	$g = $i % $m;
	if(!isset($b2g[$b])) {
		$b2g[$b] = array();
	}
	$b2g[$b][$g] = 1;
	if(isset($hb[$b]) || (isset($hg[$g]))) {
		$hb[$b] = $hg[$g] = 1;
	}
	if(count($hb) == $n || count($hg) == $m) {
		echo "Yes";
		$found = true;
		break;
	}
}
if(!$found) {
	echo "No";
}

function lcm($a,$b){

    $temp = $a;

    while(1){
         if($temp % $b == 0 && $temp % $a == 0)
             break;
         $temp++;
    }

   return $temp;
}
?>

<?php
//$file = fopen('input.in', 'r');
$file = STDIN;
list($bn, $gn) = explode(" ", trim(fgets($file)));
$biArray = explode(" ", trim(fgets($file)));
$bArray = array();
for ($i = 1; $i <= $biArray[0]; $i++) {
    $bArray[$biArray[$i]] = 1;
}
$giArray = explode(" ", trim(fgets($file)));
for ($i = 1; $i <= $giArray[0]; $i++) {
    $gArray[$giArray[$i]] = 1;
}
$gcd = gcd($bn, $gn);
$result = 1;
for ($i = 0; $i < $gcd; $i++) {
    $r = 0;
    for ($j = $i; $j < ($bn / $gcd) + $i; $j += $gcd) {
        if (isset($bArray[$j])) {
            $r = 1;
            break;
        }
    }
    if ($r == 1) {
        continue;
    }
    for ($j = $i; $j < ($gn / $gcd) + $i; $j += $gcd) {
        if (isset($gArray[$j])) {
            $r = 1;
            break;
        }
    }
    if ($r != 1) {
        $result = 0;
        break;
    }
}
echo $result? "Yes":"No";
function gcd($a, $b)
{
    return $b == 0 ? $a : gcd($b, $a % $b);
}


<?php
/**
 * Created by PhpStorm.
 * User: jianzjzhang
 * Date: 2018/5/13
 * Time: 17:45
 */

function transformDorm($room_number, $letter_number)
{
    $current_dorm = 1;
    $reduced = 0;

    $result = array();

    for($i=0; $i<count($letter_number); $i++){
        $x = $letter_number[$i];

        if($x - $reduced <= $room_number[$current_dorm-1]){
            $result[] = $current_dorm;  //push f -- the dormitory number
            $result[] = $x - $reduced;  //push k -- the room number
        }
        else{
            $reduced += $room_number[$current_dorm-1];
            $current_dorm++;
            $i--;
        }
    }

    return $result;
}

function printResult($arr)
{
    $i = 0;
    while($i < count($arr)){
        echo $arr[$i++].' '.$arr[$i++]."\n";
    }
}

function main()
{
    list($n, $m) = explode(" ", stream_get_line(STDIN, 4096, "\n"));

    $room_number = array();
    $letter_number = array();
    for($i=0; $i<intval($n)-1; $i++){
        $room_number[] = stream_get_line(STDIN, 4096, " ");
    }
    $room_number[] = stream_get_line(STDIN, 4096, "\n");

    for($i=0; $i<intval($m)-1; $i++){
        $letter_number[] = stream_get_line(STDIN, 4096, " ");
    }
    $letter_number[] = stream_get_line(STDIN, 4096, "\n");

    printResult(transformDorm($room_number, $letter_number));
}

main();

<?php

$handle = fopen("php://stdin", "r");

function deliverPost($sizes, $posts)
{
    $currentPos = 0;
    $prev = 0;
    $sum = $sizes[$currentPos];
    $prev = $sum;

    foreach ($posts as $app) {

        while($app > $sum)
        {
            $currentPos++;
            $prev = $sizes[$currentPos];
            $sum += $sizes[$currentPos];
        }

        echo $currentPos + 1, ' ', ($app - ($sum - $prev)), PHP_EOL;

    }

}

fscanf($handle, "%i  %i", $n, $a);

$sizes = array_map('doubleval', explode(' ', trim(fgets($handle))));
$posts = array_map('doubleval', explode(' ', trim(fgets($handle))));

deliverPost($sizes, $posts);

<?php

$handle = fopen("php://stdin", "r");

function deliverPost($sizes, $posts)
{
    foreach ($posts as $app) {

        for ($i = 0; $i < count($sizes); $i++) {

            if ($app <= $sizes[$i]) {
                echo $i + 1, ' ', $app, PHP_EOL;
                break;
            } else {
                $app -= $sizes[$i];
            }
        }
    }

}

fscanf($handle, "%i  %i", $n, $a);

$sizes = array_map('intval', explode(' ', fgets($handle)));

$posts = array_map('intval', explode(' ', fgets($handle)));

deliverPost($sizes, $posts);

<?php

$handle = fopen("php://stdin", "r");

function deliverPost($sizes, $posts)
{
    foreach ($posts as $app) {

        for ($i = 0; $i < count($sizes); $i++) {

            if ($app <= $sizes[$i]) {
                echo $i + 1, ' ', $app, PHP_EOL;
                break;
            } else {
                $app -= $sizes[$i];
            }
        }
    }

}

fscanf($handle, "%i  %i", $n, $a);

$sizes = array_map('doubleval', explode(' ', trim(fgets($handle))));
$posts = array_map('doubleval', explode(' ', trim(fgets($handle))));

if ($n == 2 && $a == 3) {
    var_dump($sizes, $posts);
}

deliverPost($sizes, $posts);

<?php

$handle = fopen("php://stdin", "r");

function deliverPost($sizes, $posts)
{
    var_dump($sizes, $posts);

    foreach ($posts as $app) {

        var_dump($app);

        for ($i = 0; $i < count($sizes); $i++) {

            var_dump($sizes[$i]);

            if ($app <= $sizes[$i]) {
                echo $i + 1, ' ', $app, PHP_EOL;
                break;
            } else {
                $app -= $sizes[$i];
            }
        }
    }

}

fscanf($handle, "%i  %i", $n, $a);

$sizes = array_map('intval', explode(' ', trim(fgets($handle))));
$posts = array_map('intval', explode(' ', trim(fgets($handle))));

deliverPost($sizes, $posts);

<?php

$handle = fopen("php://stdin", "r");

function deliverPost($sizes, $posts)
{
    foreach ($posts as $app) {

        for ($i = 0; $i < count($sizes); $i++) {

            if ($app <= $sizes[$i]) {
                echo $i + 1, ' ', $app, PHP_EOL;
                break;
            } else {
                $app -= $sizes[$i];
            }
        }
    }

}

fscanf($handle, "%i  %i", $n, $m);

$sizes = explode(' ', fgets($handle));
$posts = explode(' ', fgets($handle));

deliverPost($sizes, $posts);

<?php

$handle = fopen("php://stdin", "r");

function deliverPost($sizes, $posts)
{
    if (in_array(10000000000, $sizes)) {

        var_dump($sizes, $posts);
    }

    foreach ($posts as $app) {

        for ($i = 0; $i < count($sizes); $i++) {

            if ($app <= $sizes[$i]) {
                echo $i + 1, ' ', $app, PHP_EOL;
                break;
            } else {
                $app -= $sizes[$i];
            }
        }
    }

}

fscanf($handle, "%i  %i", $n, $a);

$sizes = array_map('intval', explode(' ', trim(fgets($handle))));
$posts = array_map('intval', explode(' ', trim(fgets($handle))));

deliverPost($sizes, $posts);

<?php
/**
 * Created by PhpStorm.
 * User: jianzjzhang
 * Date: 2018/5/13
 * Time: 17:45
 */

function transformDorm($room_number, $letter_number)
{
    $current_dorm = 1;
    $reduced = 0;

    $result = array();

    for($i=0; $i<count($letter_number); $i++){
        $x = $letter_number[$i];

        if($x - $reduced <= $room_number[$current_dorm-1]){
            $result[] = $current_dorm;  //push f -- the dormitory number
            $result[] = $x - $reduced;  //push k -- the room number
        }
        else{
            $reduced += $room_number[$current_dorm-1];
            $current_dorm++;
            $i--;
        }
    }

    return $result;
}

function printResult($arr)
{
    $i = 0;
    while($i < count($arr)){
        echo $arr[$i++].' '.$arr[$i++]."\n";
    }
}

function main()
{
    list($n, $m) = explode(" ", stream_get_line(STDIN, 4096, "\n"));

    $room_number = array();
    $letter_number = array();
    for($i=0; $i<intval($n)-1; $i++){
        $room_number[] = intval(stream_get_line(STDIN, 4096, " "));
    }
    $room_number[] = intval(stream_get_line(STDIN, 4096, "\n"));

    for($i=0; $i<intval($m)-1; $i++){
        $letter_number[] = intval(stream_get_line(STDIN, 4096, " "));
    }
    $letter_number[] = intval(stream_get_line(STDIN, 4096, "\n"));

    printResult(transformDorm($room_number, $letter_number));
}

main();

<?php
/**
 * Created by PhpStorm.
 * User: jianzjzhang
 * Date: 2018/5/13
 * Time: 17:45
 */

function transformDorm($room_number, $letter_number)
{
    $current_dorm = 1;
    $reduced = 0;

    $result = array();

    for($i=0; $i<count($letter_number); $i++){
        $x = $letter_number[$i];

        if($x - $reduced <= $room_number[$current_dorm-1]){
            $result[] = $current_dorm;  //push f -- the dormitory number
            $result[] = $x - $reduced;  //push k -- the room number
        }
        else{
            $reduced += $room_number[$current_dorm-1];
            $current_dorm++;
            $i--;
        }
    }

    return $result;
}

function printResult($arr)
{
    $i = 0;
    while($i < count($arr)){
        echo $arr[$i++].' '.$arr[$i++]."\n";
    }
}

function main()
{
    list($n, $m) = explode(" ", fgets(STDIN));

    $room_number = array();
    $letter_number = array();
    for($i=0; $i<$n-1; $i++){
        $room_number[] = intval(stream_get_line(STDIN, 4096, " "));
    }
    $room_number[] = intval(stream_get_line(STDIN, 4096, "\n"));

    for($i=0; $i<$m-1; $i++){
        $letter_number[] = intval(stream_get_line(STDIN, 4096, " "));
    }
    $letter_number[] = intval(stream_get_line(STDIN, 4096, "\n"));

    printResult(transformDorm($room_number, $letter_number));
}

main();

<?php

$handle = fopen("php://stdin", "r");

function deliverPost($sizes, $posts)
{
    foreach ($posts as $app) {

        for ($i = 0; $i < count($sizes); $i++) {

            if ($app <= $sizes[$i]) {
                echo $i + 1, ' ', $app, PHP_EOL;
                break;
            } else {
                $app -= $sizes[$i];
            }
        }
    }

}

fscanf($handle, "%i  %i", $n, $a);

$sizes = explode(' ', fgets($handle));
$posts = explode(' ', fgets($handle));

deliverPost($sizes, $posts);

<?php

$handle = fopen("php://stdin", "r");

function deliverPost($sizes, $posts)
{
    foreach ($posts as $app) {

        for ($i = 0; $i < count($sizes); $i++) {

            if ($app <= $sizes[$i]) {
                echo $i + 1, ' ', $app, PHP_EOL;
                break;
            } else {
                $app -= $sizes[$i];
            }
        }
    }

}

fscanf($handle, "%i  %i", $n, $a);

$sizes = array_map('intval', explode(' ', trim(fgets($handle))));
$posts = array_map('intval', explode(' ', trim(fgets($handle))));

deliverPost($sizes, $posts);

<?php

$handle = fopen("php://stdin", "r");

function deliverPost($sizes, $posts)
{
    foreach ($posts as $app) {

        for ($i = 0; $i < count($sizes); $i++) {

            if ($app <= $sizes[$i]) {
                echo $i + 1, ' ', $app, PHP_EOL;
                break;
            } else {
                $app -= $sizes[$i];
            }
        }
    }

}

fscanf($handle, "%i  %i", $n, $a);

$sizes = array_map('intval', explode(' ', trim(fgets($handle))));
$posts = array_map('intval', explode(' ', trim(fgets($handle))));

if ($n == 2 && $a == 3) {
    var_dump($sizes, $posts);
}

deliverPost($sizes, $posts);

<?php

$handle = fopen("php://stdin", "r");

function deliverPost($sizes, $posts)
{
    foreach ($posts as $app) {

        for ($i = 0; $i < count($sizes); $i++) {

            if ($app <= $sizes[$i]) {
                echo $i + 1, ' ', $app, PHP_EOL;
                break;
            } else {
                $app -= $sizes[$i];
            }
        }
    }

}

fscanf($handle, "%i  %i", $n, $m);

$sizes = explode(' ', fgets($handle));
$posts = explode(' ', fgets($handle));

/*$sizes = explode(' ', '10 15 12');
$posts = explode(' ', '25 26 27 28 29 30');*/

deliverPost($sizes, $posts);

<?php

$handle = fopen("php://stdin", "r");

function deliverPost($sizes, $posts)
{
    foreach ($posts as $app) {

        for ($i = 0; $i < count($sizes); $i++) {

            if ($app <= $sizes[$i]) {
                echo $i + 1, ' ', $app, PHP_EOL;
                break;
            } else {
                $app -= $sizes[$i];
            }
        }
    }

}
/*
fscanf($handle, "%i  %i", $n, $m);

$sizes = explode(' ', fgets($handle));
$posts = explode(' ', fgets($handle));*/

$sizes = explode(' ', '10 15 12');
$posts = explode(' ', '25 26 27 28 29 30');

deliverPost($sizes, $posts);


<?php
list($n, $m)=explode(' ', trim(fgets(STDIN)));
$a=explode(' ', trim(fgets(STDIN)));
sort($a, SORT_NUMERIC);
$i=0;
$r=0;
while($i<$m) {
if($a[$i]<0) $r+=-$a[$i];
$i++;
}
echo $r;
?>

<?
fscanf(STDIN,'%d %d',$n,$m);
$price=explode(' ',trim(fgets(STDIN)));
sort($price,SORT_NUMERIC);
for($i=0;$i<$m;$i++){
    if($price[$i]<0)
    $res+=$price[$i];
}
printf('%.0f',abs($res));
?>

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = 0;
sort($c);
for($x = 0; $x < $b; $x++)
{
    $d += abs($c[$x]);
}
print $d;
?>

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = 0;
sort($c);
for($x = 0; $x < $b; $x++)
{
    $d += abs($c[$x]);
}
if($c[0] < 0)
{
    print $d;
}
else
{
    print "0";
}
?>

<?
fscanf(STDIN,'%d %d',$n,$m);
$price=explode(' ',trim(fgets(STDIN)));
sort($price,SORT_NUMERIC);
for($i=0;$i<$m;$i++){
    $res+=$price[$i];
}
printf('%.0f',$res);
?>

<?
fscanf(STDIN,'%d %d',$n,$m);
$price=explode(' ',trim(fgets(STDIN)));
sort($price,SORT_NUMERIC);
for($i=0;$i<$m;$i++){
    $res+=$price[$i];
}
function ires($res){
    if($res<0) return abs($res);
    else return $res*(-1);
}
$res=ires($res);
printf('%.0f',$res);
?>

<?
fscanf(STDIN,'%d %d',$n,$m);
$price=explode(' ',trim(fgets(STDIN)));
sort($price,SORT_NUMERIC);
for($i=0;$i<$m;$i++){
    $res+=abs($price[$i]);
}
printf('%.0f',$res);
?>


<?php

$config = [
    6,
    2 => '1',
    5,
    5,
    4,
    5,
    6,
    3 => '7',
    7,
    6,
];
$count = trim(fgets(STDIN));
for ($i = 0; $i < $count; $i++) {
    $number = trim(fgets(STDIN));
    test($number);
}

function test($number) {
    if ($number % 2 == 0) {
        $result = '';
        for ($j = 0; $j < $number / 2; $j++) {
            $result .= '1';
        }
    } else {
        $result = '';
        if ($number >= 3) {
            $result .= '7';
        }
        for ($j = 0; $j < $number / 2 - 2; $j++) {
            $result .= '1';
        }
    }
    echo $result . PHP_EOL;
}

<?
$stdin = fopen("php://stdin", "r");
$t = fgets($stdin);

while($t > 0)
{
    $n = fgets($stdin);

    $ones = floor($n / 2);
    if ($n % 2 == 1)
    {
        echo "7";
        $ones -= 1;
    }

    while($ones > 0)
    {
        echo "1";
        $ones -= 1;
    }
    echo "\n";

    $t -= 1;
}

fclose($stdin);
?>

<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     $b = trim(fgets(STDIN));
     if($b % 2 == 0)
     {
          $c = $b / 2;
          $d = array_fill(0, $c, 1);
          print implode($d) . "\n";
     }
     else
     {
          $c = floor($b / 2);
          $d = array_fill(0, $c - 1, 1);
          print "7" . implode($d) . "\n";
     }
}
?>

<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     $b = trim(fgets(STDIN));
     if($b % 2 == 0)
     {
          $c = $b / 2;
          $d = array_fill(0, $c, 1);
          print implode($d) . "\n";
     }
     else
     {
          $c = floor($b / 2);
          $d = array_fill(0, $c - 1, 1);
          print implode($d) . "7\n";
     }
}
?>

<?php

$config = [
    6,
    2 => '1',
    5,
    5,
    4,
    5,
    6,
    3 => '7',
    7,
    6,
];
$count = trim(fgets(STDIN));
for ($i = 0; $i < $count; $i++) {
    $number = trim(fgets(STDIN));
    test($number);
}

function test($number) {
    if ($number % 2 == 0) {
        $result = '';
        for ($j = 0; $j < $number / 2; $j++) {
            $result .= '1';
        }
    } else {
        $result = '';
        for ($j = 0; $j < $number / 2 - 2; $j++) {
            $result .= '1';
        }
        $result .= '7';
    }
    echo $result;
}

<?php

$config = [
    6,
    2 => '1',
    5,
    5,
    4,
    5,
    6,
    3 => '7',
    7,
    6,
];
$count = trim(fgets(STDIN));
for ($i = 0; $i < $count; $i++) {
    $number = trim(fgets(STDIN));
    test($number);
}

function test($number) {
    if ($number % 2 == 0) {
        $result = '';
        for ($j = 0; $j < $number / 2; $j++) {
            $result .= '1';
        }
    } else {
        $result = '';
        for ($j = 0; $j < $number / 2 - 2; $j++) {
            $result .= '1';
        }
        $result .= '7';
    }
    echo $result . PHP_EOL;
}

$count = trim(fgets(STDIN));
for ($i = 0; $i < $count; $i++) {
    $number = trim(fgets(STDIN));
    if ($number % 2 == 0) {
        $result = '';
        for ($j = 0; $j < $number / 2; $j++) {
            $result .= '1';
        }
    } else {
        $result = '';
        for ($j = 0; $j < $number / 2 - 1; $j++) {
            $result .= '1';
        }
        $result .= '7';
    }
    return $result;
}

<?php

$config = [
    6,
    2 => '1',
    5,
    5,
    4,
    5,
    6,
    3 => '7',
    7,
    6,
];
$count = trim(fgets(STDIN));
for ($i = 0; $i < $count; $i++) {
    $number = trim(fgets(STDIN));
    test($number);
}

function test($number) {
    if ($number % 2 == 0) {
        $result = '';
        for ($j = 0; $j < $number / 2; $j++) {
            $result .= '1';
        }
    } else {
        $result = '';
        if ($number > 3) {
            $result .= '7';
        }
        for ($j = 0; $j < $number / 2 - 2; $j++) {
            $result .= '1';
        }
    }
    echo $result . PHP_EOL;
}


<?php
$cntInput = fgets(STDIN);
$arInput =  explode(' ',trim(fgets(STDIN)));
for($i=0;$i<$cntInput;$i++){
    for($k=$i+1;$k<$cntInput;$k++){
        $rkey = array_search($arInput[$i]+$arInput[$k],$arInput);
        if ($rkey!==false){
            echo ($rkey+1).' '.($i+1).' '.($k+1);
            exit;
        }
    }
}
echo -1;

?>

<?php
$cntInput = fgets(STDIN);
$arInput =  explode(' ',trim(fgets(STDIN)));
for($i=0;$i<$cntInput;$i++){
    for($k=$i+1;$k<$cntInput;$k++){
        $rkey = array_search($arInput[$i]+$arInput[$k],$arInput);
        if ($rkey>0){
            echo ($rkey+1).' '.($i+1).' '.($k+1);
            exit;
        }
    }
}
echo -1;

?>

<?php
$cntInput = fgets(STDIN);
$arInput =  explode(' ',trim(fgets(STDIN)));
for($i=0;$i<$cntInput;$i++){
    for($k=$i+1;$k<$cntInput;$k++){
        $rkey = array_search($arInput[$i]+$arInput[$k],$arInput);
        if ($rkey>0){
            echo $i.' '.$k.' '.$rkey;
            exit;
        }
    }
}
echo -1;

?>


<?php
// $stdin = fopen('input.txt', 'r');

$n = intval(trim(fgets(STDIN)));
$f = explode(' ', trim(fgets(STDIN)));

$pull1 = array_fill(1, $n, 0);
for ($i = 0; $i < $n; $i++){
	$f[$i] = intval($f[$i]);
	if($f[$i] != 0){
		$pull1[$f[$i]] = 1;
	}
}

$pull2 = [];
$pull3 = [];
for ($i = 1; $i <= $n; $i++){
	if($pull1[$i] == 0){
		if($f[$i-1] == 0)
			$pull2[] = $i;
		else
			$pull3[] = $i;
	}
}
$pull = array_merge(array_reverse($pull2),$pull3);

$pullIndex = 0;

//echo implode(' ', $f).' / '.implode(' ', $pull)."\n";

for ($i = 0; $i < $n; $i++){
	if($f[$i] == 0){
		if($pull[$pullIndex] == $i + 1){
			$f[$i] = $pull[$pullIndex + 1];
			$pull[$pullIndex + 1] = $pull[$pullIndex];
		} else {
			$f[$i] = $pull[$pullIndex];
		}
		$pullIndex++;
//		echo implode(' ', $f).' / '.implode(' ', $pull)."\n";
	}
}

echo implode(' ', $f);

<?php
// $stdin = fopen('input.txt', 'r');

$n = intval(trim(fgets(STDIN)));
$f = explode(' ', trim(fgets(STDIN)));

$pull1 = array_fill(1, $n-1, 0);
for ($i = 0; $i < $n; $i++){
	$f[$i] = intval($f[$i]);
	if($f[$i] != 0){
		$pull1[$f[$i]] = 1;
	}
}

$pull = [];
for ($i = 1; $i <= $n; $i++){
	if($pull1[$i] == 0){
		$pull[] = $i;
	}
}

$pullIndex = count($pull)-1;

for ($i = 0; $i < $n; $i++){
	if($f[$i] == 0){
		if($pull[$pullIndex] == $i + 1){
			$f[$i] = $pull[$pullIndex - 1];
			$pull[$pullIndex - 1] = $pull[$pullIndex];
		} else {
			$f[$i] = $pull[$pullIndex];
		}
		$pullIndex--;
	}
}

echo implode(' ', $f);

<?php
// $stdin = fopen('input.txt', 'r');

$n = intval(trim(fgets(STDIN)));
$f = explode(' ', trim(fgets(STDIN)));

$pull1 = array_fill(1, $n, 0);
for ($i = 0; $i < $n; $i++){
	$f[$i] = intval($f[$i]);
	if($f[$i] != 0){
		$pull1[$f[$i]] = 1;
	}
}

$pull2 = [];
$pull3 = [];
for ($i = 1; $i <= $n; $i++){
	if($pull1[$i] == 0){
		if($f[$i-1] == 0)
			$pull2[] = $i;
		else
			$pull3[] = $i;
	}
}
$pull = array_merge(array_reverse($pull2),$pull3);

$pullIndex = 0;

echo implode(' ', $f).' / '.implode(' ', $pull)."\n";

for ($i = 0; $i < $n; $i++){
	if($f[$i] == 0){
		if($pull[$pullIndex] == $i + 1){
			$f[$i] = $pull[$pullIndex + 1];
			$pull[$pullIndex + 1] = $pull[$pullIndex];
		} else {
			$f[$i] = $pull[$pullIndex];
		}
		$pullIndex++;
		echo implode(' ', $f).' / '.implode(' ', $pull)."\n";
	}
}

echo implode(' ', $f);

<?php
// $stdin = fopen('input.txt', 'r');

$n = intval(trim(fgets(STDIN)));
$f = explode(' ', trim(fgets(STDIN)));

$pull1 = array_fill(1, $n, 0);
for ($i = 0; $i < $n; $i++){
	$f[$i] = intval($f[$i]);
	if($f[$i] != 0){
		$pull1[$f[$i]] = 1;
	}
}

$pull2 = [];
$pull3 = [];
for ($i = 1; $i <= $n; $i++){
	if($pull1[$i] == 0){
		if($f[$i] == 0)
			$pull2[] = $i;
		else
			$pull3[] = $i;
	}
}
$pull = array_merge($pull2,$pull3);

$pullIndex = 0;

for ($i = 0; $i < $n; $i++){
	if($f[$i] == 0){
		if($pull[$pullIndex] == $i + 1){
			$f[$i] = $pull[$pullIndex + 1];
			$pull[$pullIndex + 1] = $pull[$pullIndex];
		} else {
			$f[$i] = $pull[$pullIndex];
		}
		$pullIndex++;
	}
}

echo implode(' ', $f);

<?php
// $stdin = fopen('input.txt', 'r');

$n = intval(trim(fgets(STDIN)));

$f = explode(' ', trim(fgets(STDIN)));
for ($i = 1; $i <= $n; $i++){
	$f[$i] = intval($f[$i]);
}

$pull = [];

for ($i = 1; $i <= $n; $i++){
	if(array_search($i, $f, true) === false){
		$pull[] = $i;
	}
}

$pullIndex = count($pull)-1;

for ($i = 0; $i < $n; $i++){
	if($f[$i] == 0){
		if($pull[$pullIndex] == $i + 1){
			$f[$i] = $pull[$pullIndex - 1];
			$pull[$pullIndex - 1] = $pull[$pullIndex];
		} else {
			$f[$i] = $pull[$pullIndex];
		}
		$pullIndex--;
	}
}

echo implode(' ', $f);

<?php
// $stdin = fopen('input.txt', 'r');

$n = intval(trim(fgets(STDIN)));

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

$pull = [];

for ($i = 1; $i <= $n; $i++){
	if(array_search($i, $f, true) === false){
		$pull[] = $i;
	}
}

$pullIndex = count($pull)-1;

for ($i = 0; $i < $n; $i++){
	if($f[$i] == 0){
		if($pull[$pullIndex] == $i + 1){
			$f[$i] = $pull[$pullIndex - 1];
			$pull[$pullIndex - 1] = $pull[$pullIndex];
		} else {
			$f[$i] = $pull[$pullIndex];
		}
		$pullIndex--;
	}
}

echo implode(' ', $f);

<?php
// $stdin = fopen('input.txt', 'r');

$n = intval(trim(fgets(STDIN)));
$f = explode(' ', trim(fgets(STDIN)));

$pull1 = array_fill(1, $n, 0);
for ($i = 0; $i < $n; $i++){
	$f[$i] = intval($f[$i]);
	if($f[$i] != 0){
		$pull1[$f[$i]] = 1;
	}
}

$pull2 = [];
$pull3 = [];
for ($i = 1; $i <= $n; $i++){
	if($pull1[$i] == 0){
		if($f[$i-1] == 0)
			$pull2[] = $i;
		else
			$pull3[] = $i;
	}
}
$pull = array_merge($pull2,$pull3);

$pullIndex = 0;

for ($i = 0; $i < $n; $i++){
	if($f[$i] == 0){
		if($pull[$pullIndex] == $i + 1){
			$f[$i] = $pull[$pullIndex + 1];
			$pull[$pullIndex + 1] = $pull[$pullIndex];
		} else {
			$f[$i] = $pull[$pullIndex];
		}
		$pullIndex++;
	}
}

echo implode(' ', $f);


<?php
$a = trim(fgets(STDIN));
$b = array();
$c = array();
$d = array();
for($x = 1; $x <= $a * $a; $x++)
{
    list($e, $f) = explode(" ", trim(fgets(STDIN)));
    if(($b[$e] == FALSE) && ($c[$f] == FALSE))
    {
        array_push($d, $x);
        $b[$e] = TRUE;
        $c[$f] = TRUE;
    }
}
print implode(" ", $d);
?>

<?php
//$file = fopen('input.in', 'r');
$file = STDIN;
$n = trim(fgets($file));
$k = $n * $n;
$hArray = array();
$vArray = array();
for ($i = 1; $i <= $k; $i++) {
    list($x, $y) = explode(" ", trim(fgets($file)));
    if (isset($hArray[$x]) || isset($vArray[$y])) {
        continue;
    }
    $hArray[$x] = 1;
    $vArray[$y] = 1;
    echo $i . " ";
}

<?
	define("_", PHP_EOL);
	$MAX_N = PHP_INT_MAX;

	$N = trim(fgets(STDIN));
	$b = $c = $d = [];

	for($i = 1; $i < $N * $N + 1; $i++) {
		list($v, $h) = explode(" ", trim(fgets(STDIN)));
		if(!$b[$v] && !$c[$h]) {
	        array_push($d, $i);
	        $b[$v] = $c[$h] = true;
	    }
	}

	print implode(" ", $d);
?>

<?
class Codeforces{
  var $in = STDIN;
  public function solve(){
    $n = trim(fgets($this->in));
    for($r = 1;$r <= $n*$n; $r++){
      list($i,$j) = explode(' ', trim(fgets($this->in)));
      if($vari[$i] == 0 && $varj[$j] == 0)
      {
        $vari[$i] = 1;
        $varj[$j] = 1;
        echo "$r ";
      }
    }
  }
}
$c = new Codeforces();
$c -> solve();


<?php
$a = trim(fgets(STDIN));
$b = array();
$c = array();
$d = array(0);
for($x = 0; $x < $a; $x++)
{
     list($e, $f) = explode(" ", trim(fgets(STDIN)));
     $g = $d[count($d) - 1];
     $h = floor(($g - $e) / $f);
     if($h < 0)
     {
          $h = 0;
     }
     while(TRUE)
     {
          $i = $e + $f * $h;
          if($i > $d[count($d) - 1])
          {
               $d[count($d)] = $i;
               break;
          }
          else
          {
               $h++;
          }
     }
}
unset($d[0]);
sort($d);
print $d[count($d) - 1];
?>

<?php
$line1 = trim(fgets(STDIN));
$n = $line1;
$i = 1;
$day = 0;
while($line1 > 0){
    $str = explode(' ', trim(fgets(STDIN)));
    if($day == 0){
        $day = $str[0];
    }else{
        $l = 1;
        while(($day + $l - $str[0]) % $str[1] != 0 || ($day + $l - $str[0]) < 0){

            $l++;
        }
        $day = $day + $l;
    }
    $i++;
    $line1--;
}
echo $day;

<?php
$a = trim(fgets(STDIN));
$b = array();
$c = array();
$d = array(0);
for($x = 0; $x < $a; $x++)
{
     list($e, $f) = explode(" ", trim(fgets(STDIN)));
     $g = $d[count($d) - 1];
     $h = floor(($g - $e) / $f);
     if($h < 0)
     {
          $h = 0;
     }
     while(TRUE)
     {
          $i = $e + $f * $h;
          if($i > $d[count($d) - 1])
          {
               $d[count($d)] = $i;
               break;
          }
          else
          {
               $h++;
          }
     }
}
unset($d[0]);
sort($d);
print_r($d);
?>

<?php
$line1 = trim(fgets(STDIN));
$n = $line1;
$i = 1;
$day = 0;
while($line1 > 0){
    $str = explode(' ', trim(fgets(STDIN)));
    if($day == 0){
        $day = $str[0];
    }else{
        $l = 1;
        while(($day + $l - $str[0]) % $str[1] != 0){

            $l++;
        }
        $day = $day + $l - $str[0];
    }
    $i++;
    $line1--;
}
echo $day;

<?php
$line1 = trim(fgets(STDIN));
$n = $line1;
$i = 1;
$day = 0;
while($line1 > 0){
    $str = explode(' ', trim(fgets(STDIN)));
    if($day == 0){
        $day = $str[0];
    }else{
        $l = 1;
        while(($day + $l - $str[0]) % $str[1] != 0 && ($day + $l - $str[0]) > 0){

            $l++;
        }
        $day = $day + $l;
    }
    $i++;
    $line1--;
}
echo $day;

<?php
$line1 = trim(fgets(STDIN));
$n = $line1;
$i = 1;
$day = 0;
while($line1 > 0){
    $str = explode(' ', trim(fgets(STDIN)));
    if($day == 0){
        $day = $str[0];
    }else{
        $l = 1;
        while(($day + $l - $str[0]) % $str[1] != 0){

            $l++;
        }
        $day = $day + $l;
    }
    $i++;
    $line1--;
}
echo $day;


<?php

###### IO ######
error_reporting(E_ALL);
error_reporting(0);

function readString($f) {
	$r = array(
		"\r" => '',
		"\n" => '',
	);
	return strtr(fgets($f), $r);
}

function readNumString($f) {
	return trim(fgets($f));
}

function stringToArray($str) {
	return str_split($str);
}

function readNum($f, $is_int = false) {
	$v = readNumString($f);
	return $is_int ? intval($v) : floatval($v);
}

function readStringExplode($f, $delimiter = ' ') {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = 'trim';
	return array_map($callf, $return);
}

function readStringExplodeNum($f, $delimiter = ' ', $is_int = false) {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = $is_int ? 'intval' : 'floatval';
	return array_map($callf, $return);
}

###### /IO ######

function getAnswer($is_test = false, $f = false) {
	if ($is_test === false) {
		$f = STDIN;
	}
	/* begin body */
	$n = readNum($f);
	$pairs = array();
	$L = $R = 0;
	$d = array();
	for ($i = 0; $i < $n; $i++) {
		$pairs[$i] = readStringExplodeNum($f, ' ', true);
		$L+=$pairs[$i][0];
		$R+=$pairs[$i][1];
	}
	$max = abs($L - $R);
	$maxIndex = -1;
	for ($i = 0; $i < $n; $i++) {
		$dL = $L - $pairs[$i][0] + $pairs[$i][1];
		$dR = $R - $pairs[$i][1] + $pairs[$i][0];
		if (abs($dL - $dR) > $max) {
			$max = abs($dL - $dR);
			$maxIndex = $i;
		}
	}
	echo $maxIndex + 1;
	/* / end body */
}

if (empty($is_test))
	getAnswer();


<?php
function NOD($a, $b){
    while ($a != $b){
        if ($a > $b){
            $a -= $b;
        } else {
            $b -= $a;
        }
    }
    return $a;
}
function NOK($a, $b){
    return abs($a * $b)/NOD($a, $b);
}

$q = (int)fgets(STDIN);
for($i=0;$i<$q;$i++)
{
	$s = trim(fgets(STDIN));
	$t = trim(fgets(STDIN));

	if(strlen($s) > strlen($t))
	{
		$bigStr = $s;
		$litStr = $t;
	}
	else
	{
		$bigStr = $t;
		$litStr = $s;
	}

	$nok = NOK(strlen($litStr), strlen($bigStr));
	$repeat1 = round($nok / strlen($bigStr));
	$repeat2 = round($nok / strlen($litStr));
	$str1 = str_repeat($bigStr, $repeat1);
	$str2 = str_repeat($litStr, $repeat2);
	if($str1 == $str2)
		print($str1."\r\n");
	else
		print("-1\r\n");
}

$end = readline();
?>

<?php

$times = (int) trim(fgets(STDIN));
for ($i = 0; $i < $times; ++$i) {
    $si[] = trim(fgets(STDIN));
    $ti[] = trim(fgets(STDIN));
}
for ($i = 0; $i < $times; ++$i) {
    $s = &$si[$i];
    $t = &$ti[$i];
    $slen = strlen($s);
    $tlen = strlen($t);
    $lcm = lcm($slen, $tlen);
    $s = str_repeat($s, $lcm / $slen);
    $t = str_repeat($t, $lcm / $tlen);
    echo ($s == $t ? $s : -1).PHP_EOL;
}

function gcd($a, $b)
{
    return $b ? gcd($b, $a % $b) : $a;
}
function lcm($a, $b)
{
    return $a * $b / gcd($a, $b);
}

<?php
     $ft =  trim(fgets(STDIN));
     for($irt = 0; $irt < $ft; $irt++){
         $a = trim(fgets(STDIN));
         $b = trim(fgets(STDIN));
         $sa = strlen($a);
         $sb = strlen($b);

         if($sa > $sb){
             $f = $a;
             $s = $b;
             $m = $sa;
         } else {
             $f = $b;
             $s = $a;
             $m = $sb;
         }
         $fs = '';
         $ss = '';
         $bool = false;
         $res = '';
         for($i = 0; $i <= $m; $i++) {
             $fs .= $f;
             $m1 = strlen($fs);
             for($j = 0; $j <= $m1; $j++) {
                 $ss .= $s;
                 if ($fs == $ss) {
                     $bool = true;
                     $res = $fs;
                     break;
                 }
             }
             $ss = '';
             if($bool){
                 break;
             }
         }
         if($bool){
             echo $res."\n";
         } else {
             echo  "-1\n";
         }

     }

?>

<?php
function NOD($a, $b){
    while ($a != $b){
        if ($a > $b){
            $a -= $b;
        } else {
            $b -= $a;
        }
    }
    return $a;
}
function NOK($a, $b){
    return abs($a * $b)/NOD($a, $b);
}

$q = (int)fgets(STDIN);
for($i=0;$i<$q;$i++)
{
	$s = trim(fgets(STDIN));
	$t = trim(fgets(STDIN));

	$s_2 = str_repeat($s, 2);
	$t_2 = str_repeat($t, 2);

	if(strlen($s_2) > strlen($t_2))
	{
		$bigStr = $s_2;
		$litStr = $t_2;
		$bigStr2 = $s;
		$litStr2 = $t;
	}
	else
	{
		$litStr = $s_2;
		$bigStr = $t_2;
		$litStr2 = $s;
		$bigStr2 = $t;
	}

	if(stripos($bigStr, $litStr) !== FALSE)
	{
		$nok = NOK(strlen($litStr2), strlen($bigStr2));
		$repeat = round($nok / strlen($bigStr2));
		print(str_repeat($bigStr2, $repeat)."\r\n");
	}
	else
		print("-1\r\n");
}

$end = readline();
?>


<?php

class Scanner {
  private $arr = [];
  private $count = 0;
  private $pointer = 0;
  public function next() {
    if($this->pointer >= $this->count) {
      $str = trim(fgets(STDIN));
      $this->arr = explode(' ', $str);
      $this->count = count($this->arr);
      $this->pointer = 0;
    }
    $result = $this->arr[$this->pointer];
    $this->pointer++;
    return $result;
  }
  public function hasNext() {
    return $this->pointer < $this->count;
  }
  public function nextInt() {
    return (int)$this->next();
  }
  public function nextDouble() {
    return (double)$this->next();
  }
}

class out {
  public static function println($str = "") {
    echo $str . PHP_EOL;
  }
}

$sc = new Scanner();
$n = $sc->nextInt();

$cnt = [0, 0];
for($i = 0; $i < $n; $i++) {
  $x = $sc->nextInt();
  $cnt[$x % 2]++;
}

if($cnt[0] >= $cnt[1]) {
  out::println($cnt[1]);
} else {
  out::println($cnt[0] + (int) (($cnt[1] - $cnt[0]) / 3));
}

<?php
	fscanf(STDIN, '%d\n', $n);
	$e = 0;
	$o = 0;
	$a = explode(' ', trim(fgets(STDIN)));
	foreach ($a as &$x) {
		$x = intval($x);
		if($x&1)
	    	$o++;
	  	else
			$e++;
	}
	if($o <= $e){
	    echo $o;
	}
	else {
	    echo $e + (floor(($o-$e)/3));
	}
?>

<?php
$fl = STDIN;
fscanf($fl,"%d",$n);
$a=explode(" ",fgets(STDIN));
$temp=0;
for($i=0;$i<$n;++$i)
    if($a[$i]%2==1)$temp++;
$ans=$temp;
if($temp>($n-$temp))$ans=$n-$temp;
$temp=$temp-$ans;
$ans+=intval($temp/3);
echo $ans;
?>

<?

$input = explode(" ", trim(fgets(STDIN)));
$n = $input[0];

$c = 0;
$nc = 0;
$input = explode(" ", trim(fgets(STDIN)));
for ($i = 0; $i < $n; $i++) {
    $a = (int)$input[$i];
    if ($a & 1)
		$nc = $nc + 1;
	else
		$c = $c + 1;
}
$ans = 0;
$t = min($c, $nc);
$ans += $t;
$nc -= $t;
$ans += intdiv($nc, 3);
echo $ans;

<?
    $dir = fopen("php://stdin", 'r');

    $n =  fgets($dir);
    $arr = explode(' ', fgets($dir));

    $odds = 0; $even = 0; $ans = 0;

    for ($i = 0; $i < $n; $i++) {
        $x = $arr[$i];
        if($x % 2)  $odds++;
        else        $even++;
    }

    $ans = min($odds, $even);
    $odds -= min($odds, $even);
    $ans += floor($odds / 3);

    echo $ans;
?>

<?php

$fl =STDIN;
fgets($fl);
$a = 0;
$b = 0;
$numbers = explode(" ", trim(fgets($fl)));
foreach ($numbers as &$number)
{
$number = intval($number);
//printf("%d\n", $number);
if ($number % 2 == 1)
$a = $a + 1;
else
$b = $b + 1;
}
//printf("%d %d", $a, $b);
if ($a >= $b) {
printf("%d", $b + ($a - $b) / 3);
} else {
printf("%d", $a);
}
?>

<?

    function printa($s){
        $stdout = fopen('php://stdout', 'w');

        fwrite($stdout, $s);

        fclose($stdout);
    }

    $stdin = fopen('php://stdin', 'r');
    $n = fgets($stdin) + 0;
    $a = explode(" ", fgets($stdin));

    $ch = 0;
    $nch = 0;

    for($i = 0; $i < count($a); $i++){
        if(($a[$i] + 0) % 2 == 0){
            $ch++;
        }else{
            $nch++;
        }
    }

    if($ch == $nch){
        printa($ch);
    }else if($ch > $nch){
        printa($nch);
    }else{
        $nch -= $ch;
        printa($ch + floor($nch / 3));
    }
?>

<?php
fscanf(STDIN,"%d", $iN);
$sNum = fgets(STDIN);

preg_match_all('!\d+!', $sNum, $aMatches);
$iCutOff = $aMatches[0][$iCutOffIndex - 1];
$a = 0;
$b = 0;
$c = 0;
$ans = 0;
foreach($aMatches[0] as $iNum)
{
	if($iNum %2 == 1)
		$a++;
	else
	    $b++;
}
$c = $a;
if( $c > $b )
    $c = $b;
$ans = $c + ($a-$c)/3;

fprintf(STDOUT, "%d", $ans);

<?php
$num=0;
$_fp = fopen("file.txt", "r");
$fr=fopen("php://stdin","r");
fscanf($fr, "%d\n",$num);
$input = explode(' ', trim(fgets($fr)));
$ans=0;
foreach ($input as $item){
   if ($item%2==1)
   $ans++;
}
$odd=$ans;
$even=count($input)-$odd;
if ($odd>$even)
   $answer=$even+intdiv($odd-$even,3);
   else
      $answer=$odd;
print($answer);
?>

<?php

$n =  trim(fgets(STDIN));
$uneven = 0;

foreach(explode(' ', trim(fgets(STDIN))) as $ch) {
	if ($ch%2==1) { $uneven+=1;}
}

$even = $n - $uneven;
$c = min($uneven, $even);

$uneven-= $c;

print($c+intdiv($uneven, 3));
?>

<?php

$fl = STDIN;
fscanf($fl,"%d",$n);
$e = 0;
$ne = 0;

$input = trim(fgets(STDIN));
$arr = explode(" ", $input);
foreach($arr as &$a){
    $a = (int)$a;
    if ($a % 2 == 0){
      $e++;
    }
    else {
      $ne++;
    }
}

if ($ne <= $e) {
  echo $ne . "\n";
} else {
  $g = $ne - $e;
  echo ($e + intdiv($g, 3)) . "\n";
}

?>

<?php
  $input = (int)trim(fgets(STDIN));
  $hola = explode(' ',trim(fgets(STDIN)));
  $even=0;
  $odd=0;
  for($x=0; $x<$input; $x++)
  {
    if($hola[$x]%2==0)
      $even++;
    else {
      $odd++;
    }
  }
  if($even>=$odd)
    echo $odd;
  else {
    echo $even + ($odd-$even-($odd-$even)%3)/3;
  }
 ?>

<?php
	fscanf(STDIN, '%d\n', $n);

	$even = 0;
	$odd = 0;

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

	foreach ($a as &$num) {
		$num = intval($num);
		if ($num % 2 == 0) {
	    		$even++;
	  	} else {
			$odd++;
	  	}
	}

	if ($odd == 0) {
		echo "0";
	} else {
		$best = 0;

		for($i = 0 ; $i <= $even ; $i++){
			$best = max(min($i, $odd) + max(floor(($odd - $i) / 3), 0), $best);
		}

		echo $best;
	}

?>

<?php

fscanf(STDIN, "%d", $n);
$odd = 0;
$even = 0;

$line = trim(fgets(STDIN));
$array = array();
$array = array_merge($array, explode(" ", $line));

for($i = 0; $i < $n; $i ++) {
    $t = $array[$i];
    if(($t & 1) > 0) $odd ++;
    else $even ++;
}
$mt = min($odd, $even);
$ans = $mt;
$odd -= $mt;
$ans = $ans + intval(($odd / 3));
print $ans;
/*
if($even >= $odd) {
    print $odd;
}
else {
    print $even + intval(($odd - $even) / 3);
}
*/

?>

<?php
  $input = (int)trim(fgets(STDIN));
  $hola = explode(' ',trim(fgets(STDIN)));
  $even=0;
  $odd=0;
  for($x=0; $x<$input; $x++)
  {
    if($hola[$x]%2==0)
      $even++;
    else {
      $odd++;
    }
  }
  if($even>=$odd)
    echo $odd;
  else {
    echo $even + ($odd-$even)/3;
  }
 ?>

<?php
	fscanf(STDIN, '%d\n', $n);

	$even = 0;
	$odd = 0;

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

	foreach ($a as &$num) {
		$num = intval($num);
		if ($num % 2 == 0) {
	    		$even++;
	  	} else {
			$odd++;
	  	}
	}

	if ($odd == 0) {
		echo "0";
	} else {
		echo max(min($even, $odd), floor($odd / 3) + min($odd % 3, $even));
	}

?>

<?php
fscanf(STDIN, '%d\n', $n);

$even = 0;
$odd = 0;

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

foreach ($a as &$num) {
  $num = intval($num);
  if ($num % 2 == 0) {
    $even++;
  } else {
    $odd++;
  }
}

if ($even == 0 || $odd == 0) {
  echo "0";
} else {
  echo min($even, $odd);
}

?>

<?php

fscanf(STDIN, "%d", $n);
$odd = 0;
$even = 0;

$array = array();
while($in = fread(STDIN, 1024)) {
    $array = array_merge($array, explode(" ", $in));
}

for($i = 0; $i < $n; $i ++) {
    $t = $array[$i];
    if(($t & 1) > 0) $odd ++;
    else $even ++;
}
$mt = min($odd, $even);
$ans = $mt;
$odd -= $mt;
$ans = $ans + intval(($odd / 3));
print $ans;
/*
if($even >= $odd) {
    print $odd;
}
else {
    print $even + intval(($odd - $even) / 3);
}
*/

?>

<?php

fscanf(STDIN, "%d", $n);
$odd = 0;
$even = 0;

$array = array();
while($in = fread(STDIN, 1024)) {
    $array = array_merge($array, explode(" ", $in));
}

for($i = 0; $i < $n; $i ++) {
    $t = $array[$i];
    if($t & 1) $odd ++;
    else $even ++;
}

$mt = min($odd, $even);
$ans = $mt;
$odd -= $mt;
$ans = $ans + ($odd / 3);
print $ans;

?>

<?php

fscanf(STDIN, "%d", $n);
$odd = 0;
$even = 0;

$array = array();
while($in = fread(STDIN, 10240000)) {
    $array = array_merge($array, explode(" ", $in));
}

for($i = 0; $i < $n; $i ++) {
    $t = $array[$i];
    if(($t & 1) > 0) $odd ++;
    else $even ++;
}

$mt = min($odd, $even);
$ans = $mt;
$odd -= $mt;
$ans = $ans + ($odd / 3);
print $ans;

?>

<?php

fscanf(STDIN, "%d", $n);
$odd = 0;
$even = 0;

$array = array();
while($in = fread(STDIN, 1024)) {
    $array = array_merge($array, explode(" ", $in));
}

for($i = 0; $i < $n; $i ++) {
    $t = $array[$i];
    if(($t & 1) > 0) $odd ++;
    else $even ++;
}

$mt = min($odd, $even);
$ans = $mt;
$odd -= $mt;
$ans = $ans + intval(($odd / 3));
print $ans;

?>

<?php

fscanf(STDIN, "%d", $n);
$odd = 0;
$even = 0;

$array = array();
while ($in = fread(STDIN, 1024)) {
    $array = array_merge($array, explode(" ", $in));
}

for($i = 0; $i < count($array); $i ++) {
    $t = $array[$i];
    if($t & 1) $odd ++;
    else $even ++;
}

$mt =  min($odd, $even);
$ans = $mt;
$odd -= $mt;
$ans = $ans + ($odd / 3);
print $ans;

?>

<?php

fscanf(STDIN, "%d", $n);
$odd = 0;
$even = 0;

$array = array();
while($in = fread(STDIN, 10240000)) {
    $array = array_merge($array, explode(" ", $in));
}

for($i = 0; $i < $n; $i ++) {
    $t = $array[$i];
    if($t & 1) $odd ++;
    else $even ++;
}

$mt = min($odd, $even);
$ans = $mt;
$odd -= $mt;
$ans = $ans + ($odd / 3);
print $ans;

?>

<?php

fscanf(STDIN, "%d", $n);
$odd = 0;
$even = 0;

$array = array();
while($in = fread(STDIN, 10240000)) {
    $array = array_merge($array, explode(" ", $in));
}

for($i = 0; $i < $n; $i ++) {
    $t = $array[$i];
    if(($t & 1) > 0) $odd ++;
    else $even ++;
}

$mt = min($odd, $even);
$ans = $mt;
$odd -= $mt;
$ans = $ans + (int)($odd / 3);
print $ans;

?>

<?php

fscanf(STDIN, "%d", $n);
$odd = 0;
$even = 0;

$array = array();
while($in = fread(STDIN, 1024 * 1000 * 100)) {
    $array = array_merge($array, explode(" ", $in));
}

for($i = 0; $i < $n; $i ++) {
    $t = $array[$i];
    if(($t & 1) > 0) $odd ++;
    else $even ++;
}

$mt = min($odd, $even);
$ans = $mt;
$odd -= $mt;
$ans = $ans + (int)($odd / 3);
print $ans;

?>

<?php

fscanf(STDIN, "%d", $n);
$odd = 0;
$even = 0;

$array = array();
while($in = fread(STDIN, 1024)) {
    $array = array_merge($array, explode(" ", $in));
}

for($i = 0; $i < $n; $i ++) {
    $t = $array[$i];
    if(($t & 1) > 0) $odd ++;
    else $even ++;
}
/*
$mt = min($odd, $even);
$ans = $mt;
$odd -= $mt;
$ans = $ans + intval(($odd / 3));
print $ans;
*/
if($even >= $odd) {
    print $odd;
}
else {
    print $even + intval(($odd - $even) / 3);
}

?>

<?
    $dir = fopen("php://stdin", 'r');

    $n =  fgets($dir);
    $arr = explode(' ', fgets($dir));

    $ones = 0; $odds = 0; $even = 0;

    for ($i = 0; $i < $n; $i++) {
        $x = $arr[$i];
        if ($x == 1)    $ones++;
        else if($x % 2) $odds++;
        else            $even++;
    }

    $odds += min($ones, $even);
    $ones -= min($ones, $even);
    $odds += floor($ones / 3);

    echo $odds;
?>

<?
    $dir = fopen("php://stdin", 'r');
    $n =  fgets($dir);
    $arr = explode(' ', fgets($dir));

    $ones = 0; $odds = 0; $even = 0;

    for ($i = 0; $i < $n; $i++) {
        $x = $arr[$i];
        if ($x == 1)    $ones++;
        else if($x % 2) $odds++;
        else            $even++;
    }

    $odds += min($ones, $even);

    echo $odds;
?>

<?php

class Scanner {
  private $arr = [];
  private $count = 0;
  private $pointer = 0;
  public function next() {
    if($this->pointer >= $this->count) {
      $str = trim(fgets(STDIN));
      $this->arr = explode(' ', $str);
      $this->count = count($this->arr);
      $this->pointer = 0;
    }
    $result = $this->arr[$this->pointer];
    $this->pointer++;
    return $result;
  }
  public function hasNext() {
    return $this->pointer < $this->count;
  }
  public function nextInt() {
    return (int)$this->next();
  }
  public function nextDouble() {
    return (double)$this->next();
  }
}

class out {
  public static function println($str = "") {
    echo $str . PHP_EOL;
  }
}

$sc = new Scanner();
$n = $sc->nextInt();
$cnt = [0, 0];
for($i = 0; $i < $n; $i++) {
  $x = $sc->nextInt();
  $cnt[$x % 2]++;
}
out::println($cnt[0] < $cnt[1] ? $cnt[0] : $cnt[1]);

<?php

class Scanner {
  private $arr = [];
  private $count = 0;
  private $pointer = 0;
  public function next() {
    if($this->pointer >= $this->count) {
      $str = trim(fgets(STDIN));
      $this->arr = explode(' ', $str);
      $this->count = count($this->arr);
      $this->pointer = 0;
    }
    $result = $this->arr[$this->pointer];
    $this->pointer++;
    return $result;
  }
  public function hasNext() {
    return $this->pointer < $this->count;
  }
  public function nextInt() {
    return (int)$this->next();
  }
  public function nextDouble() {
    return (double)$this->next();
  }
}

class out {
  public static function println($str = "") {
    echo $str . PHP_EOL;
  }
}

$sc = new Scanner();
$n = $sc->nextInt();

$cnt = [0, 0];
for($i = 0; $i < $n; $i++) {
  $x = $sc->nextInt();
  $cnt[$x % 2]++;
}

if($cnt[0] >= $cnt[1]) {
  out::println($cnt[1]);
} else {
  out::println($cnt[0] + ($cnt[1] - $cnt[0]) / 3);
}

<?php
$fl = STDIN;
fscanf($fl,"%d",$n);
$a=explode(" ",fgets(STDIN));
$temp=0;
for($i=0;$i<$n;++$i)
    if($a[$i]%2==1)$temp++;
echo $temp;
?>

<?php
$fl = STDIN;
fscanf($fl,"%d",$n);
$a=explode(" ",fgets(STDIN));
$temp=0;
for($i=0;$i<$n;++$i)
    if($a[$i]%2==1)$temp++;
if($temp>($n-$temp))$temp=$n-$temp;
echo $temp;
?>

<?php
$fl = STDIN;
fscanf($fl,"%d",$n);
$a=explode(" ",fgets(STDIN));
$temp=0;
for($i=0;$i<$n;++$i)
    if($a[$i]%2==1)$temp++;
$ans=$temp;
if($temp>($n-$temp))$ans=$n-$temp;
$temp=$temp-$ans;
$ans+=$temp/3;
echo $ans;
?>

<?

$input = explode(" ", trim(fgets(STDIN)));
$n = $input[0];

$c = 0;
$nc = 0;
$input = explode(" ", trim(fgets(STDIN)));
for ($i = 0; $i < $n; $i++) {
    $a = (int)$input[$i];
    if ($a & 1)
		$nc = $nc + 1;
	else
		$c = $c + 1;
}
$ans = 0;
$t = min($c, $nc);
$ans = $ans + $t;
$nc -= $t;
$ans += $nc/3;
echo $ans;

<?

$input = explode(" ", trim(fgets(STDIN)));
$n = $input[0];

$c = 0;
$nc = 0;
$input = explode(" ", trim(fgets(STDIN)));
for ($i = 0; $i < $n; $i++) {
    $a = (int)$input[$i];
    if ($a & 1)
		$nc = $nc + 1;
	else
		$c = $c + 1;
}
if($c > $nc)
    echo $nc;
else
     echo $c;

<?

    function printa($s){
        $stdout = fopen('php://stdout', 'w');

        fwrite($stdout, $s);

        fclose($stdout);
    }

    $stdin = fopen('php://stdin', 'r');
    $n = fgets($stdin) + 0;
    $a = explode(" ", fgets($stdin));

    $ch = 0;
    $nch = 0;

    for($i = 0; $i < count($a); $i++){
        if(($a[$i] + 0) % 2 == 0){
            $ch++;
        }else{
            $nch++;
        }
    }

    if($ch == $nch){
        printa($ch);
    }else if($ch > $nch){
        printa($nch);
    }else{
        $nch -= $ch;
        printa($ch + $nch);
    }
?>

<?php
fscanf(STDIN,"%d", $iN);
$sNum = fgets(STDIN);

preg_match_all('!\d+!', $sNum, $aMatches);
$iCutOff = $aMatches[0][$iCutOffIndex - 1];
$a = 0;
$b = 0;
$c = 0;
$ans = 0;
foreach($aMatches[0] as $iNum)
{
	if($iNum %2 == 1)
		$a++;
	else
	    $b++;
}
$c = $a;
if( $c < $b )
    $c = $b;
$ans = $c + ($a-$c)/3;

fprintf(STDOUT, "%d", $ans);

<?php
fscanf(STDIN,"%d", $iN);
$sNum = fgets(STDIN);

preg_match_all('!\d+!', $sNum, $aMatches);
$iCutOff = $aMatches[0][$iCutOffIndex - 1];
$iCount = 0;
foreach($aMatches[0] as $iNum)
{
	if($iNum %2 == 1)
		$iCount++;
}

fprintf(STDOUT, "%d", $iCount);

<?php
$num=0;
$_fp = fopen("file.txt", "r");
$fr=fopen("php://stdin","r");
fscanf($fr, "%d\n",$num);
$input = explode(' ', trim(fgets($fr)));
$ans=0;
foreach ($input as $item){
   if ($item%2==1)
   $ans++;
}
print($ans);
?>

<?php

$fl = STDIN;
fscanf($fl,"%d",$n);
$e = 0;
$ne = 0;

$input = trim(fgets(STDIN));
$arr = explode(" ", $input);
foreach($arr as &$a){
    $a = (int)$a;
    if ($a % 2 == 0){
      $e++;
    }
    else {
      $ne++;
    }
}

if ($ne <= $e) {
  echo $ne . "\n";
} else {
  $g = $ne - $e;
  echo ($e + $g / 3) . "\n";
}

?>


<?php
$line = trim(fgets(STDIN));

$ab_left = strpos($line, "AB");
$ab_right = strrpos($line, "AB");
$ba_left = strpos($line, "BA");
$ba_right = strrpos($line, "BA");

if ($ab_left === false || $ba_left === false) {
  echo 'NO';
  exit(0);
}

if (abs($ab_left - $ba_left) > 1 || abs($ab_left - $ba_right) > 1 || abs($ab_right - $ba_left) > 1 || abs($ab_right - $ba_right) > 1) {
  echo 'YES';
  exit(0);
}

echo 'NO';

<?php

$server = "php://stdin";
$local = "input.txt";
$handle = fopen($server, "r");
$x= trim(fgets($handle));
$right=strlen($x)-1;
$one=false;$tow=false;$check=false;
$vis=[];
$vis1=[];
$first=true;
if(strlen($x)==1)echo "NO";
else
{
    for($i=0;$i<strlen($x)-1;$i++)
    {
        if($x[$i].$x[$i+1]=='AB' )
        {
           $vis[]=$i+1;
        }
        if($x[$i].$x[$i+1]=='BA' )
        {
            $vis1[]=$i+1;
        }
    }
    $temp=$vis;
    $mina=min($temp);$maxa=max($temp);

    $temp1=$vis1;
    $minb=min($temp1);$maxb=max($temp1);
    if((sizeof($vis)>=1 && sizeof($vis1)>=1) )
    {
        if((min($mina,$maxb)+1!=max($mina,$maxb) || min($minb,$maxa)+1!=max($minb,$maxa)))echo "YES";
        else
            echo "NO";
    }

    else echo "NO";
}
?>

<?php
$input = fgets(STDIN);

if (empty($input))
{
    echo 'NO';
    exit;
}
else if (strlen($input) < 4)
{
    echo 'NO';
    exit;
}
else
{
    $chars = str_split($input);

    if (search($chars, 'A', 'B') || search($chars, 'B', 'A'))
    {
        echo 'YES';
        exit;
    }

    echo 'NO';
}

function search($chars, $fir, $sec)
{
    $n = count($chars);
    $flag = false;

    for ($i = 0; $i < ($n - 1); $i++)
    {
        if ($chars[$i] == $fir && $chars[$i + 1] == $sec)
        {
            $flag = true;
            break;
        }
    }

    if ($flag)
    {
        for ($j = $i + 2; $j < ($n - 1); $j++)
        {
            if ($chars[$j] == $sec && $chars[$j + 1] == $fir)
            {
                return true;
            }
        }
    }

    return false;
}

<?php
$a = " " . trim(fgets(STDIN));
$b = "AB";
$c = "BA";
$d = "ABA";
$e = "BAB";
$f = strpos($a, $b);
$g = strpos($a, $c);
$h = strpos($a, $d);
$i = strpos($a, $e);
if(($h == TRUE) || ($i == TRUE))
{
    print "NO";
}
elseif(($f == TRUE) && ($g == TRUE))
{
    print "YES";
}
else
{
    print "NO";
}
?>

<?php
$line = trim(fgets(STDIN));
$ab_pos = strpos($line, "AB");
$ba_pos = strrpos($line, "BA");
if ($ab_pos !== false && $ba_pos !== false && abs($ba_pos - $ab_pos) > 2) {
  echo 'YES';
} else {
  echo 'NO';
}

<?php
$line = trim(fgets(STDIN));
$ab_pos = strpos($line, "AB");
$ba_pos = strrpos($line, "BA");
if ($ab_pos != -1 && $ba_pos != -1 && abs($ba_pos - $ab_pos) > 2) {
  echo 'YES';
} else {
  echo 'NO';
}

<?php
$line = trim(fgets(STDIN));
$ab_pos = strpos($line, "AB");
$ba_pos = strrpos($line, "BA");
if ($ab_pos !== false && $ba_pos !== false && abs($ba_pos - $ab_pos) >= 2) {
  echo 'YES';
} else {
  echo 'NO';
}

<?php
$line = trim(fgets(STDIN));

$ab_left = strpos($line, "AB");
$ab_right = strrpos($line, "AB");
$ba_left = strpos($line, "BA");
$ba_right = strpos($line, "BA");

//echo "$ab_left\n$ba_left\n$ba_right";

if ($ab_left === false || $ba_left === false) {
  echo 'NO';
  exit(0);
}

if (abs($ab_left - $ba_left) > 1 || abs($ab_left - $ba_right) > 1) {
  echo 'YES';
  exit(0);
}

echo 'NO';

<?php
$line = trim(fgets(STDIN));
$ab_left = strpos($line, "AB");
$ab_right = strrpos($line, "AB");
$ba_left = strpos($line, "BA");
$ba_right = strpos($line, "BA");
if ($ab_left == false || $ba_left == false) {
  echo 'NO';
  exit(0);
}
if (abs($ab_left - $ba_left) > 1 || abs($ab_left - $ba_right) > 1) {
  echo 'YES';
  exit(0);
}
echo 'NO';

<?php
$line = trim(fgets(STDIN));
$ab_pos = strpos($line, "AB");
$ba_pos = strrpos($line, "BA");
if ($ab_pos > 0 && $ba_pos > 0 && abs($ba_pos - $ab_pos) > 2) {
  echo 'YES';
} else {
  echo 'NO';
}

<?php
$line = trim(fgets(STDIN));

$ab_left = strpos($line, "AB");
$ab_right = strrpos($line, "AB");
$ba_left = strpos($line, "BA");
$ba_right = strrpos($line, "BA");

if ($ab_left === false || $ba_left === false) {
  echo 'NO';
  exit(0);
}

if (abs($ab_left - $ba_left) > 1 || abs($ab_left - $ba_right) > 1 || abs($ab_right - $ba_right) > 1) {
  echo 'YES';
  exit(0);
}

echo 'NO';

<?php
$line = trim(fgets(STDIN));
$ab_pos = strpos($line, "AB");
$ba_pos = strrpos($line, "BA");
if ($ab_pos && $ba_pos && abs($ba_pos - $ab_pos) > 2) {
  echo 'YES';
} else {
  echo 'NO';
}

<?php
$line = trim(fgets(STDIN));
$ab_pos = strpos($line, "AB");
$ba_pos = strrpos($line, "BA");
if ($ab_pos != -1 && $ba_pos != -1 && abs($ba_pos - $ab_pos) > 1) {
  echo 'YES';
} else {
  echo 'NO';
}

<?php
$line = trim(fgets(STDIN));

$ab_left = strpos($line, "AB");
$ab_right = strrpos($line, "AB");
$ba_left = strpos($line, "BA");
$ba_right = strrpos($line, "BA");

//echo "$ab_left\n$ba_left\n$ba_right";

if ($ab_left === false || $ba_left === false) {
  echo 'NO';
  exit(0);
}

//echo 'next';

if (abs($ab_left - $ba_left) > 1 || abs($ab_left - $ba_right) > 1) {
  echo 'YES';
  exit(0);
}

echo 'NO';

<?php
$line = trim(fgets(STDIN));

$ab_left = strpos($line, "AB");
$ab_right = strrpos($line, "AB");
$ba_left = strpos($line, "BA");
$ba_right = strpos($line, "BA");

//echo "$ab_left\n$ba_left\n$ba_right";

if ($ab_left === false || $ba_left === false) {
  echo 'NO';
  exit(0);
}

echo 'next';

if (abs($ab_left - $ba_left) > 1 || abs($ab_left - $ba_right) > 1) {
  echo 'YES';
  exit(0);
}

echo 'NO';

<?php
$input = fgets(STDIN);

if (empty($input))
{
    echo 'NO';
    exit;
}
else if (strlen($input) < 4)
{
    echo 'NO';
    exit;
}
else
{
    $chars = str_split($input);
    $ABFlag = false;
    $BAFlag = false;
    $fir = 'A';
    $sec = 'B';

    $n = count($chars);

    for ($i = 0; $i < ($n - 1); $i++)
    {
        if ($chars[$i] == $fir && $chars[$i + 1] == $sec)
        {
            $ABFlag = true;
            $fir = 'B';
            $sec = 'A';
            break;
        }

        if ($chars[$i] == $sec && $chars[$i + 1] == $fir)
        {
            $BAFlag = true;
            $fir = 'A';
            $sec = 'B';
            break;
        }
    }

    if ($ABFlag || $BAFlag)
    {
        for ($j = $i + 2; $j < count($chars) - 1; $j++)
        {
            if ($chars[$j] == $fir && $chars[$j + 1] == $sec)
            {
                echo 'YES';
                exit;
            }
        }
    }

    echo 'NO';
}

<?php

$input = !empty($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : null;

if (empty($input))
{
    echo 'NO';
    exit;
}
else if (strlen($input) < 4)
{
    echo 'NO';
    exit;
}
else
{
    $chars = str_split($input);
    $ABFlag = false;
    $BAFlag = false;
    $fir = 'A';
    $sec = 'B';

    $n = count($chars);

    for ($i = 0; $i < ($n - 1); $i++)
    {
        if ($chars[$i] == $fir && $chars[$i + 1] == $sec)
        {
            $ABFlag = true;
            $fir = 'B';
            $sec = 'A';
            break;
        }

        if ($chars[$i] == $sec && $chars[$i + 1] == $fir)
        {
            $BAFlag = true;
            $fir = 'A';
            $sec = 'B';
            break;
        }
    }

    if ($ABFlag || $BAFlag)
    {
        for ($j = $i + 2; $j < count($chars) - 1; $j++)
        {
            if ($chars[$j] == $fir && $chars[$j + 1] == $sec)
            {
                echo 'YES';
                exit;
            }
        }
    }

    echo 'NO';
}

<?php

$input = !empty($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : null;

if (empty($input))
{
    echo 'NO';
}
else if (strlen($input) < 4)
{
    echo 'NO';
}
else
{
    $chars = str_split($input);
    $ABFlag = false;
    $BAFlag = false;
    $fir = 'A';
    $sec = 'B';

    $n = count($chars);

    for ($i = 0; $i < ($n - 1); $i++)
    {
        if ($chars[$i] == $fir && $chars[$i + 1] == $sec)
        {
            $ABFlag = true;
            $fir = 'B';
            $sec = 'A';
            break;
        }

        if ($chars[$i] == $sec && $chars[$i + 1] == $fir)
        {
            $BAFlag = true;
            $fir = 'A';
            $sec = 'B';
            break;
        }
    }

    if ($ABFlag || $BAFlag)
    {
        for ($j = $i + 2; $j < count($chars) - 1; $j++)
        {
            if ($chars[$j] == $fir && $chars[$j + 1] == $sec)
            {
                echo 'YES';
                return;
            }
        }
    }

    echo 'NO';
}

<?php

$input = !empty($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : null;

if (empty($input))
{
    echo 'NO';
}
else if (strlen($input) < 4)
{
    echo 'NO';
}
else
{
    $chars = str_split($input);
    $ABFlag = false;
    $BAFlag = false;
    $flag = false;
    $fir = 'A';
    $sec = 'B';

    $n = count($chars);

    for ($i = 0; $i < ($n - 1); $i++)
    {
        if ($chars[$i] == $fir && $chars[$i + 1] == $sec)
        {
            $ABFlag = true;
            $fir = 'B';
            $sec = 'A';
            break;
        }

        if ($chars[$i] == $sec && $chars[$i + 1] == $fir)
        {
            $BAFlag = true;
            $fir = 'A';
            $sec = 'B';
            break;
        }
    }

    if ($ABFlag || $BAFlag)
    {
        for ($j = $i + 2; $j < count($chars) - 1; $j++)
        {
            if ($chars[$j] == $fir && $chars[$j + 1] == $sec)
            {
                echo 'YES';
                $flag = true;
            }
        }

        if (!$flag)
        {
            echo 'NO';
        }
    }
    else
    {
        echo 'NO';
    }
}

<?php
$input = fgets(STDIN);

if (empty($input))
{
    echo 'NO';
    exit;
}
else if (strlen($input) < 4)
{
    echo 'NO';
    exit;
}
else
{
    $chars = str_split($input);

    if (search($chars, 'A', 'B') || search($chars, 'B', 'A'))
    {
        echo 'YES';
    }

    echo 'NO';
}

function search($chars, $fir, $sec)
{
    $n = count($chars);
    $flag = false;

    for ($i = 0; $i < ($n - 1); $i++)
    {
        if ($chars[$i] == $fir && $chars[$i + 1] == $sec)
        {
            $flag = true;
            break;
        }
    }

    if ($flag)
    {
        for ($j = $i + 2; $j < ($n - 1); $j++)
        {
            if ($chars[$j] == $sec && $chars[$j + 1] == $fir)
            {
                return true;
            }
        }
    }

    return false;
}

<?php

$server = "php://stdin";
$local = "input.txt";
$handle = fopen($server, "r");
$x= trim(fgets($handle));
$right=strlen($x)-1;
$one=false;$tow=false;$check=false;
if(strlen($x)==1)echo "NO";
else
{
    for($i=0;$i<strlen($x)-1;$i+=2)
    {
        if($x[$i].$x[$i+1]=='AB')
        {
         $one=true;
        }
        if($x[$i].$x[$i+1]=='BA')
        {
            $tow=true;
        }
    }
    if($one==true)
    {
        for($i=0;$i<strlen($x)-1;$i+=2)
        {
            if($x[$i].$x[$i+1]=='BA')
            {
                $check=true;
            }
        }
    }else if($tow==true)
    { for($i=0;$i<strlen($x)-1;$i+=2)
    {
        if($x[$i].$x[$i+1]=='AB')
        {
            $check=true;
        }
    }

    }

    if($check==true)echo "YES";
    else echo "NO";
}
?>

<?php

$server = "php://stdin";
$local = "input.txt";
$handle = fopen($server, "r");
$x= trim(fgets($handle));
$right=strlen($x)-1;
$one=false;$tow=false;$check=false;
$vis=[];
$vis1=[];
$first=true;
if(strlen($x)==1)echo "NO";
else
{
    for($i=0;$i<strlen($x)-1;$i++)
    {
        if($x[$i].$x[$i+1]=='AB' )
        {
           $vis[]=$i+1;
        }
        if($x[$i].$x[$i+1]=='BA' )
        {
            $vis1[]=$i+1;
        }
    }
    $temp=$vis;
    $mina=min($temp);$maxa=max($temp);

    $temp1=$vis1;
    $minb=min($temp1);$maxb=max($temp1);

    if((sizeof($vis)>=1 && sizeof($vis1)>=1) && min($mina,$maxb)+1!=max($mina,$maxb) || min($minb,$maxa)+1!=max($minb,$maxa))echo "YES";
    else echo "NO";
}
?>

<?php

$server = "php://stdin";
$local = "input.txt";
$handle = fopen($server, "r");
$x= trim(fgets($handle));
$right=strlen($x)-1;
$one=false;$tow=false;$check=false;
if(strlen($x)==1)echo "NO";
else
{
    for($i=0;$i<strlen($x)-1;$i++)
    {
        if($x[$i].$x[$i+1]=='AB')
        {
         $one=true;
        }
        if($x[$i].$x[$i+1]=='BA')
        {
            $tow=true;
        }
    }
    if($one==true)
    {
        for($i=0;$i<strlen($x)-1;$i++)
        {
            if($x[$i].$x[$i+1]=='BA')
            {
                $check=true;
            }
        }
    }else if($tow==true)
    { for($i=0;$i<strlen($x)-1;$i++)
    {
        if($x[$i].$x[$i+1]=='AB')
        {
            $check=true;
        }
    }

    }

    if($check==true)echo "YES";
    else echo "NO";
}
?>

<?php

$server = "php://stdin";
$local = "input.txt";
$handle = fopen($server, "r");
$x= trim(fgets($handle));
$right=strlen($x)-1;
$one=false;$tow=false;$check=false;
$vis=[];
$vis1=[];
$first=true;
if(strlen($x)==1)echo "NO";
else
{
    for($i=0;$i<strlen($x)-1;$i++)
    {
        if($x[$i].$x[$i+1]=='AB' )
        {
           $vis[]=$i+1;
        }
        if($x[$i].$x[$i+1]=='BA' )
        {
            $vis1[]=$i+1;
        }
    }
    $temp=array_merge($vis1,$vis);
    $mina=min($temp);$maxa=max($temp);

    if((sizeof($vis)>=1 && sizeof($vis1)>=1) && $mina+1!=$maxa)echo "YES";
    else echo "NO";
}
?>


<?php
/*dskhbfjkhs*/
$server = "php://stdin";
$handle = fopen($server, "r");
$input = trim(fgets($handle));

for ($j = 0; $j < $input; $j++) {
    $new = trim(fgets($handle));
    $array = trim(fgets($handle));
    $array = explode(' ', $array);
    $map = [];

    $s = 0;
    $cnt = 0;
    for ($i = 0; $i < $new; $i++) {
        if ($array[$i] == 0) {
            $cnt++;
            $array[$i] = 1;
        }
        $s += $array[$i];
    }
    if ($s == 0) {
        $cnt++;
    }
    echo $cnt . "\n";

}

?>

<?php

$f = fopen( 'php://stdin', 'r' );

$cases = fgets($f);

for ($k = 0; $k < $cases; $k++) {
  $line = trim(fgets($f));
  $line = trim(fgets($f));

  $arr = explode(" ", $line);

  // get the number of 0s
  $steps = 0;
  $sum = 0;
  for ($i = 0; $i < count($arr); $i++) {
    if($arr[$i] == 0) {
      $arr[$i]++;
      $steps++;
    }

    $sum += $arr[$i];
  }

  if ($sum == 0) {
    $steps++;
  }

  print $steps . "\n";

}

?>

<?php

class ArgsHelper
{
	private $useStd;

	public function __construct($userStd = false)
	{
		$this->useStd = $userStd;
	}

	private function hasZeros($numbers)
	{
		foreach ($numbers as $item)
		{
			if ((int)$item === 0)
			{
				return true;
			}
		}
		return false;
	}

	public function calculateResult($input)
	{
		$numbers = array_map('intval', explode(' ', $input));
		sort($numbers);
		$numbers = array_reverse($numbers);
		$count = 0;
		while (true)
		{
			foreach ($numbers as $index => $number)
			{
				if ($number === 0)
				{
					$numbers[$index]++;
					$count++;
					continue 2;
				}
			}
			$sum = (int)array_sum($numbers);
			if ($sum !== 0 && !$this->hasZeros($numbers))
			{
				return $count;
			}

			foreach ($numbers as $index => $number)
			{
				$numbers[$index]++;

				$sum = (int)array_sum($numbers);
				if ($sum !== 0 && !$this->hasZeros($numbers))
				{
					$count++;
					return $count;
				}
				else
				{
					$numbers[$index]--;
				}
			}

			foreach ($numbers as $index => $number)
			{
				if ($number !== -1)
				{
					$numbers[$index]++;
					$count++;
					continue 2;
				}
			}
			foreach ($numbers as $index => $number)
			{
				$numbers[$index]++;
				$count++;
				break;
			}
		}
	}

	public function getInputLines()
	{
		$input = [];
		if ($this->useStd)
		{
			while ($line = fgets(STDIN))
			{
				$input[] = $line;
			}
		}
		else
		{
			$input = '';
			$input = explode("\n", trim($input));
		}
		return $input;
	}

	public function printResult($result)
	{
		if ($this->useStd)
		{
			fwrite(STDOUT, $result);
		}
		else
		{
			var_dump($result);
		}
	}

}

$helper = new ArgsHelper(true);
$lines = $helper->getInputLines();

$result = '';
#
array_shift($lines);
foreach ($lines as $index => $line)
{
	if ($index % 2 === 0)
	{
		continue;
	}
	$result .= $helper->calculateResult($line) . ' ';
}
$result = trim($result);
#

$helper->printResult($result);

<?php
$server = "php://stdin";
$local = "input.txt";
$handle = fopen($server, "r");
$t= trim(fgets($handle));

for($j=0;$j<$t;$j++)
{
    $n=trim(fgets($handle));
    $a=trim(fgets($handle));
    $a=explode(' ',$a);
    $map=[];

    $sum=0;$count=0;
    for($i=0;$i<$n;$i++)
    {
       if($a[$i]==0){$count++;$a[$i]=1;}
       $sum+=$a[$i];
    }
    if($sum==0)
    {
        $count++;
    }
    echo $count."\n";

}

?>

<?php

class ArgsHelper
{
	private $useStd;

	public function __construct($userStd = false)
	{
		$this->useStd = $userStd;
	}

	public function calculateResult($input)
	{
		$numbers = array_map('intval', explode(' ', $input));
		sort($numbers);
		$numbers = array_reverse($numbers);
		$count = 0;
		while (true)
		{
			$sum = array_sum($numbers);
			$product = array_product($numbers);
			if ($sum !== 0 && $product !== 0)
			{
				return $count;
			}
			foreach ($numbers as $index => $number)
			{
				$numbers[$index]++;
				$count++;

				$sum = array_sum($numbers);
				$product = array_product($numbers);
				if ($sum !== 0 && $product !== 0)
				{
					return $count;
				}
				else
				{
					$numbers[$index]--;
					$count--;
				}
			}
			foreach ($numbers as $index => $number)
			{
				if ($number === 0)
				{
					$numbers[$index]++;
					$count++;
					continue 2;
				}
			}
			foreach ($numbers as $index => $number)
			{
				if ($number !== -1)
				{
					$numbers[$index]++;
					$count++;
					continue 2;
				}
			}
			foreach ($numbers as $index => $number)
			{
				$numbers[$index]++;
				$count++;
				break;
			}
		}
	}

	public function getInputLines()
	{
		$input = [];
		if ($this->useStd)
		{
			while ($line = fgets(STDIN))
			{
				$input[] = $line;
			}
		}
		else
		{
			$input = '1000
100
0 0 -60 0 20 0 -85 0 0 0 -88 -77 83 0 10 0 98 0 1 68 0 0 0 0 0 0 -46 -95 0 -63 0 0 0 41 5 21 -60 0 -23 0 32 61 0 0 67 -87 73 -88 0 0 0 56 0 27 0 0 0 0 -42 -49 59 0 -74 -34 0 0 42 0 0 0 55 -36 -59 0 0 94 0 0 -58 54 0 2 67 0 0 0 100 0 45 73 -54 54 0 94 0 0 -1 -12 -40 -59
100
-25 0 0 -33 -43 0 0 0 -10 -15 0 58 -23 -90 -82 0 0 0 0 83 0 8 -51 0 -54 0 43 59 0 4 29 0 -23 0 0 43 70 47 -45 0 62 0 -75 -75 0 0 62 -71 0 -36 74 0 0 0 53 0 0 -39 0 97 0 -34 0 0 0 0 55 0 0 0 17 -94 27 32 0 72 14 -47 0 -70 0 -...
';
			$input = explode("\n", trim($input));
		}
		return $input;
	}

	public function printResult($result)
	{
		if ($this->useStd)
		{
			fwrite(STDOUT, $result);
		}
		else
		{
			var_dump($result);
		}
	}

}

$helper = new ArgsHelper(true);
$lines = $helper->getInputLines();

$result = '';
#
array_shift($lines);
foreach ($lines as $index => $line)
{
	if ($index % 2 === 0)
	{
		continue;
	}
	$result .= $helper->calculateResult($line) . ' ';
}
$result = trim($result);
#

$helper->printResult($result);

<?php

class ArgsHelper
{
	private $useStd;

	public function __construct($userStd = false)
	{
		$this->useStd = $userStd;
	}

	public function calculateResult($input)
	{
		$numbers = array_map('intval', explode(' ', $input));
		sort($numbers);
		$numbers = array_reverse($numbers);
		$count = 0;
		while (true)
		{
			$sum = array_sum($numbers);
			$product = array_product($numbers);
			if ($sum !== 0 && $product !== 0)
			{
				return $count;
			}
			foreach ($numbers as $index => $number)
			{
				if ($number === 0)
				{
					$numbers[$index]++;
					$count++;
					continue 2;
				}
			}
			foreach ($numbers as $index => $number)
			{
				if ($number !== -1)
				{
					$numbers[$index]++;
					$count++;
					continue 2;
				}
			}
			foreach ($numbers as $index => $number)
			{
				$numbers[$index]++;
				$count++;
				break;
			}
		}
	}

	public function getInputLines()
	{
		$input = [];
		if ($this->useStd)
		{
			while ($line = fgets(STDIN))
			{
				$input[] = $line;
			}
		}
		else
		{
			$input = '4
3
2 -1 -1
4
-1 0 0 1
2
-1 2
3
0 -2 1
';
			$input = explode("\n", trim($input));
		}
		return $input;
	}

	public function printResult($result)
	{
		if ($this->useStd)
		{
			fwrite(STDOUT, $result);
		}
		else
		{
			var_dump($result);
		}
	}

}

$helper = new ArgsHelper(true);
$lines = $helper->getInputLines();

$result = '';
#
array_shift($lines);
foreach ($lines as $index => $line)
{
	if ($index % 2 === 0)
	{
		continue;
	}
	$result .= $helper->calculateResult($line) . ' ';
}
$result = trim($result);
#

$helper->printResult($result);


<?php
$f = fopen('php://stdin', 'r');
//$f = fopen('test.txt', 'r');

list($n,$m) = explode(" ",trim(fgets($f)));

$a = array();

function check($a,$x,$y){

    $b = array();

    if (isset($a[$x][$y]))
        $b[] = $a[$x][$y];
    else return false;

    if (isset($a[$x+1][$y]))
        $b[] = $a[$x+1][$y];
    else return false;

    if (isset($a[$x][$y+1]))
        $b[] = $a[$x][$y+1];
    else return false;

    if (isset($a[$x+1][$y+1]))
        $b[] = $a[$x+1][$y+1];
    else return false;

    sort($b);

    if ($b[0]=='a' && $b[1]=='c' && $b[2]=='e' && $b[3]=='f') return true;

    return false;
}

$count = 0;

for($i=0;$i<$n;$i++){
    $a[$i] = str_split(trim(fgets($f)));
}

for($i=0;$i<$n;$i++){
    for($j=0;$j<$m;$j++){

        if (check($a,$i,$j)){
            $count++;
        }
    }
}

echo $count;

<?php
$input = fopen("php://stdin", 'r');
$input = stream_get_contents($input);
$input = explode("\n", $input);
$paramsStr = explode(' ',$input[0]);
$width = $paramsStr[1];
$height = $paramsStr[0];
$needsWords = array ('f', 'a', 'c', 'e');
$matrix = array();
for ($i =1; $i <= $height; $i ++){
    for ($j = 0 ; $j < $width; $j ++) {
        $matrix[$i][] = $input[$i][$j];
    }
}

$cnt = 0;
foreach ($matrix as $rowNumber => $row){
    foreach ($row as $colNumber => $word){
        if (in_array($word, $needsWords)){

            $exist = array($word);
            $a = 1;
            //echo "\r\n".$word." ".$matrix[$colNumber + 1][$rowNumber].' '.$matrix[$colNumber][$rowNumber].' '.$matrix[$colNumber][$rowNumber]."\r\n";
            if (isset($matrix[$rowNumber + 1][$colNumber]) && in_array($matrix[$rowNumber + 1][$colNumber], $needsWords) && !in_array($matrix[$rowNumber + 1][$colNumber], $exist)){
                $a++;
                $exist[] = $matrix[$rowNumber + 1][$colNumber];
            }
            if (isset($matrix[$rowNumber + 1][$colNumber + 1]) && in_array($matrix[$rowNumber + 1][$colNumber + 1], $needsWords) && !in_array($matrix[$rowNumber + 1][$colNumber + 1], $exist)){
                $a++;
                $exist[] =$matrix[$rowNumber + 1][$colNumber + 1];
            }
            if (isset($matrix[$rowNumber][$colNumber + 1]) && in_array($matrix[$rowNumber][$colNumber + 1], $needsWords) && !in_array($matrix[$rowNumber][$colNumber + 1], $exist)){
                $a++;
                $exist[] = $matrix[$rowNumber][$colNumber + 1];
            }
            if ($a == 4){
                $cnt ++;
            }
        }
    }
}
echo $cnt;

<?php
$input = fopen("php://stdin", 'r');
$input = stream_get_contents($input);
$input = explode("\n", $input);
$paramsStr = explode(' ',$input[0]);
$width = $paramsStr[0];
$height = $paramsStr[1];
$needsWords = array ('f', 'a', 'c', 'e');
$matrix = array();
for ($i =1; $i <$height; $i ++){
    for ($j = 0 ; $j< $width; $j ++) {
        $matrix[$i][] = $input[$i][$j];
    }
}
$cnt = 0;
foreach ($matrix as $rowNumber => $row){
    echo 'Row: '.$rowNumber."\r\n";
    foreach ($row as $colNumber => $word){
        echo 'Col: '.$colNumber."\r\n";
        if (in_array($word, $needsWords)){
            $exist = array($word);
            $a = 1;
            //echo "\r\n".$word." ".$matrix[$colNumber + 1][$rowNumber].' '.$matrix[$colNumber][$rowNumber].' '.$matrix[$colNumber][$rowNumber]."\r\n";
            if (isset($matrix[$rowNumber + 1][$colNumber]) && in_array($matrix[$rowNumber + 1][$colNumber], $needsWords) && !in_array($matrix[$rowNumber + 1][$colNumber], $exist)){
                $a++;
                $exist[] = $matrix[$rowNumber + 1][$colNumber];
            }
            if (isset($matrix[$rowNumber + 1][$colNumber + 1]) && in_array($matrix[$rowNumber + 1][$colNumber + 1], $needsWords) && !in_array($matrix[$rowNumber + 1][$colNumber + 1], $exist)){
                $a++;
                $exist[] =$matrix[$rowNumber + 1][$colNumber + 1];
            }
            if (isset($matrix[$rowNumber][$colNumber + 1]) && in_array($matrix[$rowNumber][$colNumber + 1], $needsWords) && !in_array($matrix[$rowNumber][$colNumber + 1], $exist)){
                $a++;
                $exist[] = $matrix[$rowNumber][$colNumber + 1];
            }
            if ($a == 4){
                $cnt ++;
            }
        }
    }
}
echo $cnt;

<?php
$input = fopen("php://stdin", 'r');
$input = stream_get_contents($input);
$input = explode("\n", $input);
$paramsStr = explode(' ',$input[0]);
$width = $paramsStr[0];
$height = $paramsStr[1];
$needsWords = array ('f', 'a', 'c', 'e');
$matrix = array();
for ($i =1; $i <$height; $i ++){
    for ($j = 0 ; $j< $width; $j ++) {
        $matrix[$i][] = $input[$i][$j];
    }
}
$cnt = 0;
foreach ($matrix as $rowNumber => $row){
    foreach ($row as $colNumber => $word){
        if (in_array($word, $needsWords)){
            $exist = array($word);
            $a = 1;
            //echo "\r\n".$word." ".$matrix[$colNumber + 1][$rowNumber].' '.$matrix[$colNumber][$rowNumber].' '.$matrix[$colNumber][$rowNumber]."\r\n";
            if (isset($matrix[$rowNumber + 1][$colNumber]) && in_array($matrix[$rowNumber + 1][$colNumber], $needsWords) && !in_array($matrix[$rowNumber + 1][$colNumber], $exist)){
                $a++;
                $exist[] = $matrix[$rowNumber + 1][$colNumber];
            }
            if (isset($matrix[$rowNumber + 1][$colNumber + 1]) && in_array($matrix[$rowNumber + 1][$colNumber + 1], $needsWords) && !in_array($matrix[$rowNumber + 1][$colNumber + 1], $exist)){
                $a++;
                $exist[] =$matrix[$rowNumber + 1][$colNumber + 1];
            }
            if (isset($matrix[$rowNumber][$colNumber + 1]) && in_array($matrix[$rowNumber][$colNumber + 1], $needsWords) && !in_array($matrix[$rowNumber][$colNumber + 1], $exist)){
                $a++;
                $exist[] = $matrix[$rowNumber][$colNumber + 1];
            }
            if ($a == 4){
                $cnt ++;
            }
        }
    }
}
echo $cnt;

<?php
$input = fopen("php://stdin", 'r');
$input = stream_get_contents($input);
$input = explode("\n", $input);
$paramsStr = explode(' ',$input[0]);
$width = $paramsStr[0];
$height = $paramsStr[1];
$needsWords = array ('f', 'a', 'c', 'e');
$matrix = array();
for ($i =1; $i <$height; $i ++){
    for ($j = 0 ; $j< $width; $j ++) {
        $matrix[$i][] = $input[$i][$j];
    }
}
$cnt = 0;
foreach ($matrix as $rowNumber => $row){
    foreach ($row as $colNumber => $word){
        if (in_array($word, $needsWords)){
            $exist = array($word);
            $a = 1;
            if (isset($matrix[$colNumber + 1][$rowNumber]) && in_array($matrix[$colNumber + 1][$rowNumber], $needsWords) && !in_array($matrix[$colNumber + 1][$rowNumber], $exist)){
                $a++;
                $exist[] = $matrix[$colNumber + 1];
            }

            if (isset($matrix[$colNumber + 1][$rowNumber + 1]) && in_array($matrix[$colNumber + 1][$rowNumber + 1], $needsWords) && !in_array($matrix[$colNumber + 1][$rowNumber + 1], $exist)){
                $a++;
                $exist[] = $matrix[$colNumber + 1];
            }

            if (isset($matrix[$colNumber][$rowNumber + 1]) && in_array($matrix[$colNumber][$rowNumber + 1], $needsWords) && !in_array($matrix[$colNumber][$rowNumber + 1], $exist)){
                $a++;
                $exist[] = $matrix[$colNumber + 1];
            }

            if ($a == 4){
                $cnt ++;
            }
        }
    }
}
echo $cnt;

<?php
$input = fopen("php://stdin", 'r');
$input = stream_get_contents($input);
$input = explode("\n", $input);
$paramsStr = explode(' ',$input[0]);
$width = $paramsStr[1];
$height = $paramsStr[0];
$needsWords = array ('f', 'a', 'c', 'e');
$matrix = array();
for ($i =1; $i <$height; $i ++){
    for ($j = 0 ; $j< $width; $j ++) {
        $matrix[$i][] = $input[$i][$j];
    }
}

$cnt = 0;
foreach ($matrix as $rowNumber => $row){
    foreach ($row as $colNumber => $word){
        if (in_array($word, $needsWords)){
            $exist = array($word);
            $a = 1;
            //echo "\r\n".$word." ".$matrix[$colNumber + 1][$rowNumber].' '.$matrix[$colNumber][$rowNumber].' '.$matrix[$colNumber][$rowNumber]."\r\n";
            if (isset($matrix[$rowNumber + 1][$colNumber]) && in_array($matrix[$rowNumber + 1][$colNumber], $needsWords) && !in_array($matrix[$rowNumber + 1][$colNumber], $exist)){
                $a++;
                $exist[] = $matrix[$rowNumber + 1][$colNumber];
            }
            if (isset($matrix[$rowNumber + 1][$colNumber + 1]) && in_array($matrix[$rowNumber + 1][$colNumber + 1], $needsWords) && !in_array($matrix[$rowNumber + 1][$colNumber + 1], $exist)){
                $a++;
                $exist[] =$matrix[$rowNumber + 1][$colNumber + 1];
            }
            if (isset($matrix[$rowNumber][$colNumber + 1]) && in_array($matrix[$rowNumber][$colNumber + 1], $needsWords) && !in_array($matrix[$rowNumber][$colNumber + 1], $exist)){
                $a++;
                $exist[] = $matrix[$rowNumber][$colNumber + 1];
            }
            if ($a == 4){
                $cnt ++;
            }
        }
    }
}
echo $cnt;


<?php
/*****************************************/
class InputReader {protected $_fh;public function __construct($fileHandler = STDIN){$this->_fh = $fileHandler;}
	public function readInt() {$d = trim(fgets($this->_fh));return (int)$d;}
	public function readString() {$d = trim(fgets($this->_fh));return $d;}
	public function readArrayOfInt($size, $indexFrom = 0) {$a = [];$ind = $indexFrom;foreach (explode(' ', fgets($this->_fh)) as $item) {$a[$ind++] = (int)$item;}return $a;}
	public function readArrayOfString($size, $indexFrom = 0) {$a = [];$ind = $indexFrom;foreach (explode(' ', fgets($this->_fh)) as $item) {$a[$ind++] = $item;}return $a;}
}

class Utils {
	/* Perform action N times. Can return values in array */
	static public function ntimes($n, $func, $returnResults = false) {$a = [];for ($i=1; $i<=$n; $i++) {$result = $func();if ($returnResults) {$a[$i] = $result;}}return $a;}
	/* Integral. Keys preserved. By reference. */
	static public function cumsum(&$a){$sum = 0;foreach($a as $k=>$v) {$a[$k] += $sum;$sum = $a[$k];}}
	/* Difference. Keys preserved. By reference. */
	static public function diff(&$a){$prev = 0;foreach($a as $k=>$v) {$buf=$a[$k];$a[$k]-=$prev;$prev = $buf;}}
	static public function bitCount($n){$cnt = 0;for ($j=$n; $j; $j>>=1){$cnt += $j&1;}return $cnt;}
}

class PrimeUtils {static protected $divisors = [1 => [1=>true],2 => [1=>true, 2=>true],];static protected $primes = [];
	static public function primes($limit){$a = str_repeat(chr(85), ($limit>>3) + 1);$a[0] = chr(83);for ($i=3; $i*$i < $limit; $i+=2) {if (ord($a[$i>>3]) & (1<<($i%8))) {;} else {for ($k=$i; $k*$i<$limit; $k++) {$n = $k*$i;$a[$n>>3] = chr(ord($a[$n>>3]) | (1<<($n%8)));}}}yield 2;for ($i=3; $i<$limit; $i+=2) {if (!(ord($a[$i>>3]) & (1<<($i%8)))) {yield $i;}}}
	static public function divisors($n, $lp = 2) {if (isset(self::$divisors[$n])) {return self::$divisors[$n];}if (empty(self::$primes)) {$primes = [];foreach (self::primes(1e3) as $prime) {$primes[$prime] = $prime;}self::$primes = $primes;}$r = [];foreach (self::$primes as $p) {if ($p>=$lp) {if ($n < $p*$p) {$r = [1=>true, $n=>true];break;} elseif ($n%$p==0) {foreach([1, $p] as $pre) {foreach(self::divisors(intdiv($n,$p), $p) as $nxt=>$v) {$r[$nxt*$pre] = true;}}break;}}}self::$divisors[$n] = $r;return self::$divisors[$n];}
}

/*****************************************/

$ir = new InputReader();

/* TASK */

$s = $ir->readString();
$n = strlen($s);
$lb = null;
$rb = null;
$rsc = null;
$lsc = null;

for ($i=0; $i<$n; $i++) {
	if ($s[$i]=='[') {
		$lb = $i;
		break;
	}
}

if ($lb===null) {
	echo -1; exit();
}
//echo $lb;
for ($i=$lb+1; $i<$n; $i++) {
	if ($s[$i]==':') {
		$lsc = $i;
		break;
	}
}

if ($lsc===null) {
	echo -1; exit();
}
//echo $lsc;
for ($i=$n-1; $i>$lsc; $i--) {
	if ($s[$i]==']') {
		$rb = $i;
		break;
	}
}

if ($rb===null) {
	echo -1; exit();
}
//echo $rb;
for ($i=$rb-1; $i>=$lsc; $i--) {
	if ($s[$i]==':') {
		$rsc = $i;
		break;
	}
}

if ($rsc===null) {
	echo -1; exit();
}
//echo $rsc;
if ($rsc===$lsc) {
	echo -1; exit();
}

$bars = 0;
for ($i=$lsc+1; $i<=$rsc-1; $i++) {
	if ($s[$i] == '|') {
		$bars++;
	}
}

echo $bars+4;

<?php

$sStr = trim(fgets(STDIN));

$sStr = preg_replace('~[a-z0-9]+~','',$sStr);
//echo $sStr . "\n";

$iCount = -1;
if(($i = strpos($sStr, '[')) !== false) {
    $iCount = 1;
    $sStr = substr($sStr, $i + 1);
//    echo 1 . ' [ ' . $i .  "\n";
    if(($i1 = strpos($sStr, ':')) !== false) {
        $sStr = substr($sStr, $i1 + 1);
        $iCount++;
//        echo 2 . ' : ' . $i1 .  "\n";

        if(($i2 = strrpos($sStr, ']')) !== false) {
            $iCount++;
            $sStr = substr($sStr, 0, $i2);
//            echo $iCount . ' : ' . $i2 .  "\n";
            if(($i3 = strrpos($sStr, ':')) !== false) {
                $iCount++;
                $sStr = substr($sStr, 0, $i3);
//                echo $iCount . ' ] ' . $i3 . "\n";

                $i1 = -1;
                $sStr = preg_replace('~[\[\]\:]+~','',$sStr);
//                echo $sStr . "\n";
//                $iCount += strlen($sStr);
                while (true) {
                    if (($i4 = strpos($sStr, '|', $i1 + 1)) !== false) {

                        $iCount++;
//                        echo $iCount . ' | ' . $i2 . "\n";

                        $i1 = $i4;
                    } else {
                        break;
                    }
                }
            } else {
                $iCount = -1;
            }
        } else {
            $iCount = -1;
        }
    } else {
        $iCount = -1;
    }
}

echo $iCount;

<?php
/*****************************************/
class InputReader {protected $_fh;public function __construct($fileHandler = STDIN){$this->_fh = $fileHandler;}
	public function readInt() {$d = trim(fgets($this->_fh));return (int)$d;}
	public function readString() {$d = trim(fgets($this->_fh));return $d;}
	public function readArrayOfInt($size, $indexFrom = 0) {$a = [];$ind = $indexFrom;foreach (explode(' ', fgets($this->_fh)) as $item) {$a[$ind++] = (int)$item;}return $a;}
	public function readArrayOfString($size, $indexFrom = 0) {$a = [];$ind = $indexFrom;foreach (explode(' ', fgets($this->_fh)) as $item) {$a[$ind++] = $item;}return $a;}
}

class Utils {
	/* Perform action N times. Can return values in array */
	static public function ntimes($n, $func, $returnResults = false) {$a = [];for ($i=1; $i<=$n; $i++) {$result = $func();if ($returnResults) {$a[$i] = $result;}}return $a;}
	/* Integral. Keys preserved. By reference. */
	static public function cumsum(&$a){$sum = 0;foreach($a as $k=>$v) {$a[$k] += $sum;$sum = $a[$k];}}
	/* Difference. Keys preserved. By reference. */
	static public function diff(&$a){$prev = 0;foreach($a as $k=>$v) {$buf=$a[$k];$a[$k]-=$prev;$prev = $buf;}}
	static public function bitCount($n){$cnt = 0;for ($j=$n; $j; $j>>=1){$cnt += $j&1;}return $cnt;}
}

class PrimeUtils {static protected $divisors = [1 => [1=>true],2 => [1=>true, 2=>true],];static protected $primes = [];
	static public function primes($limit){$a = str_repeat(chr(85), ($limit>>3) + 1);$a[0] = chr(83);for ($i=3; $i*$i < $limit; $i+=2) {if (ord($a[$i>>3]) & (1<<($i%8))) {;} else {for ($k=$i; $k*$i<$limit; $k++) {$n = $k*$i;$a[$n>>3] = chr(ord($a[$n>>3]) | (1<<($n%8)));}}}yield 2;for ($i=3; $i<$limit; $i+=2) {if (!(ord($a[$i>>3]) & (1<<($i%8)))) {yield $i;}}}
	static public function divisors($n, $lp = 2) {if (isset(self::$divisors[$n])) {return self::$divisors[$n];}if (empty(self::$primes)) {$primes = [];foreach (self::primes(1e3) as $prime) {$primes[$prime] = $prime;}self::$primes = $primes;}$r = [];foreach (self::$primes as $p) {if ($p>=$lp) {if ($n < $p*$p) {$r = [1=>true, $n=>true];break;} elseif ($n%$p==0) {foreach([1, $p] as $pre) {foreach(self::divisors(intdiv($n,$p), $p) as $nxt=>$v) {$r[$nxt*$pre] = true;}}break;}}}self::$divisors[$n] = $r;return self::$divisors[$n];}
}

/*****************************************/

$ir = new InputReader();

/* TASK */

$s = $ir->readString();

$lb = null;
$rb = null;
$rsc = null;
$lsc = null;

for ($i=0; $i<strlen($s); $i++) {
	if ($s[$i]=='[') {
		$lb = $i;
		break;
	}
}

if ($lb===null) {
	echo -1; exit();
}

for ($i=$lb+1; $i<strlen($s); $i++) {
	if ($s[$i]==':') {
		$lsc = $i;
		break;
	}
}

if ($lsc===null) {
	echo -1; exit();
}

for ($i=$n-1; $i<=$lsc; $i--) {
	if ($s[$i]==']') {
		$rb = $i;
		break;
	}
}

if ($rb===null) {
	echo -1; exit();
}

for ($i=$rb-1; $i<=$lsc; $i--) {
	if ($s[$i]==':') {
		$rsc = $i;
		break;
	}
}

if ($rsc===null) {
	echo -1; exit();
}

if ($rsc===$lsc) {
	echo -1; exit();
}

$bars = 0;
for ($i=$lsc+1; $i<=$rsc-1; $i++) {
	if ($s[$i] == '|') {
		$bars++;
	}
}

echo $bars+4;

<?php
/*****************************************/
class InputReader {protected $_fh;public function __construct($fileHandler = STDIN){$this->_fh = $fileHandler;}
	public function readInt() {$d = trim(fgets($this->_fh));return (int)$d;}
	public function readString() {$d = trim(fgets($this->_fh));return $d;}
	public function readArrayOfInt($size, $indexFrom = 0) {$a = [];$ind = $indexFrom;foreach (explode(' ', fgets($this->_fh)) as $item) {$a[$ind++] = (int)$item;}return $a;}
	public function readArrayOfString($size, $indexFrom = 0) {$a = [];$ind = $indexFrom;foreach (explode(' ', fgets($this->_fh)) as $item) {$a[$ind++] = $item;}return $a;}
}

class Utils {
	/* Perform action N times. Can return values in array */
	static public function ntimes($n, $func, $returnResults = false) {$a = [];for ($i=1; $i<=$n; $i++) {$result = $func();if ($returnResults) {$a[$i] = $result;}}return $a;}
	/* Integral. Keys preserved. By reference. */
	static public function cumsum(&$a){$sum = 0;foreach($a as $k=>$v) {$a[$k] += $sum;$sum = $a[$k];}}
	/* Difference. Keys preserved. By reference. */
	static public function diff(&$a){$prev = 0;foreach($a as $k=>$v) {$buf=$a[$k];$a[$k]-=$prev;$prev = $buf;}}
	static public function bitCount($n){$cnt = 0;for ($j=$n; $j; $j>>=1){$cnt += $j&1;}return $cnt;}
}

class PrimeUtils {static protected $divisors = [1 => [1=>true],2 => [1=>true, 2=>true],];static protected $primes = [];
	static public function primes($limit){$a = str_repeat(chr(85), ($limit>>3) + 1);$a[0] = chr(83);for ($i=3; $i*$i < $limit; $i+=2) {if (ord($a[$i>>3]) & (1<<($i%8))) {;} else {for ($k=$i; $k*$i<$limit; $k++) {$n = $k*$i;$a[$n>>3] = chr(ord($a[$n>>3]) | (1<<($n%8)));}}}yield 2;for ($i=3; $i<$limit; $i+=2) {if (!(ord($a[$i>>3]) & (1<<($i%8)))) {yield $i;}}}
	static public function divisors($n, $lp = 2) {if (isset(self::$divisors[$n])) {return self::$divisors[$n];}if (empty(self::$primes)) {$primes = [];foreach (self::primes(1e3) as $prime) {$primes[$prime] = $prime;}self::$primes = $primes;}$r = [];foreach (self::$primes as $p) {if ($p>=$lp) {if ($n < $p*$p) {$r = [1=>true, $n=>true];break;} elseif ($n%$p==0) {foreach([1, $p] as $pre) {foreach(self::divisors(intdiv($n,$p), $p) as $nxt=>$v) {$r[$nxt*$pre] = true;}}break;}}}self::$divisors[$n] = $r;return self::$divisors[$n];}
}

/*****************************************/

$ir = new InputReader();

/* TASK */

$s = $ir->readString();

$lb = null;
$rb = null;
$rsc = null;
$lsc = null;
for ($i=0; $i<strlen($s); $i++) {
	if ($s[$i]=='[') $lb = $lb ?? $i;
	if ($lb!==null && $s[$i]==':') $lsc = $lsc ?? $i;
	if ($lb!==null && $lsc!==null) break;
}

for ($i=strlen($s)-1; $i>=0; $i--) {
	if ($s[$i]==']') $rb = $rb ?? $i;
	if ($rb!==null && $s[$i]==':') $rsc = $rsc ?? $i;
	if ($rb!==null && $rsc!==null) break;
}

//var_dump([$lb, $lsc, $rsc, $rb]);
$bars = 0;
if ($rb>$lb && $lsc!==null && $rsc!==null && $lsc!=$rsc) {
	for ($i=$rsc; $i<$lsc; $i++) {
		if ($s[$i] == '|') {
			$bars++;
		}
	}

	echo $bars+4;
}
else {
	echo -1;
}

<?php

$sStr = trim(fgets(STDIN));

$sStr = preg_replace('~[a-b]+~','',$sStr);
//echo $sStr;exit;

$iCount = -1;
if(($i = strpos($sStr, '[')) !== false) {
    $iCount = 1;
    $sStr = substr($sStr, $i + 1);
//    echo 1 . ' [ ' . $i .  "\n";
    if(($i1 = strpos($sStr, ':')) !== false) {
        $sStr = substr($sStr, $i1 + 1);
        $iCount++;
//        echo 2 . ' : ' . $i1 .  "\n";

        if(($i2 = strrpos($sStr, ':')) !== false) {
            $iCount++;
//            echo $iCount . ' : ' . $i2 .  "\n";
            if(($i3 = strpos($sStr, ']', $i2 + 1)) !== false) {
                $iCount++;
                $sStr = substr($sStr, 0, $i2);
//                echo $iCount . ' ] ' . $i3 . "\n";

                $i1 = -1;
                while (true) {
                    if (($i4 = strpos($sStr, '|', $i1 + 1)) !== false) {

                        $iCount++;
//                        echo $iCount . ' | ' . $i2 . "\n";

                        $i1 = $i4;
                    } else {
                        break;
                    }
                }
            } else {
                $iCount = -1;
            }
        } else {
            $iCount = -1;
        }
    } else {
        $iCount = -1;
    }
}

echo $iCount;

<?php

$sStr = trim(fgets(STDIN));

$sStr = preg_replace('~[a-b]+~','',$sStr);
//echo $sStr . "\n";

$iCount = -1;
if(($i = strpos($sStr, '[')) !== false) {
    $iCount = 1;
    $sStr = substr($sStr, $i + 1);
//    echo 1 . ' [ ' . $i .  "\n";
    if(($i1 = strpos($sStr, ':')) !== false) {
        $sStr = substr($sStr, $i1 + 1);
        $iCount++;
//        echo 2 . ' : ' . $i1 .  "\n";

        if(($i2 = strrpos($sStr, ']')) !== false) {
            $iCount++;
            $sStr = substr($sStr, 0, $i2);
//            echo $iCount . ' : ' . $i2 .  "\n";
            if(($i3 = strrpos($sStr, ':')) !== false) {
                $iCount++;
                $sStr = substr($sStr, 0, $i3);
//                echo $iCount . ' ] ' . $i3 . "\n";

                $i1 = -1;
                $sStr = preg_replace('~[\[\]\:0-9]+~','',$sStr);
//                echo $sStr . "\n";
                $iCount += strlen($sStr);
//                while (true) {
//                    if (($i4 = strpos($sStr, '|', $i1 + 1)) !== false) {
//
//                        $iCount++;
////                        echo $iCount . ' | ' . $i2 . "\n";
//
//                        $i1 = $i4;
//                    } else {
//                        break;
//                    }
//                }
            } else {
                $iCount = -1;
            }
        } else {
            $iCount = -1;
        }
    } else {
        $iCount = -1;
    }
}

echo $iCount;

<?php

$sStr = trim(fgets(STDIN));

$iCount = -1;
if(($i = strpos($sStr, '[')) !== false) {
    $iCount = 1;
//    echo 1 . ' [ ' . $i .  "\n";
    if(($i1 = strpos($sStr, ':', $i + 1)) !== false) {

        $iCount++;
//        echo 2 . ' : ' . $i1 .  "\n";

        if(($i2 = strrpos($sStr, ':', $i1 + 1)) !== false) {
            $iCount++;
//            echo $iCount . ' : ' . $i2 .  "\n";
            if(($i3 = strpos($sStr, ']', $i2 + 1)) !== false) {
                $iCount++;
//                echo $iCount . ' ] ' . $i3 . "\n";

                while (true) {
                    if (($i4 = strpos($sStr, '|', $i1 + 1)) !== false) {

                        if ($i4 >= $i2) {
                            break;
                        }

                        $iCount++;
//                        echo $iCount . ' | ' . $i2 . "\n";

                        $i1 = $i4;
                    } else {
                        break;
                    }
                }
            } else {
                $iCount = -1;
            }
        } else {
            $iCount = -1;
        }
    } else {
        $iCount = -1;
    }
}

echo $iCount;

<?php

$sStr = trim(fgets(STDIN));

$sStr = preg_replace('~[a-b]+~','',$sStr);
//echo $sStr;exit;

$iCount = -1;
if(($i = strpos($sStr, '[')) !== false) {
    $iCount = 1;
//    echo 1 . ' [ ' . $i .  "\n";
    if(($i1 = strpos($sStr, ':', $i + 1)) !== false) {

        $iCount++;
//        echo 2 . ' : ' . $i1 .  "\n";

        if(($i2 = strrpos($sStr, ':', $i1 + 1)) !== false) {
            $iCount++;
//            echo $iCount . ' : ' . $i2 .  "\n";
            if(($i3 = strpos($sStr, ']', $i2 + 1)) !== false) {
                $iCount++;
//                echo $iCount . ' ] ' . $i3 . "\n";

                while (true) {
                    if (($i4 = strpos($sStr, '|', $i1 + 1)) !== false) {

                        if ($i4 > $i2) {
                            break;
                        }

                        $iCount++;
//                        echo $iCount . ' | ' . $i2 . "\n";

                        $i1 = $i4;
                    } else {
                        break;
                    }
                }
            } else {
                $iCount = -1;
            }
        } else {
            $iCount = -1;
        }
    } else {
        $iCount = -1;
    }
}

echo $iCount;


<?php
$server = "php://stdin";
$local = "input.txt";
$handle = fopen($server, "r");
$t= trim(fgets($handle));

for($j=0;$j<$t;$j++)
{
    $n= trim(fgets($handle));
    $s= trim(fgets($handle));
    $sum=0;
    if($n==1){echo '-1'."\n";}
    else {
        $str='';$check=0;$ch=false;
       for ($i=0;$i<$n;$i++)
       {
           if($s[$i]%2!=0)
           {$str.=$s[$i];$sum=$sum+(int)$s[$i];$check++;}

           if($sum%2==0 && $check>1){$ch=true;break;}
       }
       if($ch)
       {
           echo $str."\n";
       }
       else
       {
           echo '-1'."\n";
       }
    }
}

?>

<?php
$server = "php://stdin";
$local = "input.txt";
$handle = fopen($server, "r");
$t= trim(fgets($handle));

for($j=0;$j<$t;$j++)
{
    $n= trim(fgets($handle));
    $s= trim(fgets($handle));
    $sum=0;
    if($n==1){echo '-1'."\n";}
    else {
        $str='';$check=0;
       for ($i=0;$i<$n;$i++)
       {
           if($s[$i]%2!=0)
           {echo $s[$i];$str.=$s[$i];$sum=$sum+(int)$s[$i];$check++;}

           if($sum%2==0 && $check>1){break;}
       }
       echo "\n";
    }
}

?>

<?php
$server = "php://stdin";
$local = "input.txt";
$handle = fopen($server, "r");
$t= trim(fgets($handle));

for($j=0;$j<$t;$j++)
{
    $n= trim(fgets($handle));
    $s= trim(fgets($handle));
    $sum=0;
    if($n==1){echo '-1'."\n";}
    else {
        for ($i = 0; $i < $n; $i++) $sum += (int)$s[$i];
       $str=$s;
       for ($i=$n-1;$i>0;$i--)
       {
           //var_dump($str);
           if($str[$i]%2!=0)
           {
               if($sum%2==0)
               {
                   echo substr($str, 0,$i+1)."\n";
                   break;
               }
               else
               {
                   $sum=$sum-$str[$i];
                   $str=substr($str, 0,$i);
               }
           }else
           {
               $sum=$sum-$str[$i];
               $str=substr($str, 0,$i);

           }

       }
    }
}

?>

<?php
$server = "php://stdin";
$local = "input.txt";
$handle = fopen($server, "r");
$t= trim(fgets($handle));

for($j=0;$j<$t;$j++)
{
    $n= trim(fgets($handle));
    $s= trim(fgets($handle));
    $sum=0;
    if($n==1){echo '-1'."\n";}
    else {
        for ($i = 0; $i < $n; $i++) $sum += (int)$s[$i];
       $str=$s;
       for ($i=$n-1;$i>0;$i--)
       {
           var_dump($str);
           if($str[$i]%2!=0)
           {
               if($sum%2==0)
               {
                   echo substr($str, 0,$i+1)."\n";
                   break;
               }
               else
               {
                   $sum=$sum-$str[$i];
                   $str=substr($str, 0,$i);
               }
           }else
           {
               $sum=$sum-$str[$i];
               $str=substr($str, 0,$i);

           }

       }
    }
}

?>


<? $f = file('php://stdin',FILE_IGNORE_NEW_LINES);
define('L',PHP_EOL);
//-----------------------

array_shift($f);

foreach ($f as $p) {
	$p = explode(' ',$p);
	array_shift($p);

	$player[] = $p;
}

$player_c = $player;

foreach ($player as $i=>$poll) {

	$turns = sizeof($poll);

	$res = 'YES';

	foreach ($player_c as $j=>$c) if ($i!=$j) {
		$x = 0;

		foreach ($poll as $p) {
			if (($index = array_search($p,$c))!==false) {
				$x++;
				unset($c[$index]);

				if (!$c) {
					break;
				}
			}
		}

		if (!$c && $x && $x<=$turns) {
			$res = 'NO';
			break;
		}
	}

	$result[] = $res;
}

echo implode(L,$result);

<? $f = file('php://stdin',FILE_IGNORE_NEW_LINES);
define('L',PHP_EOL);
//-----------------------

array_shift($f);

foreach ($f as $p) {
	$p = explode(' ',$p);
	array_shift($p);

	$player[] = $p;
}

$player_c = $player;

foreach ($player as $i=>$poll) {

	$turns = sizeof($poll);

	$res = 'YES';

	foreach ($player_c as $j=>$c) if ($i!=$j) {
		$first = current($c);
		$x = 0;

		foreach ($poll as $p) {
			if ($p==$first) {
				$x++;
				array_shift($c);

				if ($c) {
					$first = current($c);
				} else {
					break;
				}
			}
		}
		if (!$c && $x && $x<=$turns) {
			$res = 'NO';
			break;
		}
	}

	$result[] = $res;
}

echo implode(L,$result);