#!/usr/bin/perl

use strict;
use warnings;

my $n=<>;
my $up=<>;
my @current=split(' ',<>);
my $last=7-$up;
my $can=1;
foreach my $count(1..$n-1){
	@current=split(' ',<>);
	if($last==$current[0] || $last==7-$current[0] || $last==$current[1] || $last==7-$current[1]){
		$can=0;
	}
}
if($can==1){
	print"YES\n";
}else{
	print"NO\n";
}

chomp($totalDice = <STDIN>);
chomp($topFace = <STDIN>);
$bottomFace = 7 - $topFace;
@diceFaces = ( 1, 2, 3, 4, 5, 6);
@topBottomFaces = ( $topFace, $bottomFace);
my %diceFaces=map{$_ =>1} @diceFaces;
my %topBottomFaces=map{$_=>1} @topBottomFaces;
my @remFaces=grep(!defined $topBottomFaces{$_}, @diceFaces);
while (<STDIN>) { # assigns each line in turn to $_
    #print "Just read in this line: $_";
    $currentLine = $_;
    if($currentLine =~ /([1-6]) ([1-6])/) {
        $leftFace = $1;
        $rightFace = $2;
        if ( grep { $_ eq $leftFace } @remFaces ) {
        }
        else {
            print "NO";
            exit 0;
        }
        if ( grep { $_ eq $rightFace } @remFaces ) {
        }
        else {
            print "NO";
            exit 0;
        }
    }
    $totalDice--;
}
if ( $totalDice == 0) {
    print "YES";
}
else {
    print "NO";
}

chomp($totalDice = <STDIN>);
$topFace = <STDIN>;
$bottomFace = 7 - $topFace;
@diceFaces = ( 1, 2, 3, 4, 5, 6);
@topBottomFaces = ( $topFace, $bottomFace);
@diff{ @diceFaces } = { @diceFaces };
delete @diff{ @topBottomFaces };
@remFaces = (keys %diff);
while (<STDIN>) { # assigns each line in turn to $_
	#print "Just read in this line: $_";
	$currentLine = $_;
	if($currentLine =~ /([1-6]) ([1-6])/) {
		$leftFace = $1;
		$rightFace = $2;
		if ( grep { $_ eq $leftFace } @remFaces ) {
		}
		else {
			print "NO";
			exit 0;
		}
		if ( grep { $_ eq $leftFace } @remFaces ) {
		}
		else {
			print "NO";
			exit 0;
		}
	}
	$totalDice--;
}
if ( $totalDice == 0) {
	print "YES";
}
else {
	print "NO";
}

chomp($totalDice = <STDIN>);
$topFace = <STDIN>;
$bottomFace = 7 - $topFace;
@diceFaces = ( 1, 2, 3, 4, 5, 6);
@topBottomFaces = ( $topFace, $bottomFace);
@diff{ @diceFaces } = { @diceFaces };
delete @diff{ @topBottomFaces };
@remFaces = (keys %diff);
while (<STDIN>) { # assigns each line in turn to $_
    $currentLine = $_;
    if($currentLine =~ /([1-6]) ([1-6])/) {
        $leftFace = $1;
        $rightFace = $2;
        if ( grep { $_ eq $leftFace } @remFaces ) {
        }
        else {
            print "NO";
            exit 0;
        }
        if ( grep { $_ eq $rightFace } @remFaces ) {
        }
        else {
            print "NO";
            exit 0;
        }
    }
    $totalDice--;
}
if ( $totalDice == 0) {
    print "YES";
}
else {
    print "NO";
}


#!/usr/bin/perl
use v5.10;

@a = (0, split / /, <>);
chomp($line = <>);
$tb{int($_)}++ foreach (split //, $line);
$ans+=$a[$_] * $tb{$_} foreach(keys %tb);
say $ans;

#!/usr/bin/perl

my @cal = split / /, <>;
my $s = <>;
chomp $s;

print eval join '+', map { $cal[$_ - 1] } (split //, $s);

use strict;
#open STDIN, '<input.txt';
#open STDOUT, '>output.txt';
my @a = split(' ', <>);
my @b = split(//, <>);
my $ans = 0;
sub kek{
  my ($a, $b) = @_;
for(my $i = 0; $i < $#b; $i++){
  print $a[$b[$i] - 1], ' ';
  $ans += $a[$b[$i] - 1];
}
print $ans;
}
kek(\@a, \@b);


#!/usr/bin/perl

my ($tcase) = split(/\s+/o,<STDIN>);

while( $tcase -- > 0 ){
  ( my $s = <STDIN> ) =~ s/[\x00-\x20]+$//ios;
  if( length($s) % 2 == 1 ){
    print "No\n"; next;
  }
  if( substr($s,0,length($s)/2) eq substr($s,length($s)/2,length($s)/2) ){
    print "Yes\n";
  } else {
    print "No\n";
  }
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	print m/^(.+)\1$/ ? "YES" : "NO";

	}

#!/usr/bin/perl

my ($tcase) = split(/\s+/o,<STDIN>);

while( $tcase -- > 0 ){
  ( my $s = <STDIN> ) =~ s/[\x00-\x20]+$//ios;
  if( length($s) % 2 == 1 ){
    print "No\n"; next;
  }
  print ( substr($s,0,length($s)/2) . "\n" );
}

#!/usr/bin/perl

my ($tcase) = split(/\s+/o,<STDIN>);

while( $tcase -- > 0 ){
  ( my $s = <STDIN> ) =~ s/[\x00-\x20]+$//ios;
  my %x = ();
  for(my $i=0;$i<length($s);$i++){
    for(my $j=$i;$j<length($s);$j++){
      my $ss = substr($s,$i,$j-$i+1);
      $x{$ss}++;
    }
  }
  my $res = 'No';
	foreach my $k (keys %x){
    $res = 'Yes' if $x{$k}-0 > 1;
	}
  print "$res\n";
}


#!/usr/bin/perl -wl
use strict;

while(<>) {
    @_ = reverse map { my @k = split ' ', <>; $k[1] } 1..$_;
    chomp(my $k = <>);
    1 while scalar(@_) > 0 and eval $k.'-'.pop @_ > 0;
    print scalar(@_) + 1;
}

#!/usr/bin/perl -wl

use strict;

while( <> ){
    $_ = join ',', map { '-' x ( 1 + eval join '-', reverse split ' ', <> ) } 1 .. $_;
    s/-*\K-/;/g;
    y/,//d;
    chomp( my $k = <> );
    substr $_, 0, $k - 1, '';
    print scalar( () = /;/g );
    }


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	@_ = split ' ', <>;

	my %h;
	my $i = 0;
	map { $h{ ++ $i } = $_ } @_;
	my @c;

	for my $key ( 1 .. @_ ){
		next if not exists $h{ $key };
		my $new = $key;
		my $c = 0;
		while( exists $h{ $new } ){
			$c ++;
			my $old = $new;
			$new = $h{ $old };
			delete $h{ $old };
			}
		push @c, $c;
		}

	@c = ( sort { $a <=> $b } @c );

	if( @c >= 2 ){
		push @c, ( pop @c ) + ( pop @c );
		}

	my $sum = 0;
	map { $sum += $_ ** 2 } @c;

	print $sum;
	}


#!/usr/bin/perl

use warnings;
use strict;

use Data::Dumper;

my $keyboards = <>;
my $numbers = <>;

my @splitted = split(" ", $numbers);

my $min;
my $max;
foreach my $one (@splitted) {
	if (! defined $min || $min > $one) {
	      $min = $one;
	}
	if (! defined $max || $one > $max) {
		$max = $one;
	}
}

my $result = $max - $min + 1 - $keyboards;

print "$result\n";

use v5.10;

chomp($n = <STDIN>);
chomp($piano = <STDIN>);
@pianoList = split / /, $piano;
@pianoList = sort {$a<=>$b} @pianoList;
say $pianoList[$#pianoList] - $pianoList[0] - $n + 1;


#!/usr/bin/perl

while (<>) {
    chomp;
    $i=$j=0;
    s/heavy/H/g;
    s/metal/M/g;
    s/[a-z]//g;
    @m=split//;
    foreach (@m){
        /H/ and $i++;
        /M/ and $j+=$i;
        }
    print "$j\n";
}

#!/usr/bin/perl
chomp ($_ = <>);
$i = 0;
$res = 0;
for ($i = 0; $i < length ($_) - 4; $i++) {
    $block = substr $_, $i, 5;

    if ($block eq "heavy") {
        $h++;
        $i += 4;
    } elsif ($block eq "metal") {
        $res += $h;
        $i += 4;
    }
}

print $res;

#!/usr/bin/perl -pi
for ($i = 0; $i < length ($_); $i++) {
    $block = substr $_, $i, 5;

    if ($block eq "heavy") {
        $h++;
        $i += 4;
    } elsif ($block eq "metal") {
        $res += $h;
        $i += 4;
    }
}
$_ = $res;

#!/usr/bin/perl

while (<>) {
    chomp;
    s/heavy/H/g;
    s/metal/M/g;
    s/[a-z]//g;
    @m=split//;
    foreach (@m){
        /H/ and $i++;
        /M/ and $j+=$i;
        }
    print "$j\n";
    $i=$j=0;
}


#!/usr/bin/perl
use v5.10;

chomp($n = <>);
push @a, [split //, <>] foreach (0 .. $n-1);
$ch = $a[0][0];
$a[$_][$_] ne $ch and say "NO" and exit foreach(0 .. $n-1);
$a[$_][$n-1-$_] ne $ch and say "NO" and exit foreach(0 .. $n-1);
$a[1][0] eq $ch and say "NO" and exit;
$ch = $a[1][0];
foreach $i (0 .. $n-1) {
	foreach $j (0 .. $n-1) {
		($j==$i || $j==$n-1-$i) and next;
		$a[$i][$j] ne $ch and say "NO" and exit;
	}
}
say "YES";

#!/usr/bin/perl
use v5.10;

chomp($n = <>);
push @a, [split //, <>] foreach (0 .. $n-1);
$ch = $a[0][0];
$a[$_][$_] ne $ch and say "NO" and exit foreach(0 .. $n-1);
$a[$_][$n-1-$_] ne $ch and say "NO" and exit foreach(0 .. $n-1);
$ch = $a[1][0];
foreach $i (0 .. $n-1) {
	foreach $j (0 .. $n-1) {
		($j==$i || $j==$n-1-$i) and next;
		$a[$i][$j] ne $ch and say "NO" and exit;
	}
}
say "YES";


my $n   = <>;
my @arr = split " ", <>;
my $l   = scalar @arr - 1;
my $ans = 0;
@arr = sort @arr;
while (scalar @arr > 1) {
    # if ($arr[-1] >= 4) {
    #     $ans += int(($arr[-1] + 3) / 4);
    #     pop @arr;
    # }
    # else {
        if ($arr[-1] + $arr[0] <= 4) {
            push @arr, shift(@arr) + pop @arr;
        }
        else {
            $ans += int(($arr[-1] +3) / 4);
            pop @arr;
        }
    # }
}
$ans+= int(($arr[0]+3)/4) if scalar @arr;
print $ans . "\n";

my $n   = <>;
my @arr = split " ", <>;
my $l   = scalar @arr - 1;
my $ans = 0;
@arr = sort @arr;
while (scalar @arr > 1) {
    if ($arr[-1] >= 4) {
        $ans += int(($arr[-1] + 3) / 4);
        # print "+ $arr[-1]\n";
        pop @arr;
    }
    else {
        if ($arr[-1] + $arr[0] <= 4) {
            # print "- $arr[-1] $arr[0]\n";
            push @arr, shift(@arr) + pop @arr;
        }
        else {
            $ans += int(($arr[-1] +3) / 4);
            # print "* $arr[-1]\n";
            pop @arr;
        }
    }
    # print scalar @arr . "\n";
}
$ans+= int(($arr[0]+3)/4) if scalar @arr;
print $ans . "\n";

#!/usr/bin/perl
# 158 B

use warnings;
use strict;
use POSIX;

my $n = <STDIN>;
my @groups = split(" ", <STDIN>);
my ($one, $two, $three, $four);
my $ret = 0;

foreach(@groups){
    if($_==1){
        $one++;
    }elsif($_==2){
        $two++;
    }elsif($_==3){
        $three++;
    }else{
        $four++;
    }
}

# groups of 4
$ret += $four;
$four= 0;

# groups of 3
if($three<=$one){
    $ret += $three;
    $one -= $three;
    $three=0;
} else {
    $ret += $one;
    $three -= $one;
    $ret += $three;
    $three = 0;
    $one = 0;
}

# groups of 2
my $div = floor($two/2);
my $rem = $two % 2;

$ret += $div;
$two = $rem; #at this point $two will be 0 or 1

if($two == 1){
    $one -= 2;
    $ret++;
}

$ret += ceil($one/4);
print $ret;

use strict;
use warnings;

my $n = <>;
chomp($n);

die('n to big') unless $n >= 1 && $n <= 100000;

my $line = <>;
chomp($line);
my @in = split(' ', $line);

my $result = 0;

# calculate how many groups of each size in stop one. so for example:
# 1 2 4 3 3 goes into
# 1: 1
# 2: 1
# 3: 2
# 4: 1

#remove 4 from equation since its a corner case
#possible compbis per number:
# 3: only go with 1...
# 2: can go with 2 and 1
# 1: can go with 3, 2, 1
#we see higher number is more restrictive. therefor lest start to solve backwards
# try to generalisation:
# x: 4-x.... test. 3: 4-3=1 OK. 2: 4-2=2 OK. 1

my %groups;
for (@in) {
    die('1-4...') unless $_ >= 1 && $_ <= 4;
    $groups{$_}++;
}
my $taxies = 0;

while (grep { $_ >= 1 } values %groups) {
    if ($groups{4}) {
        $groups{4}--;
        $taxies++;
    } elsif($groups{3}) {
        $groups{3}--;
        if ($groups{1}) {
            $groups{1}--;
        }
        $taxies++;
    } elsif ($groups{2}) {
        $groups{2}--;
        if ($groups{2}) {
            $groups{2}--;
        } elsif ($groups{1} >= 2) {
            $groups{1} -=2;
        } elsif ($groups{1} >= 1) {
            $groups{1}--;
        }
        $taxies++;
    } elsif ($groups{1}) {
        $groups{1}--;
        if ($groups{1} >= 3) {
            $groups{1} -= 3;
        } elsif($groups{1} >= 2) {
            $groups{1} -= 2;
        } elsif($groups{1} >= 1) {
            $groups{1}--;
        }
        $taxies++;
    } else {
        die('invalid input error...');
    }

}

print $taxies;

$DEBUG = 0;
chomp($n = <STDIN>);
chomp($line = <STDIN>);

%g = {};
map { $g{$_} = 0; } 1..4;
foreach(split /\s/, $line) {
	$g{$_}++;
}
$taxis = $g{4};
print "$taxis groups of 4\n" if $DEBUG;
if($g{3} <= $g{1}) {
	print $g{3}." groups of 3+1\n" if $DEBUG;
	$taxis += $g{3};
	$g{1} -= $g{3};
	$g{3} = 0;
} else {
	$taxis += $g{1};
	print $g{1}." groups of 3+1\n" if $DEBUG;
	$g{3} -= $g{1};
	$g{1} = 0;
}
if($g{3} > 0) { $taxis += $g{3}; print $g{3}." groups of 3\n" if $DEBUG; }
while($g{2} > 1) {
	$taxis++;
	$g{2} -= 2;
	print "A group of 2+2\n" if $DEBUG;
}
if($g{2}>0) {
	$taxis++;
	$g{1} -= 2;
	print "A group of 2+1+1, ".$g{1}." singles left\n" if $DEBUG;
}
while($g{1}>0) {
	$taxis++;
	$g{1} -= 4;
	print "A group of 4 singles\n" if $DEBUG;
}
print "$taxis\n";

$\="\n";
$n=<STDIN>;

$/=" ";
my $cabs=0;
my @passGroups=(0,0,0,0,0);

for ($i=1;$i<$n;$i++){
    $passGroups[<STDIN>]+=1;
}
$/="\n";
$passGroups[<STDIN>]+=1;
#print "\n", @passGroups;

#print "1) ".$cubs;

#print "        passGroups[1]= $passGroups[1]";
#print "        passGroups[2]= $passGroups[2]";
#print "        passGroups[3]= $passGroups[3]";
#print "        passGroups[4]= $passGroups[4]";

$cubs+=$passGroups[4];
#print "12) ".$cubs;
#print "        passGroups[1]= $passGroups[1]";
#print "        passGroups[2]= $passGroups[2]";
#print "        passGroups[3]= $passGroups[3]";
#print "        passGroups[4]= $passGroups[4]";

if ($passGroups[1]<=$passGroups[3]){
    $cubs+=$passGroups[3];
    $passGroups[1]=0;
}

if ($passGroups[1]>$passGroups[3]){
    $passGroups[1]-=$passGroups[3];
    $cubs+=$passGroups[3];
}

if ($passGroups[2]%2!=0){
    $cubs+=1;
    $passGroups[2]-=1;
    if ($passGroups[1]>1){
        $passGroups[1]-=2;
    }
    if ($passGroups[1]==1){
        $passGroups[1]-=1;
    }

}
    $cubs+=$passGroups[2]/2;
if ($passGroups[1]%4!=0){
    $cubs+=int($passGroups[1]/4) +1;
}else{
    $cubs+=int($passGroups[1]/4);
}

print $cubs;

$\="\n";
$n=<STDIN>;
my $cabs=0;
my @passGroups=(0,0,0,0,0);

@passs=split(" ",<STDIN>);

foreach $i (@passs){
    $passGroups[$i]+=1;
    #print $i;
}

$cubs+=$passGroups[4];

if ($passGroups[1]<=$passGroups[3]){
    $cubs+=$passGroups[3];
    $passGroups[1]=0;
}

if ($passGroups[1]>$passGroups[3]){
    $passGroups[1]-=$passGroups[3];
    $cubs+=$passGroups[3];
}

if ($passGroups[2]%2!=0){
    $cubs+=1;
    $passGroups[2]-=1;
    if ($passGroups[1]>1){
        $passGroups[1]-=2;
    }
    if ($passGroups[1]==1){
        $passGroups[1]-=1;
    }

}
    $cubs+=$passGroups[2]/2;
if ($passGroups[1]%4!=0){
    $cubs+=int($passGroups[1]/4) +1;
}else{
    $cubs+=int($passGroups[1]/4);
}

print $cubs;

#!/usr/bin/perl
use strict; use warnings;

sub ceil { my $j = $_[0]; my $i = int $j; $i < $j ? $i + 1 : $i }

my ($taxi, $one, $two, $three) = (0, 0, 0, 0); <STDIN>;
foreach (split ' ', <STDIN>)
{ if ($_ == 1) { if ($three) { --$three } else { ++$one; next } }
  elsif ($_ == 2) { if ($two) { --$two } else { ++$two; next } }
  elsif ($_ == 3) { if ($one) { --$one } else { ++$three; next } }
  ++$taxi }
if ($two) { $one -= $one > 1 ? 2 : $one; ++$taxi }
printf "%d\n", $taxi + $three + ceil $one / 4

#!/usr/bin/perl
use strict; use warnings;

sub ceil { my $j = $_[0]; my $i = int $j; $i < $j ? $i + 1 : $i }

sub input
{ <STDIN>; $_ = <STDIN>; s/\s//g; my ($n, $m, @a) = length;
  foreach my $i (1..4)
  { s/$i+//g; $m = length; push @a, $n - $m; $n = $m }
  @a }

my ($one, $two, $three, $four) = input;
$one -= $one < $three ? $one : $three;
if ($two % 2) { $one -= $one < 2 ? $one : 2; ++$two }
printf "%d\n", $two / 2 + $three + $four + ceil $one / 4

#!/usr/bin/perl

use 5.012;
use warnings;
use integer;

chomp(my $i = <>);
chomp(my $line = <>);
my $g = ($line =~ s/ //gr);
my $cnt = ($g =~ tr/4/4/);
my $threes = ($g =~ tr/3/3/);
my $twos = ($g =~ tr/2/2/);
my $ones = ($g =~ tr/1/1/);
$cnt += $threes;
$ones -= $threes;
$cnt += $ones if $ones > 0;
$cnt += $twos / 2;
$cnt++ if $twos % 2;
print $cnt;

#!/usr/bin/perl

use 5.012;
use warnings;
use integer;

chomp(my $i = <>);
chomp(my $line = <>);
my $g = ($line =~ s/ //gr);
my $cnt = ($g =~ tr/4/4/);
my $threes = ($g =~ tr/3/3/);
my $twos = ($g =~ tr/2/2/);
my $ones = ($g =~ tr/1/1/);
$cnt += $threes;
$ones -= $threes;
$ones = 0 if $ones < 0;
$cnt += $twos / 2;
$cnt++ if $twos % 2;
$cnt += $ones / 4;
$cnt++ if $ones % 4;
print $cnt;

use strict;
use warnings;

my $n = <>;
chomp($n);

die('n to big') unless $n >= 1 && $n <= 10000;

my $line = <>;
chomp($line);
my @in = split(' ', $line);

my $result = 0;

# calculate how many groups of each size in stop one. so for example:
# 1 2 4 3 3 goes into
# 1: 1
# 2: 1
# 3: 2
# 4: 1

#remove 4 from equation since its a corner case
#possible compbis per number:
# 3: only go with 1...
# 2: can go with 2 and 1
# 1: can go with 3, 2, 1
#we see higher number is more restrictive. therefor lest start to solve backwards
# try to generalisation:
# x: 4-x.... test. 3: 4-3=1 OK. 2: 4-2=2 OK. 1

my %groups;
for (@in) {
    die('1-4...') unless $_ >= 1 && $_ <= 4;
    $groups{$_}++;
}
# I want to loop: 4 3 3 2 1

for (my $i = 4; $i >= 1; $i--) {
    while ($groups{$i} >= 1) {
        my $taxi_occupany = $i;
        $groups{$i}--; #remove self from groups
        my $max_friend_group = 4 - $taxi_occupany;;

        for (my $j = $max_friend_group; $j >= 1; $j--) {
            if ($groups{$j} > 0) {
                $groups{$j}--;
                $taxi_occupany += $j;
            }
            last if $taxi_occupany >= 4;
        }
        $result++;
    }
}

print $result;

$\="\n";
$n=<STDIN>;

$/=" ";
my $cabs=0;
my @passGroups=(0,0,0,0,0);

for ($i=1;$i<$n;$i++){
    $passGroups[<STDIN>]+=1;
}
$/="\n";
$passGroups[<STDIN>]+=1;

$cubs+=$passGroups[4];

if ($passGroups[1]<=$passGroups[3]){
    $cubs+=$passGroups[3];
    $passGroups[1]=0;
}

if ($passGroups[1]>$passGroups[3]){
    $passGroups[1]-=$passGroups[3];
    $cubs+=$passGroups[3];
}

if ($passGroups[2]%2!=0){
    $cubs+=1;
    $passGroups[2]-=1;
    if ($passGroups[1]>1){
        $passGroups[1]-=2;
    }
}

    $cubs+=$passGroups[2]/2;

if ($passGroups[1]%4!=0){
    $cubs+=int($passGroups[1]/4) +1;
}else{
    $cubs+=int($passGroups[1]/4);
}

print $cubs;


use strict;
use warnings;

chomp (my $length = <>);
chomp (my @array = split(" ", <>));
my @positions;
for (my $i = 0;$i<@array;$i++){
	$positions[$array[$i]] = $i;
}

chomp (my $numQueries = <>);
chomp (my @queries = split(" ", <>));

my $vasya = 0;
my $petya = 0;

for(@queries) {
	$vasya += $positions[$_]+1;
	$petya += $length - $positions[$_];
}

print "$vasya $petya";

use strict;
use warnings;

chomp (my $length = <>);
chomp(my $temp = <>);
my @array = split(" ", $temp);
undef $temp;
my @positions;
for (my $i = 0;$i<@array;$i++){
	$positions[$array[$i]] = $i;
}

chomp (my $numQueries = <>);
chomp($temp = <>);
my @queries = split(" ", $temp);
undef $temp;

my $vasya = 0;
my $petya = 0;

for(@queries) {
	$vasya += $positions[$_]+1;
	$petya += $length - $positions[$_];
}

print "$vasya $petya";

chomp($n=<STDIN>);
@elements = split /\s+/, <STDIN>;
chomp($m = <STDIN>);
@queries = split /\s+/, <STDIN>;

%index = { };
$i = 1;
foreach $e (@elements) {
	$index{$e} = $i;
	$i++;
}

$vasya = 0;
$petya = 0;

foreach $q (@queries) {
	$i = $index{$q};
	$vasya += $i;
	$petya += ($n-$i + 1);
}
print "$vasya $petya\n";

use warnings;
use strict;
use List::Util ("sum", "max", "min");

my ($n) =  scalar <>;
my @vals = split " ", scalar <>;
my $q = scalar <>;
my @qs = split " ", scalar <>;

my %pos_left;
my %pos_right;

for (0..$#vals)
{
	$pos_left{$vals[$_]} ||= 1 + $_;
	$pos_right{$vals[$_]} = 1 + $#vals - $_;
}
$\ = " ";
print sum(map $pos_left{$_},  @qs);
print sum(map $pos_right{$_},  @qs);


use strict;
use warnings;

my ( $n, $x ) = split ( " ",  <> );
my $cur = 1;
my $result = 0;
while ( $n-- ){
	my ( $s, $e ) = split ( " ", <> );
	my $y = int ( ( $s - $cur ) / $x );
	$result += $e - ( $y * $x + $cur ) + 1;
	$cur = $e + 1;
}
print "$result\n";


#!/bin/perl -w
use strict;
use warnings;
use List::Util qw{max};

my @a = split //, <>;
my @b = split //, <>;
pop @a;
pop @b;
my ($fours, $sevens) = (0, 0);

for (0 .. $#a) {
    if ($a[$_] > $b[$_]) {
	$sevens++;
    } elsif ($a[$_] < $b[$_]) {
	$fours++;
    }
}

print max($sevens, $fours) . "\n";


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	@_ = map ~~<>, 1 .. $_;

	my %h;

	for( @_ ){
		my( $name, undef, @T ) = split;
		$h{ $name }{ $_ } = 1 for @T;
		}

	for my $name ( keys %h ){
		my @keys = keys %{ $h{ $name } };
		for my $i ( @keys ){
			for my $j ( @keys ){
				$i =~ /(?<=.)$j$/ and delete $h{ $name }{ $j };
				}
			}
		}

	print for ~~ keys %h,
		map { my @K = keys %{ $h{ $_ } }; $_ . ' ' . @K . ' ' . "@K" } keys %h;

	}


#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

<>;

while(<>){
	my( $n, $k ) = split;

	$_ = <>, chomp;

	s/\*/x/;
##	s/\*[^*]*$/x/;

	$debug and print;

	$k --;

	s/[x*].{0,$k}\K(?=\*)/,/g;

	$debug and print;

	s/,\*/x/g;

	$debug and print;

	print scalar( () = m/x/g );
	}


chomp($n = <STDIN>);
@soldiers = split /\s+/, <STDIN>;
unshift @soldiers, 0;
$mingap = 9999;
$minpos = 0;
for($i=1;$i<($n);$i++) {
	$d = abs($soldiers[$i]-$soldiers[$i+1]);
#	print "diff between pos $i and ".($i+1)." is $d\n";
	if( $d < $mingap) {
		$mingap = $d;
		$minpos = $i;
	}
}
#print "minggap $mingap, diff between first and last : ".abs($soldiers[1]-$soldiers[-1])."\n";

if(abs($soldiers[1]-$soldiers[-1])<$mingap) {
	print "1 $n\n";
} else {
	print "".($minpos)." ".($minpos+1)."\n";
}

chomp($n = <STDIN>);
@soldiers = split /\s+/, <STDIN>;
unshift @soldiers, 0;
$mingap = 9999;
$minpos = 0;
for($i=1;$i<($n-1);$i++) {
	if(abs($soldiers[$i]-$soldiers[$i+1]) < $mingap) {
		$mingap = abs($soldiers[$i]-$soldiers[$i+1]);
		$minpos = $i;
	}
}
#print "minggap $mingap, diff between first and last : ".abs($soldiers[0]-$soldiers[-1])."\n";

if(abs($soldiers[1]-$soldiers[-1])<$mingap) {
	print "1 $n\n";
} else {
	print "".($minpos)." ".($minpos+1)."\n";
}

chomp($n = <STDIN>);
@soldiers = split /\s+/, <STDIN>;

$mingap = 9999;
$minpos = 0;
for($i=1;$i<($n-1);$i++) {
	if(abs($soldiers[$i]-$soldiers[$i+1]) < $mingap) {
		$mingap = abs($soldiers[$i]-$soldiers[$i+1]);
		$minpos = $i;
	}
}
#print "minggap $mingap, diff between first and last : ".abs($soldiers[0]-$soldiers[-1])."\n";

if(abs($soldiers[0]-$soldiers[-1])<$mingap) {
	print "1 $n\n";
} else {
	print "-".($minpos)." - ".($minpos+1)."-\n";
}

chomp($n = <STDIN>);
@soldiers = split /\s+/, <STDIN>;

$mingap = 9999;
$minpos = 0;
for($i=1;$i<($n-1);$i++) {
	if(abs($soldiers[$i]-$soldiers[$i+1]) < $mingap) {
		$mingap = abs($soldiers[$i]-$soldiers[$i+1]);
		$minpos = $i;
	}
}
#print "minggap $mingap, diff between first and last : ".abs($soldiers[0]-$soldiers[-1])."\n";

if(abs($soldiers[0]-$soldiers[-1])<$mingap) {
	print "1 $n\n";
} else {
	print ($minpos)." ".($minpos+1)."\n";
}

chomp($n = <STDIN>);
@soldiers = split /\s+/, <STDIN>;

$mingap = 9999;
$minpos = 0;
for($i=1;$i<($n-1);$i++) {
	if(abs($soldiers[$i]-$soldiers[$i+1]) < $mingap) {
		$mingap = abs($soldiers[$i]-$soldiers[$i+1]);
		$minpos = $i;
	}
}
#print "minggap $mingap, diff between first and last : ".abs($soldiers[0]-$soldiers[-1])."\n";

if(abs($soldiers[0]-$soldiers[-1])<$mingap) {
	print "1 $n\n";
} else {
	print "".($minpos)." ".($minpos+1)."\n";
}


use strict;

# cf713a
=item

A. Memory and Crow
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n integers b1, b2, ..., bn written in a row. For all i from 1 to n, values ai are defined by the crows performing the following procedure:

    The crow sets ai initially 0.
    The crow then adds bi to ai, subtracts bi + 1, adds the bi + 2 number, and so on until the n'th number. Thus, ai = bi - bi + 1 + bi + 2 - bi + 3....

Memory gives you the values a1, a2, ..., an, and he now wants you to find the initial numbers b1, b2, ..., bn written in the row? Can you do it?
Input

The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of integers written in the row.

The next line contains n, the i'th of which is ai ( - 109 ≤ ai ≤ 109) — the value of the i'th number.
Output

Print n integers corresponding to the sequence b1, b2, ..., bn. It's guaranteed that the answer is unique and fits in 32-bit integer type.
Examples
Input

5
6 -4 8 -2 3

Output

2 4 6 1 3

Input

5
3 -2 -1 5 6

Output

1 -3 4 11 6

Note

In the first sample test, the crows report the numbers 6, - 4, 8, - 2, and 3 when he starts at indices 1, 2, 3, 4 and 5 respectively. It is easy to check that the sequence 2 4 6 1 3 satisfies the reports. For example, 6 = 2 - 4 + 6 - 1 + 3, and  - 4 = 4 - 6 + 1 - 3.

In the second sample test, the sequence 1,  - 3, 4, 11, 6 satisfies the reports. For example, 5 = 11 - 6 and 6 = 6.

=cut

# ai = bi - bi + 1 + bi + 2 - bi + 3...
=item
a0=b0
a1=b1-b0
a2=b2-b1+b0=b2-a1
a3=b3-b2+b1-b0=b3-a2

b0=a0
b1=a0+a1
b2=a1+a2
b3=a2+a3
=cut

my ($n,$l)=<>;
my @as=split(' ',$l);
for(my $i=1;$i<$n;++$i) {
  print $as[$i-1]+$as[$i],' ';
}
print $as[$n-1],"\n";


$k=<>;
@ch=split('',<>);
pop(@ch);
foreach $var (@ch){
$map{$var}++;

}
$ok=1;
foreach $var (values%map){
if($var%$k!=0){
$ok=0;
last;
}
}
if(!$ok){
print -1;
}
else{

for($i=0;$i<$k;$i++){
while(($key,$v) = each(%map) ) {
   print $key x ($v/$k) ;
}

}

}

#!/bin/perl
use strict;
use warnings;

my $k = int <STDIN>;
my $str = <STDIN>;
chomp $str;
my @arr = split //, $str;

my %hash;
my $i;
foreach $i ( @arr ) {
	if ( $hash{$i} ) {
		$hash{$i}++;
	} else {
		$hash{$i} = 1;
	}
}

$str = '';
foreach $i ( keys %hash ) {
	if ( $hash{$i} % $k ) {
		print -1;
		goto end;
	} else {
		$str .= $i x ( $hash{$i} / $k );
	}
}

print $str x $k;

end:

__END__


my $n = <STDIN>;
my $line = <STDIN>;
chomp $line;
my @chars = split(//, $line);
my $CA = 0;
my $CD = 0;
for my $i (0..$n-1) {
	$CA++ if $chars[$i] eq 'A';
	$CD++ if $chars[$i] eq 'D';
}
print 'Danik'      if $CD > $CA;
print 'Anton'      if $CD < $CA;
print 'Friendship' if $CD == $CA;

#!/usr/bin/perl

use 5.012;
use warnings;

chomp(my $a = <>);
chomp(my $b = <>);

my $an = ($b =~ tr/A/A/);
my $da = ($b =~ tr/D/D/);

if ($an > $da) {
	print "Anton";
}
elsif ($da > $an) {
	print "Danik";
}
else {
	print "Friendship";
}

use strict;
use warnings;

sub main
{
    my $gameLen = <STDIN>;
    my $result = <STDIN>;
    my $anton=0;
    my $danik=0;
    my $current;
    for(my $i=0;$i<$gameLen;$i++){
        if(substr($result,$i,1) eq "A"){
            $anton = $anton+1;
        }
        if(substr($result,$i,1) eq "D"){
            $danik = $danik+1;
        }
    }
    if($anton>$danik){
        printf("Anton\n");
    }elsif($anton<$danik){
        printf("Danik\n");
    }else{
        printf("Friendship\n")
    }
}

main();

use strict;
use warnings;

<>;
my $s=<>;
chomp($s);
my $k=length($s);
$s =~ s/A//g;
my $D=length($s);
my $A=$k-$D;
if($D>$A)
{
  print "Danik\n";
}
elsif($A>$D)
{
  print "Anton\n";
}
else
{
  print "Friendship\n";
}

use feature qw(switch);
$n=<>;
@ch=split('',<>);

$A=0,$D=0;
foreach $car (@ch){
given($car){

when('A') {$A++};
when('D') {$D++};

}

}

if($A>$D){

print "Anton";
}
elsif($D>$A){
print "Danik";
}
else{
print "Friendship";
}

#!/usr/bin/perl
use strict;
my $n = <STDIN>;
my $array =<STDIN>;
chomp $array;
my @array_p=split(undef, $array);
my $A = 0;
my $B = 0;
foreach my $item (@array_p)
{
    $item eq 'A' ? $A++ : $B++ ;
}
$A == $B ? print 'Friendship' : $A > $B ? print 'Anton' : print 'Danik';

#!/usr/bin/env perl

use strict;
use warnings;
use feature qw/ say /;

# essential
my @tokens = ();

sub ceil {
    my $n = shift;
    my $in = int($n);
    return $n if $in == $n;
    return $in + 1;
}

sub read_line {
    chomp (my $line = <STDIN>);
    return $line;
}

sub read_token {
    @tokens = split q{ }, read_line() if @tokens == 0;
    return shift @tokens;
}

sub min {
    my @numbers = @_;
    my $min = shift @numbers;
    for ( @numbers ) {
        $min = $min<$_ ? $min : $_;
    }
    return $min;
}

sub max {
    my @numbers = @_;
    my $max = shift @numbers;
    for ( @numbers ) {
        $max = $max > $_ ? $max : $_;
    }
    return $max;
}

sub sum {
    my @numbers = @_;
    my $sum = 0;
    for (@numbers) {
        $sum += $_;
    }
    return $sum;
}

# solve

my $n = read_token;

my $winners = read_line;

my $a = 0;
my $d = 0;

for (0..$n-1) {
    my $winner = substr($winners, $_, 1);
    $a++ if $winner eq 'A';
    $d++ if $winner eq 'D';
}

say   $a == $d ? 'Friendship'
    : $a > $d  ? 'Anton'
    :            'Danik'

#!/usr/bin/env perl

use strict;
use warnings;
use feature qw/ say /;

# essential
my @tokens = ();

sub ceil {
    my $n = shift;
    my $in = int($n);
    return $n if $in == $n;
    return $in + 1;
}

sub read_line {
    chomp (my $line = <STDIN>);
    return $line;
}

sub read_token {
    @tokens = split q{ }, read_line() if @tokens == 0;
    return shift @tokens;
}

sub min {
    my @numbers = @_;
    my $min = shift @numbers;
    for ( @numbers ) {
        $min = $min<$_ ? $min : $_;
    }
    return $min;
}

sub max {
    my @numbers = @_;
    my $max = shift @numbers;
    for ( @numbers ) {
        $max = $max > $_ ? $max : $_;
    }
    return $max;
}

sub sum {
    my @numbers = @_;
    my $sum = 0;
    for (@numbers) {
        $sum += $_;
    }
    return $sum;
}

# solve

my $n = read_token;

my @winners = split q{}, read_line;

my $a = 0;
my $d = 0;

for (@winners) {
    $a++ if $_ eq 'A';
    $d++ if $_ eq 'D';
}

say   $a == $d ? 'Friendship'
    : $a > $d  ? 'Anton'
    :            'Danik'

$\ = $/;

<>;

while(<>){
	$A = () = /A/g;
	$D = () = /D/g;

	print qw(Friendship Anton Danik)[ $A <=> $D ]
	}

<>;

$_ = <>;

$A = () = /A/g;
$D = () = /D/g;

print qw(Friendship Anton Danik)[ $A <=> $D ]

my $n = <STDIN>;
my $line = <STDIN>;
chomp $line;
my @chars = split(//, $line);
my $CA = 0;
my $CD = 0;
foreach my $i (@chars) {
	$CA++ if $chars[i] eq 'A';
	$CD++ if $chars[i] eq 'D';
}
print 'Danik'      if $CD > $CA;
print 'Anton'      if $CD < $CA;
print 'Friendship' if $CD == $CA;

#!/usr/bin/perl
use strict;
my $n = <STDIN>;
my $array =<STDIN>;
my @array_p=split(undef, $array);
my $A = 0;
my $B = 0;
foreach my $item (@array_p)
{
    $item eq 'A' ? $A++ : $B++ ;
}
$A == $B ? print 'Friendship' : $A > $B ? print 'Anton' : print 'Danik';


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

<>;

while(<>){
	$debug and print '-' x 15;
	my( $n, $k ) = split;
	my( $sum, @p ) = split ' ', <>;

	my $add = 0;

	while( @p ){
		my $p_next = shift @p;
		$debug and print "sum:[$sum], p_next:[$p_next]";

		if( $sum * $k < $p_next * 100 ){
			my $diff = ( int $p_next * 100 / $k ) + !!( $p_next * 100 % $k ) - $sum;
			$add += $diff;
			$sum += $diff + $p_next;
			}
		else{
			$sum += $p_next;
			}
		}

	print $add;
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	chomp;
	my $dict = $_;
	$_ = <>;

	s/\?/[$dict]/g;
	s/\*/[^$dict]*/;

	my $qr = qr/^$_$/;

	print <> =~ $qr ? "YES" : "NO" for 1 .. <>;
	}

#!/usr/bin/perl

# http://perlmonks.org/?node_id=1196089

use strict;
use warnings;

$\ = $/;

while(<>)
  {
  chomp;
  my $dict = $_;

  $_ = <>;

  my($head, $tail) = map length( $_ // '' ), split /\*/;

  s/\?/[$dict]/g;
  s/\*/ [^$dict ]* /;

  my $qr = qr/^$_/;

  $_ = <>,
    $head <= length($_) && substr($_, $head, 0, ' '),
    $tail && $tail <= length($_) && substr($_, -$tail, 0, ' '),
    print $_ =~ $qr ? "YES" : "NO"
      for 1 .. <>;
  }


#!/usr/bin/perl
# your code goes here
use v5.10;

chomp($n = <>);
our ($x, $y, $z) = (0) x 3;
while ($n-- > 0) {
	($xx, $yy, $zz) = split / /, <>;
	$x += $xx;
	$y += $yy;
	$z += $zz;
}
$x==0 and $y==0 and $z==0 and say "YES" or say "NO";

my $num = <>;
chomp $num;
my @force = (0, 0, 0);
for (1..$num) {
    my $line = <>;
    chomp $line;
    my @new = split ' ', $line;
    $force[$_] += $new[$_] for (0..2);
}
my $flag=1;
for(0..2) {
    $flag = 0 if $force[$_];
}
$flag ? print "YES" : print "NO";


use strict;
use warnings;

$\ = "\n";

my ($s, $n) = split ' ', <>;
my @dragons;
for (1..$n) {
    push @dragons, [split ' ', <>];
}

@dragons = sort { $a->[0] <=> $b->[0] } @dragons;

my $dead;
for my $dragon (@dragons) {
    my ($strength, $increment) = @$dragon;
    if ($s > $strength) {
        $s += $increment;
    } else {
        $dead = 1;
        last;
    }
}

print $dead ? 'NO' : 'YES';

$DEBUG = 0;
chomp($line=<STDIN>);
($str, $n) = $line =~ /(\d+)\s+(\d+)/;

my @dragons = ();
while($n-- > 0) {
	chomp($line=<STDIN>);
	($dstr, $inc) = $line =~ /(\d+)\s+(\d+)/;
	push(@dragons, {strength=>$dstr, powerup => $inc});
}

foreach $dragon (sort by_strength @dragons) {
	print "Battle with $str against ".$dragon->{strength}."\n" if $DEBUG;
	if($str <= $dragon->{strength}) {
		print "NO\n";
		exit;
	}
	$str += $dragon->{powerup};
}
print "YES\n";

sub by_strength {
	$a->{strength} <=> $b->{strength};
}

use strict;
use warnings;

$\ = "\n";

my ($s, $n) = split ' ', <>;
my @dragons;
for (1..$n) {
    push @dragons, [split ' ', <>];
}

my $dead;
for my $dragon (@dragons) {
    my ($strength, $increment) = @$dragon;
    if ($s > $strength) {
        $s += $increment;
    } else {
        $dead = 1;
        last;
    }
}

print $dead ? 'NO' : 'YES';


#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

while (<>){

#    <>=~/ /;
#    <>=~/ (\d+) /;
#    <>=~/ (\d+) (\d+) /;

$_=<>; chomp;
s/-?\d+/$sum+=$&/eg;
s/\S//g; s/$/ /;
$sum+= 1e4 * length;
print (($sum % length )? (-1+ length): (length))

}

#***********SUBS************

sub num {$a <=> $b}
sub num_rev {$b <=> $a}
sub rev {$b cmp $a}

sub p{print}
sub n{print "\n"}
sub pn{p,n}
sub pn_mas {pn for @_}

sub fact{
	my $i= shift;   my $j= 1;
	$j*=$i-- while $i;
	return $j
}

# Transpose "charbox"
sub trans{
	my @t=(); my $i=0;
	$i=0, s/./$t[$i++].=$&/eg for @_;
	return @t
}

sub rotL{
	my @t=(); my $i=0; my $j;
	for (@_){
		$i=0, $j=length; $t[$i++].=chop while $j--
	}
	return @t
}

sub rotR{
    my @m=@_;  chomp @m;   my @t=();
	my $i= length $m[0];
	while ($i--){
		$t[$i]=~s/^/chop/e for @m
	}
return @t
}

sub revlines{
	$_=reverse $_ for @_;
	return @_
}

# Transpose "value sets" ## slow?
sub trans_s{
	my @t=(); my $j;
	$j=0, s/\S+/($t[$j++].="$& ")?$&:$&/eg for @_;
	chop @t;
return @t
}

# rotL_s
# rotR_s

# slow?
sub revlines_s{
	$_=join" ", reverse split/\s+/ 	for @_;
	return @_
}

sub div{
    my ($n, $k)=@_;
return ($n - $n % $k) / $k
}

sub uniq{
	my @m=sort @_; my @u;
	push @u, my $i=shift @m;
	$i eq $_ or push @u, $i=$_ for @m;
return @u
}

sub max{
    my $max=shift;
    $max<$_ and $max=$_ for @_;
return $max
}

sub min{
    my $min=shift;
    $min>$_ and $min=$_ for @_;
return $min
}

# count (kas_ieskoma, masyvas)
sub count{
    my $i=0;
    $_ eq $_[0] and $i++ for @_;
return --$i
}

#!/usr/bin/perl -w

use strict;
use List::Util qw(sum);

chomp(my $n = <>);
chomp($_ = <>);
my $res = $n - ((sum split) % $n == 0 ? 0 : 1);
print $res, "\n";

use List::Util qw(sum);
$n=<>;
$_=<>;
print((sum split)%$n?$n-1:$n)

#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

while (<>){

#    <>=~/ /;
#    <>=~/ (\d+) /;
#    <>=~/ (\d+) (\d+) /;

$n=@_=split/ /,<>;
$x=1;
for ($i=0; $i<$n; $i++){
$sum = 0;
	for ($j=$i; $j<$n; $j++){
	$sum += $_[$j];
#		print "$sum";
	$k=$j-$i+1;
	$sum % $k or $k>$x and $x=$k;
#		print " $x\n";
	}
}
print "$x";

}

#***********SUBS************

sub num {$a <=> $b}
sub num_rev {$b <=> $a}
sub rev {$b cmp $a}

sub p{print}
sub n{print "\n"}
sub pn{p,n}
sub pn_mas {pn for @_}

sub fact{
	my $i= shift;   my $j= 1;
	$j*=$i-- while $i;
	return $j
}

# Transpose "charbox"
sub trans{
	my @t=(); my $i=0;
	$i=0, s/./$t[$i++].=$&/eg for @_;
	return @t
}

sub rotL{
	my @t=(); my $i=0; my $j;
	for (@_){
		$i=0, $j=length; $t[$i++].=chop while $j--
	}
	return @t
}

sub rotR{
    my @m=@_;  chomp @m;   my @t=();
	my $i= length $m[0];
	while ($i--){
		$t[$i]=~s/^/chop/e for @m
	}
return @t
}

sub revlines{
	$_=reverse $_ for @_;
	return @_
}

# Transpose "value sets" ## slow?
sub trans_s{
	my @t=(); my $j;
	$j=0, s/\S+/($t[$j++].="$& ")?$&:$&/eg for @_;
	chop @t;
return @t
}

# rotL_s
# rotR_s

# slow?
sub revlines_s{
	$_=join" ", reverse split/\s+/ 	for @_;
	return @_
}

sub div{
    my ($n, $k)=@_;
return ($n - $n % $k) / $k
}

sub uniq{
	my @m=sort @_; my @u;
	push @u, my $i=shift @m;
	$i eq $_ or push @u, $i=$_ for @m;
return @u
}

sub max{
    my $max=shift;
    $max<$_ and $max=$_ for @_;
return $max
}

sub min{
    my $min=shift;
    $min>$_ and $min=$_ for @_;
return $min
}

# count (kas_ieskoma, masyvas)
sub count{
    my $i=0;
    $_ eq $_[0] and $i++ for @_;
return --$i
}

#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

while (<>){

#    <>=~/ /;
#    <>=~/ (\d+) /;
#    <>=~/ (\d+) (\d+) /;

$n=@_=split/ /,<>;
$_+=1e4 for @_;
$x=1;
for ($i=0; $i<$n; $i++){
$sum = 0;
	for ($j=$i; $j<$n; $j++){
	$sum += $_[$j];
#		print "$sum";
	$k=$j-$i+1;
	$sum % $k or $k>$x and $x=$k;
#		print " $x\n";
	}
}
print "$x";

}

#***********SUBS************

sub num {$a <=> $b}
sub num_rev {$b <=> $a}
sub rev {$b cmp $a}

sub p{print}
sub n{print "\n"}
sub pn{p,n}
sub pn_mas {pn for @_}

sub fact{
	my $i= shift;   my $j= 1;
	$j*=$i-- while $i;
	return $j
}

# Transpose "charbox"
sub trans{
	my @t=(); my $i=0;
	$i=0, s/./$t[$i++].=$&/eg for @_;
	return @t
}

sub rotL{
	my @t=(); my $i=0; my $j;
	for (@_){
		$i=0, $j=length; $t[$i++].=chop while $j--
	}
	return @t
}

sub rotR{
    my @m=@_;  chomp @m;   my @t=();
	my $i= length $m[0];
	while ($i--){
		$t[$i]=~s/^/chop/e for @m
	}
return @t
}

sub revlines{
	$_=reverse $_ for @_;
	return @_
}

# Transpose "value sets" ## slow?
sub trans_s{
	my @t=(); my $j;
	$j=0, s/\S+/($t[$j++].="$& ")?$&:$&/eg for @_;
	chop @t;
return @t
}

# rotL_s
# rotR_s

# slow?
sub revlines_s{
	$_=join" ", reverse split/\s+/ 	for @_;
	return @_
}

sub div{
    my ($n, $k)=@_;
return ($n - $n % $k) / $k
}

sub uniq{
	my @m=sort @_; my @u;
	push @u, my $i=shift @m;
	$i eq $_ or push @u, $i=$_ for @m;
return @u
}

sub max{
    my $max=shift;
    $max<$_ and $max=$_ for @_;
return $max
}

sub min{
    my $min=shift;
    $min>$_ and $min=$_ for @_;
return $min
}

# count (kas_ieskoma, masyvas)
sub count{
    my $i=0;
    $_ eq $_[0] and $i++ for @_;
return --$i
}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

OUTER:
while(<>){

	@_ = split ' ', <>;

	for my $i ( 0 .. @_ - 1 ){
		if( $_[ $i ] < $i ){
			print "NO";
			next OUTER;
			}
		else{
			$_[ $i + 1 ] += $_[ $i ] - $i;
			}
		}

	print "YES";
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

OUTER:
while(<>){

	if( $_ == 1 ){
		print "NO";
		<>;
		next;
		}

	@_ = split ' ', <>;

	for my $i ( 0 .. @_ - 1 ){
		if( $_[ $i ] < $i ){
			print "NO";
			next OUTER;
			}
		else{
			$_[ $i + 1 ] += $_[ $i ] - $i;
			}
		}

	print "YES";
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){

	if( $_ == 1 ){
		print "NO";
		<>;
		next;
		}

	@_ = split ' ', <>;

	my $sum = 0;

	$sum += $_ for @_;

	if( @_ * ( @_ - 1 ) / 2 > $sum ){
		print "NO";
		}
	else{
		print "YES";
		}
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){

	if( $_ == 1 ){
		print "NO";
		next;
		}

	@_ = split ' ', <>;

	my $sum = 0;

	$sum += $_ for @_;

	if( @_ * ( @_ - 1 ) / 2 > $sum ){
		print "NO";
		}
	else{
		print "YES";
		}
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	@_ = split ' ', <>;

	my $sum = 0;

	$sum += $_ for @_;

	if( @_ * ( @_ - 1 ) / 2 > $sum ){
		print "NO";
		}
	else{
		print "YES";
		}

	}


#!/usr/bin/perl
use strict;
use warnings;

chomp (my $n = <>);

my @dots;
push @dots, <> for ($n);

my $count = 0;

foreach my $dot (@dots)
{
    my $index = shift;
    my ($x, $y) = split / /, $dot;
    my $bits;

    foreach (@dots)
    {
        my ($x_, $y_) = split / /, $_;

            if ($x eq $x_)
            {
                $bits |= 1 if ($y_ > $y);
                $bits |= 2 if ($y_ < $y);
            }

            if ($y eq $y_)
            {
                $bits |= 4 if ($x_ < $x);
                $bits |= 8 if ($x_ > $x);
            }

    }

    $count++ if ($bits eq 15);
}

print $count, "\n";

#!/usr/bin/perl
use warnings;
use strict;
chomp(my $n = <STDIN>);
my @pts = ();
while($n-- > 0){
	my @tmp = split(/[\s\/n]/,<STDIN>);
	push(@pts,\@tmp);
}
my @check = (0,0,0,0);
my $count = 0;
foreach my $pt (@pts) {
	@check = (0,0,0,0);
	foreach my $otherpt (@pts) {
		if($$pt[0]==$$otherpt[0]){
			$check[0] = 1 if($$pt[1]>$$otherpt[1]);
			$check[1] = 1 if($$pt[1]<$$otherpt[1]);
		}
		elsif($$pt[1]==$$otherpt[1]){
			$check[2] = 1 if($$pt[0]>$$otherpt[0]);
			$check[3] = 1 if($$pt[0]<$$otherpt[0]);
		}
	}
	my $checksum = 0;
	$checksum += $_ for @check;
	$count++ if($checksum == 4);
}

print $count;

#!/usr/bin/perl
use strict;
use warnings;

my $n = <>;
my @dots;
push @dots, <> for ($n);

my $count = 0;
foreach my $dot (@dots)
{
    my $index = shift;
    my ($x, $y) = split /\s/, $dot;
    my $bits;

    foreach (@dots)
    {
        my ($x_, $y_) = split /\s/;

        $bits |= ($y_ < $y)? 1 : (($y_ > $y) ? 2 : 0) if ($x eq $x_);
        $bits |= ($x_ < $x)? 4 : (($x_ > $x) ? 8 : 0) if ($y eq $y_);
    }

    $count++ if ($bits eq 15);
}

print $count, "\n";

#!/usr/bin/perl
use strict;
use warnings;

chomp (my $n = <>);

my @dots;
push @dots, <> for ($n);

my $count = 0;

sub is_super($)
{
    my $index = shift;
    my ($x, $y) = split / /, $dots[$index];
    chomp ($y);
    my $score = 0;
    my ($left, $right, $up, $down) = (0,0,0,0);

    foreach (@dots)
    {
        chomp($_);
        my ($x_, $y_) = split / /, $_;

            if ($x eq $x_)
            {
                $up = ($y_ > $y) if (!$up);
                $down = ($y_ < $y) if (!$down);
            }

            if ($y eq $y_)
            {
                $left = ($x_ < $x) if (!$left);
                $right = ($x_ > $x) if (!$right);
            }

    }

    $count++ if ($left && $right && $up && $down);
}

is_super($_) for (0..$n-1);

print $count, "\n";

#!/usr/bin/perl
use strict;
use warnings;

chomp (my $n = <>);

my @dots;
push @dots, <> for ($n);

my $count = 0;

sub is_super($)
{
    my $index = shift;
    my ($x, $y) = split / /, $dots[$index];
    chomp ($y);
    my $score = 0;

    foreach (@dots)
    {
        chomp($_);
        my ($x_, $y_) = split / /, $_;

        $score++ if (($x > $x_) and ($y eq $y_));
        $score++ if (($x < $x_) and ($y eq $y_));
        $score++ if (($x eq $x_) and ($y > $y_));
        $score++ if (($x eq $x_) and ($y < $y_));
    }

    $count++ if ($score >= 4);
}

is_super($_) for (0..$n-1);

print $count, "\n";

#!/usr/bin/perl
use strict;
use warnings;

chomp (my $n = <>);

my @dots;
push @dots, <> for ($n);

my $count = 0;

sub is_super($)
{
    my $index = shift;
    my ($x, $y) = split / /, $dots[$index];
    chomp ($y);
    my $score = 0;

    foreach (@dots)
    {
        chomp($_);
        my ($x_, $y_) = split / /, $_;

        if (($x_ ne $x) and ($y_ ne $y))
        {
            $score++ if ($x > $x_ and $y eq $y_);
            $score++ if ($x < $x_ and $y eq $y_);
            $score++ if ($x == $x and $y > $y_);
            $score++ if ($x == $x and $y < $y_);
        }
    }

    $count++ if ($score >= 4);
}

for (0..$n-1)
{
    is_super($_);
}

print $count, "\n";

#!/usr/bin/perl
use strict;
use warnings;

chomp (my $n = <>);

my @dots;
push @dots, <> for ($n);

my $count = 0;

sub is_super($)
{
    my $index = shift;
    my ($x, $y) = split / /, $dots[$index];
    chomp ($y);
    my $score = 0;

    foreach (@dots)
    {
        chomp($_);
        my ($x_, $y_) = split / /, $_;

        if (($x_ ne $x) and ($y_ ne $y))
        {
            $score++ if ($x > $x_ and $y eq $y_);
            $score++ if ($x < $x_ and $y eq $y_);
            $score++ if ($x == $x and $y > $y_);
            $score++ if ($x == $x and $y < $y_);
        }
    }

    $count++ if ($score > 4);
}

for (0..$n-1)
{
    is_super($_);
}

print $count, "\n";


#!perl -w
use strict;
use warnings;
use POSIX qw[ceil];

main();

sub main {
    my ($n, $m) = split /\D/, <>;
    my @arr;
    for my $i (0..int($m / 2)-1) {
        unshift @arr, $m - $i;
        unshift @arr, $i + 1;
    }
    if ($m & 1) {
        unshift @arr, int($m / 2) + 1;
    }
    my $c = 0;
    for my $i (1..$n) {
        print $arr[$c % @arr], "\n";
        $c++;
    }
}

__END__

#!/usr/bin/perl

use 5.006;
use strict;
use warnings;

my $n;
my $m;

sub mcmp {
  my $v1 = abs (($m + 1) / 2 - $a);
  my $v2 = abs (($m + 1) / 2 - $b);
  return -1 if($v1 < $v2);
  return -1 if ($v1 == $v2 && $a < $b);
  return 1;
}

my $num = <>;
my @arr = split(/ /, $num);
$n = $arr[0];
$m = $arr[1];
my @array = ();
for(1..$m) {
    push @array, $_;
}
my @ans= sort mcmp @array;
for(0..($n - 1)) {
  my $t = int($_ % $m);
  print "$ans[$t]\n";
}


use warnings;
use strict ;
use List::Util qw(first max maxstr min minstr reduce shuffle sum);

my $n = <>;
my @data = (<>);

for my $str (@data)
{
    chomp $str;
    $str or next;
    my @u = split //, $str;

    my @res = ();
    for my $rows (1, 2, 3, 4, 6, 12)
    {
        my $rl = @u / $rows;

        for my $i (0..$rl-1)
        {

            #print $i;
            my $en = join "", map $u[$_* $rl + $i], 0..$rows-1;

            if ($en eq 'X'x $rows )
            {
                #print "ok";
                push @res, $rows . "x" . $rl;
                last;
            }
        }

    }
    print join " ", (scalar(@res),  @res);
    print "\n";

}


#!/usr/bin/perl -w

use strict;

my $res = 0;
chomp(my $n = <>);
for (1 .. $n) {
	my $cur = 0;
	chomp($_ = <>);
	map { $cur += $_ } split;
	++$res if $cur >= 2;
}
print "$res\n";

#!/usr/bin/env perl

use v5.012;
use warnings;

my $cnt = <STDIN>;
my $ret = 0;
while ($cnt--) {
	my $ones = (<STDIN> =~ tr/1/1/);
	$ret++ if $ones >= 2;
}
print $ret;

use strict;
use warnings;

my $n = <>;
my $ans = 0;
my $count;
while($n-- > 0){
    $count = 0;
    my @a = split(/ /,<>);
    $count++ if $a[0]==1;
    $count++ if $a[1]==1;
    $count++ if $a[2]==1;
    $ans++ if $count > 1;
}
print "$ans\n";

my $n = <STDIN>;
my $count = 0;
while($n--) {
    my($a, $b, $c) = split(' ', <STDIN>);
    if(($a + $b + $c) > 1) {
        $count++;
    }
}
print $count;

#!/usr/bin/perl
# your code goes here

chomp($n = <>);
$ans = 0;
while ($n-- > 0) {
	$c = 0;
	$c += $_ foreach (split / /,<>);
	$c>=2 and $ans++;
}
print $ans;

#use strict;
#use warnings;
#use diagnostics;

my $lines = <STDIN>;
my $solutions = 0;
for (my $i=0; $i<$lines; $i++) {
	my $sum = 0;
	$line = <STDIN>;
	@inputs = split (/ /,$line);
	foreach (@inputs) {
		$sum += $_;
	}

	if ($sum >= 2) {
		$solutions++;
	}
}

print $solutions;

my $n = <>;
my $solutions = 0;
while (<>) {
    my ($x,$y,$z) = $_ =~ /^(\d) (\d) (\d)$/;

    $solutions++ if $x+$y+$z > 1;
}
print $solutions;

use strict;
use warnings;

my $n = <>;
chomp($n);

my $solutions = 0;
for (my $i=0; $i < $n; $i++) {
    my $in = <>;
    chomp($in);

    my @verdicts = split(' ', $in);
    my $p = scalar grep { $_ } @verdicts;
    $solutions += ($p >= 2) ? 1 : 0;
}

print $solutions;

use strict;
use warnings;

my $n = <>;
my $res = 0;
for (1..$n) {
	my @sols = split ' ', <>;
	my $b = 0;
	for (@sols) { $b += $_ };
	$res++ if $b > 1;
}
print $res."\n";

#!/usr/bin/perl
use strict;
my $n = <STDIN>;
my $sum = 0;
while (my $row = <STDIN>)
{
    my $sum_w = 0;
    chomp $row;
    my @inputs_p = split (' ', $row);
    foreach my $item (@inputs_p)
    {
        if ($item == 1) { $sum_w++; }
    }
    if ($sum_w >= 2) { $sum++;}
}
print $sum;

chomp(my $testCase=<STDIN>);
my $count = 0;
while($testCase){
	my $a=<STDIN>;
	my $sum;
	my @arr = split / /,$a;
	foreach (@arr){
		$sum += $_;
	}
	if($sum >= 2 ){
	 $count++;
	}
	$testCase--;
}
print "$count";

my $len=0; my $count = <STDIN>; for(1..$count){ @_ = split " ", <STDIN>; my $sym = 0; for(@_){ $sym += $_; } if($sym > 1){ $len += 1; } }; print "$len\n";

#!/usr/bin/perl
use strict; use warnings;

my $n = 0; <STDIN>;
while (<STDIN>)
{ my ($a, $b, $c) = split ' ', $_; ++$n if $a + $b + $c > 1 }
print $n . "\n"

use strict;
use warnings;
use diagnostics;

my $lines = <STDIN>;
my $solutions = 0;
for (my $i=0; $i<$lines; $i++) {
	my $sum = 0;
	$sum += <STDIN>;
	$sum += <STDIN>;
	$sum += <STDIN>;

	if ($sum >= 2) {
		$solutions++;
	}
}

print $solutions."\n";

#use strict;
#use warnings;
#use diagnostics;

my $lines = <STDIN>;
my $solutions = 0;
for (my $i=0; $i<$lines; $i++) {
	my $sum = 0;
	$sum += <STDIN>;
	$sum += <STDIN>;
	$sum += <STDIN>;

	if ($sum >= 2) {
		$solutions++;
	}
}

print $solutions;

#use strict;
#use warnings;
#use diagnostics;

my $lines = <STDIN>;
my $solutions = 0;
for (my $i=0; $i<$lines; $i++) {
	my $sum = 0;
	$sum += <STDIN>;
	$sum += <STDIN>;
	$sum += <STDIN>;

	if ($sum >= 2) {
		$solutions++;
	}
}

print "2";
#print $solutions."\n";

chomp(my $testCase=<STDIN>);
my $count;
while($testCase){
	my $a=<STDIN>;
	my $sum;
	my @arr = split / /,$a;
	foreach (@arr){
		$sum += $_;
	}
	if($sum >= 2 ){
	 $count++;
	}
	$testCase--;
}
print $count;


#!/usr/bin/env perl

use strict;
use warnings;
use feature qw/ say /;

# send argument as array, not reference, to function requiring list

# essential
my @tokens = ();

sub ceil {
    my $n = shift;
    my $in = int($n);
    return $n if $in == $n;
    return $in + 1;
}

sub read_line {
    chomp (my $line = <STDIN>);
    return $line;
}

sub read_token {
    @tokens = split q{ }, read_line() if @tokens == 0;
    return shift @tokens;
}

sub min {
    my @numbers = @_;
    my $min = shift @numbers;
    for ( @numbers ) {
        $min = $min<$_ ? $min : $_;
    }
    return $min;
}

sub max {
    my @numbers = @_;
    my $max = shift @numbers;
    for ( @numbers ) {
        $max = $max > $_ ? $max : $_;
    }
    return $max;
}

sub sum {
    my @numbers = @_;
    my $sum = 0;
    for (@numbers) {
        $sum += $_;
    }
    return $sum;
}

# solve

my $n = read_token;

my $count = 0;
my $answer = '';

if ($n % 2 == 1) {
    $n -= 3;
    while ($n > 0) {
        $count++;
        $n -= 2;
        $answer .= '2 ';
    }
    $count++;
    $answer .= '3';
}
else {
    while ($n > 0) {
        $count++;
        $n -= 2;
        $answer .= '2 ';
    }
}

say $count;
say $answer;


#Perl v5.18.2
$n = <STDIN>;

print 9*$n;
print " ";
print 8*$n;

use strict;
use warnings FATAL => 'all';

my $n = <STDIN>;
chomp $n;
$n = int($n);

my $a = 9 * $n;
my $b = 8 * $n;

print $a, ' ', $b

#!/usr/bin/env perl
package Codeforces::Task::A;
use v5.10;
use strict;
use warnings;

sub min {
    my ($a, $b) = @_;
    if ($a < $b) {
		return $a;
	} else {
        return $b;
    }
}
sub max {
    my ($a, $b) = @_;
    if ($a > $b) {
        return $a;
    } else {
        return $b;
    }
}

sub solve {
    my $n = int(<>);
    if ($n % 2 == 0) {
        print $n + 4, " 4";
    } else {
        print $n + 9, " 9";
    }
    print "\n";
}

sub main {
    my $t = 1;
#    $t = int(<>);
    while ($t--) {
        solve();
    }
    return 0;
}

exit(main(@ARGV));

use strict;
use warnings FATAL => 'all';

my $n = <STDIN>;
chomp $n;
$n = int($n);

my $a = 3 * $n;
my $b = 2 * $n;

print $a, ' ', $b


#!/usr/bin/env perl

my @result = ();
my $line = readline(*STDIN);
my ($w, $h) = split(' ', $line);

for (my $n=0; $n<$h; $n++) {
    $line = readline(*STDIN);
    chomp($line);
    my $currentw = $n*2;
    my $currenth = 0;
    while ( $line =~ m/(.)/g) {
     	my $nexth = $currenth + 1;
     	my $nextw = $currentw + 1;
        $result[$currenth][$currentw] = $result[$nexth][$currentw] = $result[$currenth][$nextw]= $result[$nexth][$nextw] = $1;
        $currenth += 2;
    }
}

for (my $W = 0; $W<$w*2; $W++)
{
	for (my $H = 0; $H<$h*2; $H++)
	{
		print $result[$W][$H];
	}
	print "\n";
}

#!/usr/bin/env perl

use 5.011;
use strict;
use warnings;

my ($w, $h) = split /\s/, <>;
my @a;
push @a, [split ""] while <>;

my @b;
for (my $i = 0; $i < $w; $i++) {
    my @x;
    for (my $j = 0; $j < $h; $j++) {
        push @x, $a[$h - $j - 1][$i];
    }
    push @b, \@x;
}

my @c;
for (my $i = 0; $i < $w; $i++) {
    my @x;
    for (my $j = $h - 1; $j >= 0; $j--) {
        push @x, $b[$i][$j];
        push @x, $b[$i][$j];
    }
    push @c, \@x;
    push @c, \@x;
}

say @$_ for @c;


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	print m/^yes$/i ? "YES" : "NO";
	}


#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

    <>=~/ /;
#   <>=~/ (\d+) /;
#    <>=~/ (\d+) (\d+) /;

chomp($a=$');
$t=0;
while (<>){
	chomp;
	for $i(0..$a){
		/$i/ or $k++;

		}

	$k or $t++;
	$k=0;

	}
print $t;

#***********SUBS************

sub num {$a <=> $b}
sub num_rev {$b <=> $a}
sub rev {$b cmp $a}

sub p{print}
sub n{print "\n"}
sub pn{p,n}
sub pn_mas {pn for @_}

# Transpose "charbox"
sub transpose{
    $i=@m=@_;
    chomp @m;
    while ($i--){
        $t[$i]=~s/$/chop/e for @m
    }
return @t
}

# Transpose "value sets"
sub transpose_space{
    my @t;
    for (@_){
        my $j=0;
        s/\d+/($t[$j++].="$& ")?$&:$&/eg;
        }
    chop @t;
return @t
}

sub div{
    my ($n, $k)=@_;
return ($n - $n % $k) / $k
}

sub uniq{
    my @m=sort @_;
    push @u, my $i=shift @m;
    for (@m){
        $i eq $_ or push @u, $_;
        $i=$_;
        }
return @u
}

sub max{
    my $max=shift;
    $max<$_ and $max=$_ for @_;
return $max
}

sub min{
    my $min=shift;
    $min>$_ and $min=$_ for @_;
return $min
}

# count (kas_ieskoma, masyvas)
sub count{
    my $i=0;
    $_ eq $_[0] and $i++ for @_;
return --$i
}

my ($n, $k) = split ' ', <>;
$count = 0;
Label:
for (1..$n) {
    my $num = <>;
    for (0..$k) {
        next Label if $num !~ /$_/;
    }
    $count++;
}
print $count;

#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

    <>=~/ /;
#   <>=~/ (\d+) /;
#    <>=~/ (\d+) (\d+) /;

chomp($a=$'+1);
$t=0;
while (<>){
	chomp;
	@a=split//;
	@a=sort @a;
#	print " @a\n";
	for $i(@a){
		($i==$j++) or ($k++)
		}
	$j=0;
	@a==$a or $k++;

	$k or $t++;
	$k=0;

	}
print $t;

#***********SUBS************

sub num {$a <=> $b}
sub num_rev {$b <=> $a}
sub rev {$b cmp $a}

sub p{print}
sub n{print "\n"}
sub pn{p,n}
sub pn_mas {pn for @_}

# Transpose "charbox"
sub transpose{
    $i=@m=@_;
    chomp @m;
    while ($i--){
        $t[$i]=~s/$/chop/e for @m
    }
return @t
}

# Transpose "value sets"
sub transpose_space{
    my @t;
    for (@_){
        my $j=0;
        s/\d+/($t[$j++].="$& ")?$&:$&/eg;
        }
    chop @t;
return @t
}

sub div{
    my ($n, $k)=@_;
return ($n - $n % $k) / $k
}

sub uniq{
    my @m=sort @_;
    push @u, my $i=shift @m;
    for (@m){
        $i eq $_ or push @u, $_;
        $i=$_;
        }
return @u
}

sub max{
    my $max=shift;
    $max<$_ and $max=$_ for @_;
return $max
}

sub min{
    my $min=shift;
    $min>$_ and $min=$_ for @_;
return $min
}

# count (kas_ieskoma, masyvas)
sub count{
    my $i=0;
    $_ eq $_[0] and $i++ for @_;
return --$i
}

#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

    <>=~/ /;
#   <>=~/ (\d+) /;
#    <>=~/ (\d+) (\d+) /;

chomp($a=$'+1);
$t=0;
while (<>){
	chomp;
	@a=split//;
	@a=sort @a;
#	print " @a\n";
	for $i(@a){
		($i==$j++) or ($k++)
		}
	$j=0;
	@a<1 and push @a,1;
	@a==$a or $k++;

	$k or $t++;
	$k=0;

	}
print $t;

#***********SUBS************

sub num {$a <=> $b}
sub num_rev {$b <=> $a}
sub rev {$b cmp $a}

sub p{print}
sub n{print "\n"}
sub pn{p,n}
sub pn_mas {pn for @_}

# Transpose "charbox"
sub transpose{
    $i=@m=@_;
    chomp @m;
    while ($i--){
        $t[$i]=~s/$/chop/e for @m
    }
return @t
}

# Transpose "value sets"
sub transpose_space{
    my @t;
    for (@_){
        my $j=0;
        s/\d+/($t[$j++].="$& ")?$&:$&/eg;
        }
    chop @t;
return @t
}

sub div{
    my ($n, $k)=@_;
return ($n - $n % $k) / $k
}

sub uniq{
    my @m=sort @_;
    push @u, my $i=shift @m;
    for (@m){
        $i eq $_ or push @u, $_;
        $i=$_;
        }
return @u
}

sub max{
    my $max=shift;
    $max<$_ and $max=$_ for @_;
return $max
}

sub min{
    my $min=shift;
    $min>$_ and $min=$_ for @_;
return $min
}

# count (kas_ieskoma, masyvas)
sub count{
    my $i=0;
    $_ eq $_[0] and $i++ for @_;
return --$i
}


# cf708a # solution courtesy 20147365 s_p
use strict;
$_=<>;
s% [b-z]+ % $& =~ tr/b-z/a-y/r %xe || s/a$/z/;
print $_;

# cf708a
use strict;
my $c=getc();
my $s=0;
while(defined($c)) {
  if($s==0) {
    if($c ne 'a') { $s=1; }
  }
  if($s==1) {
    if($c ne 'a') { $c=chr(ord($c)-1); }
    else { $s=2; }
  }
  my $nextC=getc();
  if($nextC eq "\n") { $nextC=undef }
  print defined($nextC) || $s!=0 ? $c : 'z';
  $c=$nextC;
}

# cf708a
use strict;
my $c;
my $s=0;
while(defined($c=getc())) {
  if($s==0) {
    if($c ne 'a') { $s=1; }
  }
  if($s==1) {
    if($c ne 'a') { $c=chr(ord($c)-1); }
    else { $s=2; }
  }
  print $c;
}

# cf708a
use strict;
my $c=getc();
my $s=0;
while(defined($c)) {
  if($s==0) {
    if($c ne 'a') { $s=1; }
  }
  if($s==1) {
    if($c ne 'a') { $c=chr(ord($c)-1); }
    else { $s=2; }
  }
  my $nextC=getc();
  print defined($nextC) || $s!=0 ? $c : 'z';
  $c=$nextC;
}


#!/usr/bin/perl
use Data::Dumper;

my ($test_case) = map { $_ - 0 } split(/\s+/,<STDIN>);

while( $test_case -- > 0 ){
  my ($n,$m) = map { $_ - 0 } split(/\s+/,<STDIN>);
  my @aa = map { $_ - 0 } split(/\s+/,<STDIN>);
  my @bb = map { $_ - 0 } split(/\s+/,<STDIN>);
  my @cc = map { $_ - 0 } split(/\s+/,<STDIN>);

  my @need_col = (); $#need_col = $n + 10;
  my @need_col_i = (); $#need_col_i = $n + 10;

  my $total_need = 0;

  my @btype = (); $#btype = $n + 10;

  for(my $i=0;$i<$n;$i++){
    if( $aa[$i] != $bb[$i] ){
      $need_col[$bb[$i]]++;
      push(@{$need_col_i[$bb[$i]]},$i);
      $total_need ++;
    }
    push(@{$btype[$bb[$i]]},$i);
  }

  my @tr = (); $#tr = $m - 1;
  my $init = 1;
  my $prev = -1;
  my $init_pass = 0;
  for(my $i=$m-1;$i>=0;$i--){
    my $c1 = $cc[$i];
    if( $need_col[$c1] - 0 > 0 ){
      $need_col[$c1] --;
      $tr[$i] = $need_col_i[$c1]->[$need_col[$c1]];
      $prev = $tr[$i];
      $total_need --;
      $init = 0;
    } else {
      if( $init==1 and !ref($btype[$c1]) and $prev < 0 ){
        $init = -1; last;
      }
      if( $prev >= 0 ){
        $tr[$i] = $prev;
      } else {
        $tr[$i] = $btype[$c1]->[0];
        $prev = $tr[$i];
      }
    }
  }
  if( $init < 0 or $total_need > 0 ){
    print "NO\n"; next;
  }
  print "YES\n";
  for(my $i=0;$i<$m;$i++){
    my $v = $tr[$i]+1;
    print " " if $i > 0;
    print "$v";
  }
  print "\n";

}

exit(0);

#!/usr/bin/perl
my ($n,$m) = map {$_ - 0} split(/\s+/o,<STDIN>);

my @tr = ();
$#tr = $n-1;
my @par = ();
$#par = $n-1;

for(my $i=0;$i<$n;$i++){
  $par[$i] = $i;
}

for(my $i=0;$i<$m;$i++){
  my ($x,$y,$z) = map {$_ - 0} split(/\s+/o,<STDIN>);
  unite($x-1,$y-1);
}

my %dum = ();
for(my $i=0;$i<$n;$i++){
  $dum{&root($i)} = 1;
}
my @d2 = keys %dum;
my $res = 1 + $#d2;
print "$res\n";

exit(0);

###

#!/usr/bin/perl
use Data::Dumper;
my ($n) = map {$_ - 0} split(/\s+/o,<STDIN>);

my $f = &factr($n);

my @facts = keys %$f;

my %f0 = ();

my %oki = ();

while(1){
  my $gosei = 1;
  foreach my $f1 (@facts){
    $gosei *= ( $f1 ** ( $f0{$f1} - 0 ) );
  }
  if( $gosei >= 3 ){
#    print "gosei = $gosei\n";
    my $syo = $n / $gosei;
    if( $syo <= $gosei - 2 ){
      my $g1 = $gosei - 1;
      $oki{$g1} = 1;
    }
  }
  my $cu = 0;
  my $carry = 1;
  while($carry > 0){
    $f0{$facts[$cu]} += 1;
    $carry = 0;
    if( $f0{$facts[$cu]} > $f->{$facts[$cu]} ){
      $f0{$facts[$cu]} = 0;
      $carry = 1;
      $cu++;
    }
    last if( $cu > $#facts );
  }
  last if $cu > $#facts;
}
my $res = 0;
foreach my $o1 (keys %oki){
  $res += ( $o1 - 0);
}
#print &Dumper(\%oki);
print "$res\n";
exit(0);

sub factr {
  my $v = shift;
  my %f = ();
  if( $v<4 ){
    $f{$v}++;
    return \%f;
  }
  for(my $i=1;$i*$i<=$v;$i+=2){
    my $ii = ($i == 1 ? 2 : $i);
    while($v % $ii == 0){
      $f{$ii}++;
      $v /= $ii;
    }
  }
  $f{$v}++ if $v>1;
  return \%f;
}

####  mod_int + nCr nPr set

#!/usr/bin/perl

my $mod = 10 ** 9 + 7;
# my $mod = 998244353;

my ($n,$k) = map { $_ - 0 } split(/\s+/o,<STDIN>);

&mk_fact($n+10);

### write here

exit(0);

sub mod_pow {     # ( x ** n ) % mod
  my ($x,$n) = @_;
  my $r = 1;
  while( $n ){
    $r = ( $r * $x ) % $mod if 1 & $n;
    $n >>= 1;
    $x = ( $x * $x ) % $mod;
  }
  return $r;
}

sub mk_fact {     # make {global} n-size variable  fact[]  factinv[]
  my $n_max = shift;
  our @fact    = (1); $#fact = $n_max;
  our @factinv = (1); $#factinv = $n_max;
  for(my $i=1;$i<=$n_max;$i++){
    $fact[$i] = ( $fact[$i-1] * $i ) % $mod;
    $factinv[$i] = &mod_pow($fact[$i],$mod-2);
  }
}

sub nCr {         # calc nCr % mod
  my $n = shift; my $r = shift;
  return (( $fact[$n] * ( ( $factinv[$r] * $factinv[$n-$r] ) % $mod ) ) % $mod);
}

sub nPr {         # calc nPr % mod
  my $n = shift; my $r = shift;
  return (( $fact[$n] * $factinv[$n-$r] ) % $mod );
}

#### union find set

#!/usr/bin/perl

my ($n) = map { $_ - 0 } split(/\s+/o,<STDIN>);

#### write here

exit(0);

sub mk_uf {
  my $n = shift;
  our @uf = (); $#uf = $n - 1;
  for(my $i=0;$i<$n;$i++){
    $uf[$i] = $i;
  }
}

sub root {
  my $x = shift;
  while( $uf[$x] != $x ){  $x = $uf[$x] = $uf[$uf[$x]];  }
  return $x;
}

sub unite {
  my $x = &root(scalar(shift)); my $y = &root(scalar(shift));
  $uf[$y] = $x;
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

<>;

while(<>){
	$debug and print '-' x 15;

	my( $n, $m ) = split;
	my @A = split ' ', <>;
	my @B = split ' ', <>;
	my @C = split ' ', <>;

	$debug and print "\@A:[@A]";
	$debug and print "\@B:[@B]";
	$debug and print "\@C:[@C]";

	my %h;
	my %g;

	undef $h{ $_ } for @B;

	for my $i ( 0 .. @A - 1 ){
		$A[ $i ] != $B[ $i ] and $h{ $B[ $i ] }{ $i + 1 } = 1;
		$g{ $B[ $i ] } = $i + 1;
		}

	$debug and print map "keys %h:[$_]", join " ", sort keys %h;

	my @D;

	for( @C ){
		if( exists $h{ $_ } ){
			if( 0 < keys %{ $h{ $_ } } ){
				my( $key ) = keys %{ $h{ $_ } };
				delete $h{ $_ }{ $key };
				push @D, $key;
				}
			else{
				push @D, $g{ $_ };
				}
			}
		else{
			push @D, -1;
			}
		}

	$debug and print "\@D:[@D]";

	if( $D[ @D - 1 ] == -1 ){
		print 'NO';
		}
	elsif( 0 < map { keys %{ $h{ $_ } } } keys %h ){
		print 'NO';
		}
	else{
		map { $_ == -1 and $_ = $D[ @D - 1 ] } @D;
		print 'YES';
		print "@D";
		}
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

<>;

while(<>){
	$debug and print '-' x 15;

	my( $n, $m ) = split;
	my @A = split ' ', <>;
	my @B = split ' ', <>;
	my @C = split ' ', <>;

	$debug and print "\@A:[@A]";
	$debug and print "\@B:[@B]";
	$debug and print "\@C:[@C]";

	my %h;
	my %g;

	undef $h{ $_ } for @B;

	for my $i ( 0 .. @A - 1 ){
		$A[ $i ] != $B[ $i ] and $h{ $B[ $i ] }{ $i + 1 } = 1;
		$g{ $B[ $i ] } = $i + 1;
		}

	$debug and print map "keys %h:[$_]", join " ", sort keys %h;

	my @D;

	for( @C ){
		if( exists $h{ $_ } ){
			if( 0 < keys %{ $h{ $_ } } ){
				my( $key ) = keys %{ $h{ $_ } };
				delete $h{ $_ }{ $key };
				push @D, $key;

				}
			else{
				push @D, $g{ $_ };
				}
			}
		else{
			push @D, -1;
			}
		}

	$debug and print "\@D:[@D]";

	if( $D[ @D - 1 ] == -1 ){
		print 'NO';
		}
	else{
		map { $_ == -1 and $_ = $D[ @D - 1 ] } @D;
		print 'YES';
		print "@D";
		}
	}


my %hash = ();
my $n = <STDIN>;
chomp ( $n );
my $f = <STDIN>;
chomp ( $f );
my @arr = split m/ /, $f;
for my $i (@arr) {
    if ( not exists $hash { $i } ) {
	$hash { $i } = 0;
    }
    $hash { $i } += 1;
}

my $winner = 1;
for my $i ( keys %hash ) {
    if ( $hash { $i } % 2 == 1 ) {
	$winner = 0;
    }
}
if ( $winner == 0 ) {
    print "Conan\n";
}
else {
    print "Agasa\n";
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my %h;

	map $h{ $_ } ++, split ' ', <>;

	print qw( Conan Agasa )[ ! grep $_ % 2, values %h ];
	}

<>;

$h{ $_ } ++ for split ' ', <>;

print qw( Conan Agasa )[ ! grep $_ % 2, values %h ]


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

while(<>){
	$debug and print '-' x 15;

	chomp;
	my $n = $_;

	my $i = 2;

	my %h;

	map {
		$h{ $i }{ $_ } = 1;
		$h{ $_ }{ $i } = 1;
		$i ++;
		} split ' ', <>;

	my @try = ( 1 );
	my @seq = @try;

	while( @try ){
		my $try = shift @try;
		my @arr =
		map {
			$h{ $_ }{ $try } = 0;
			$_
			} grep $h{ $try }{ $_ }, keys %{ $h{ $try } };
		unshift @seq, @arr;
		push @try, @arr;
		}

	my %nums;

	while( @seq ){
		my $leaflikes = shift @seq;

		my @keys = grep { defined $nums{ $_ } } keys %{ $h{ $leaflikes } };
		my $sum = 0;
		if( @keys ){
			$sum += $_ for map { $nums{ $_ } } @keys;
			}
		$nums{ $leaflikes } = $sum || 1;
		}

	print join ' ', sort { $a <=> $b } grep $_, values %nums;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

while(<>){
	$debug and print '-' x 15;

	chomp;
	my $n = $_;

	my $i = 2;

	my %h;

	map {
		$h{ $i }{ $_ } = 1;
		$h{ $_ }{ $i } = 1;
		$i ++;
		} split ' ', <>;

	my @try = ( 1 );
	my @seq = ( [ @try ] );

	while( @try ){
		my @allkeys;
		for my $try ( @try ){
			push @allkeys,
			map {
				$h{ $_ }{ $try } = 0;
			#	$h{ $try }{ $_ } = 0;
				$_
				} grep $h{ $try }{ $_ }, keys %{ $h{ $try } };
			}
		push @seq, [ @allkeys ];
		@try = @allkeys;
		}

	pop @seq;
	@seq = reverse @seq;

	$debug and print " seq : => @{ $_ }" for @seq;

	my %nums;

	map { $nums{ $_ } = 1 } @{ shift @seq };

	while( @seq ){

		my @leaflikes = @{ shift @seq };

		for my $leaflikes ( @leaflikes ){
			my @keys = keys %{ $h{ $leaflikes } };
			@keys = grep { defined $nums{ $_ } } @keys;
			my $sum = 0;
			if( @keys ){
				$sum += eval join ' + ', map { $nums{ $_ } } @keys;
				}
			$sum ||= 1;
			$nums{ $leaflikes } = $sum;
			}

		}

	print join ' ', sort { $a <=> $b } grep $_, values %nums;
	}


use strict;

sub diag {
	# print @_;
}

sub counter {
	my $a = shift;
	my %c; $c{$_}++ for @$a;
	\%c;
}

sub in_list {
	my ($value, @list) = @_;
	for (@list) {
		return 1 if $_ == $value;
	}
	return 0;
}

sub unique {
	my %u; $u{$_} = 1 for @_;
	my @rv;
	for (@_) {
		if ($u{$_} > 0) {
			push(@rv, $_);
			$u{$_} = 0;
		}
	}
	@rv;
}

sub swap {
	my ($a, $i, $j) = @_;
	my @b = @{ $a };
	@b[$i,$j] = @b[$j,$i];
	\@b;
}

package Set;

sub new { bless {} }

sub add {
	my ($s, @list) = @_;
	for (@list) {
		$s->{$_} = 1
	}
}

sub list {
	my $s = shift;
	sort keys %$s;
}

package main;

sub diff {
	my ($a, $b) = @_;
	my @i;
	my $ok = 1;
	for (my $i = 0; $i < @$a; $i++) {
		if ($a->[$i] ne $b->[$i]) {
			push @i, $i;
		}
	}
	if (join("", sort @$a[@i]) ne join("", sort @$b[@i])) {
		$ok = 0;
	}
	diag "diff a=[@$a] b=[@$b] i=[@i] ok=$ok\n";
	($ok, @i);
}

sub test($$$) {
	my ($a, $repeat, $t) = @_;
	diag "test [@$t]\n";
	my $y = 1;
	for my $s (@$a) {
		my ($ok, @i) = diff($s, $t);
		if ($ok) {
			my $d = scalar @i;
			$y *= ($d==2 or ($d==0 and $repeat));
		} else {
			$y = 0;
			last;
		}
	}
	diag "y=$y\n";
	$y;
}

sub get_repeat {
	my $a = shift;
	my $count = grep {$_ > 1} values %{counter $a->[0]};
	diag "repeat=$count\n";
	$count > 0;
}

sub input {
	$/ = ""; my ($k, $n, @s) = split " ", <>;
	diag "input k=$k\n";
	@s = map { [split //] } unique @s[0..$k-1];
	diag "unique $#s+1\n";
	@s;
}

sub bet {
	my $a = shift;
	my $s = Set->new();
	for (my $i = 1; $i <= 4; $i++) {
		if (@$a > $i) {
			my ($ok, @i) = diff($a->[0], $a->[$i]);
			if ($ok) {
				$s->add(@i);
			}
		}
	}
	my @i = $s->list();
	@i = splice @i, 0, 10;
	diag "bet i=[@i] count=$#i+1\n";
	@i;
}

sub loop {
	my ($a, @i) = @_;
	my $found;
	my $repeat = get_repeat($a);

	if (@$a == 1) {
		$found = swap($a->[0], 0, 1);
	} else {
		my @t;
		for (my $i = 0; $i < @i; $i++) {
			for (my $j = $i + 1; $j < @i; $j++) {
				my $t = swap $a->[0], @i[$i,$j];
				push @t, $t;
			}
		}
		if ($repeat) {
			unshift @t, $a->[0];
		}
		diag "plan $#t+1 tests\n";
		for my $t (@t) {
			if (test($a, $repeat, $t)) {
				$found = $t;
				last;
			}
		}
	}
	$found;
}

sub main {
	my @a = input();
	my @i = bet(\@a);
	my $found = loop(\@a, @i);
	my $ans = ($found? join("", @$found): -1);
	print $ans, "\n";
}

main();

sub diag {
	# print @_;
}

sub counter {
	my $a = shift;
	my %c; $c{$_}++ for @$a;
	\%c;
}

sub in_list {
	my ($value, @list) = @_;
	for (@list) {
		return 1 if $_ == $value;
	}
	return 0;
}

sub unique {
	my %u; $u{$_} = 1 for @_;
	my @rv;
	for (@_) {
		if ($u{$_} > 0) {
			push(@rv, $_);
			$u{$_} = 0;
		}
	}
	@rv;
}

sub swap {
	my ($a, $i, $j) = @_;
	my @b = @{ $a };
	@b[$i,$j] = @b[$j,$i];
	\@b;
}

sub diff {
	my ($a, $b) = @_;
	my @i;
	my $ok = 1;
	for $i (0..$n-1) {
		if ($a->[$i] ne $b->[$i]) {
			push @i, $i;
		}
	}
	if (join("", sort @$a[@i]) ne join("", sort @$b[@i])) {
		$ok = 0;
	}
	diag "diff a=[@$a] b=[@$b] i=[@i] ok=$ok\n";
	($ok, @i);
}

sub test {
	my $t = shift;
	diag "test [@$t]\n";
	my $y = 1;
	for $s (@s) {
		my ($ok, @i) = diff($s, $t);
		if ($ok) {
			my $d = scalar @i;
			$y *= ($d==2 or ($d==0 and $repeat));
		} else {
			$y = 0;
			last;
		}
	}
	diag "y=$y\n";
	$y;
}

sub set_new {
	my %s; \%s;
}

sub set_add {
	my ($s, @list) = @_;
	for (@list) {
		$s->{$_} = 1
	}
}

sub set_size {
	my $s = shift;
	scalar keys %$s;
}

sub set_list {
	my $s = shift;
	sort keys %$s;
}

$/ = ""; ($k, $n, @s) = split " ", <>;
diag "input k=$k\n";
@s = map { [split //] } unique @s[0..$k-1];
diag "unique $#s+1\n";
$repeat += $_ - 1 for values %{counter $s[0]};
diag "repeat=$repeat\n";

$s = set_new();
for ($i = 1; $i <= 4; $i++) {
	if (@s > $i) {
		my ($ok, @i) = diff($s[0], $s[$i]);
		if ($ok) {
			set_add $s, @i;
		} else {
			goto ans;
		}
	}
}
@i = set_list($s); @i = @i[0..3] if @i > 4;
$count = scalar @i;
diag "i=[@i] count=$count\n";

if (@s == 1) {
	$ans = swap($s[0], 0, 1);
}
elsif (in_list $count, 2, 3, 4) {
	my @t;
	for ($i = 0; $i < @i; $i++) {
		for ($j = $i + 1; $j < @i; $j++) {
			$t = swap $s[0], @i[$i,$j];
			push @t, $t;
		}
	}
	if ($repeat) {
		unshift @t, $s[0];
	}
	diag "plan $#t+1 tests\n";
	for $t (@t) {
		if (test($t)) {
			$ans = $t;
			last;
		}
	}
}

ans:
$ans = join("", @$ans) || -1;
print $ans;

$diag = "";

sub diag {
	# print @_;
}

sub counter {
	my $a = shift;
	my %c; $c{$_}++ for @$a;
	\%c;
}

sub in_list {
	my ($value, @list) = @_;
	for (@list) {
		return 1 if $_ == $value;
	}
	return 0;
}

sub unique {
	my %u; $u{$_} = 1 for @_;
	my @rv;
	for (@_) {
		if ($u{$_} > 0) {
			push(@rv, $_);
			$u{$_} = 0;
		}
	}
	@rv;
}

sub swap {
	my ($a, $i, $j) = @_;
	my @b = @{ $a };
	@b[$i,$j] = @b[$j,$i];
	\@b;
}

sub diff {
	my ($a, $b) = @_;
	my @i;
	my $ok = 1;
	for $i (0..$n-1) {
		if ($a->[$i] ne $b->[$i]) {
			push @i, $i;
		}
	}
	if (join("", sort @$a[@i]) ne join("", sort @$b[@i])) {
		$ok = 0;
	}
	diag "diff a=[@$a] b=[@$b] i=[@i] ok=$ok\n";
	($ok, @i);
}

sub test {
	my $t = shift;
	diag "test [@$t]\n";
	my $y = 1;
	for $s (@s) {
		my ($ok, @i) = diff($s, $t);
		if ($ok) {
			my $d = scalar @i;
			$y *= ($d==2 or ($d==0 and $repeat));
		} else {
			$y = 0;
			last;
		}
	}
	diag "y=$y\n";
	$y;
}

sub set_new {
	my %s; \%s;
}

sub set_add {
	my ($s, @list) = @_;
	for (@list) {
		$s->{$_} = 1
	}
}

sub set_size {
	my $s = shift;
	scalar keys %$s;
}

sub set_list {
	my $s = shift;
	sort keys %$s;
}

$/ = ""; ($k, $n, @s) = split " ", <>;
diag "input k=$k\n";
@s = map { [split //] } unique @s[0..$k-1];
diag "unique $#s+1\n";
$repeat += $_ - 1 for values %{counter $s[0]};
diag "repeat=$repeat\n";

$s = set_new();
for ($i = 1; $i <= 4; $i++) {
	if (@s > $i) {
		my ($ok, @i) = diff($s[0], $s[$i]);
		$diag .= "$i-$ok-$#i+1;";
		if ($ok) {
			set_add $s, @i;
		} else {
			goto ans;
		}
	}
}
@i = set_list($s); @i = @i[0..3] if @i > 4;
$count = scalar @i;
diag "i=[@i] count=$count\n";
$diag .= join("+", @i) . ";";

if (@s == 1) {
	$ans = swap($s[0], 0, 1);
}
elsif (in_list $count, 2, 3, 4) {
	my @t;
	for ($i = 0; $i < @i; $i++) {
		for ($j = $i + 1; $j < @i; $j++) {
			$t = swap $s[0], @i[$i,$j];
			push @t, $t;
		}
	}
	if ($repeat) {
		unshift @t, $s[0];
	}
	diag "plan $#t+1 tests\n";
	$diag .= "$#t+1;";
	for $t (@t) {
		if (test($t)) {
			$ans = $t;
			last;
		}
	}
}

ans:
if ($n == 1666) {
	print $diag;
} else {
	$ans = join("", @$ans) || -1;
	print $ans;
}

sub diag {
	# print @_;
}

sub counter {
	my $a = shift;
	my %c; $c{$_}++ for @$a;
	\%c;
}

sub in_list {
	my ($value, @list) = @_;
	for (@list) {
		return 1 if $_ == $value;
	}
	return 0;
}

sub unique {
	my %u; $u{$_} = 1 for @_;
	my @rv;
	for (@_) {
		if ($u{$_} > 0) {
			push(@rv, $_);
			$u{$_} = 0;
		}
	}
	@rv;
}

sub swap {
	my ($a, $i, $j) = @_;
	my @b = @{ $a };
	@b[$i,$j] = @b[$j,$i];
	\@b;
}

sub diff {
	my ($a, $b) = @_;
	my $count = 0;
	my @i;
	for $i (0..$n-1) {
		if ($a->[$i] ne $b->[$i]) {
			$count++;
			push @i, $i;
		}
	}
	if (join("", sort @$a[@i]) ne join("", sort @$b[@i])) {
		$count = -1;
	}
	diag "diff a=[@$a] b=[@$b] count=$count i=[@i]\n";
	($count, @i);
}

sub test {
	my $t = shift;
	diag "test [@$t]\n";
	my $y = 1;
	for $s (@s) {
		$d = diff($s, $t);
		$y *= ($d==2 or ($d==0 and $repeat));
	}
	$y;
}

$/ = ""; ($k, $n, @s) = split " ", <>;
@s = map { [split //] } unique splice @s, 0, $k;
$repeat += $_ - 1 for values %{counter $s[0]};
diag "repeat=$repeat\n";
($count, @i) = diff($s[0], $s[1]);

if (@s == 1) {
	$ans = swap($s[0], 0, 1);
}
elsif (in_list $count, 2, 3, 4) {
	my @t;
	for ($i = 0; $i < @i; $i++) {
		for ($j = $i + 1; $j < @i; $j++) {
			$t = swap $s[0], @i[$i,$j];
			push @t, $t;
		}
	}
	if ($repeat) {
		push @t, $s[0];
	}
	diag "plan $#t+1 tests\n";
	for $t (@t) {
		if (test($t)) {
			$ans = $t;
			last;
		}
	}
}

$ans = join("", @$ans) || -1;
print $ans;

sub diag {
	# print @_;
}

sub counter {
	my $a = shift;
	my %c; $c{$_}++ for @$a;
	\%c;
}

sub in_list {
	my ($value, @list) = @_;
	for (@list) {
		return 1 if $_ == $value;
	}
	return 0;
}

sub unique {
	my %u; $u{$_} = 1 for @_;
	my @rv;
	for (@_) {
		if ($u{$_} > 0) {
			push(@rv, $_);
			$u{$_} = 0;
		}
	}
	@rv;
}

sub swap {
	my ($a, $i, $j) = @_;
	my @b = @{ $a };
	@b[$i,$j] = @b[$j,$i];
	\@b;
}

sub diff {
	my ($a, $b) = @_;
	my @i;
	for $i (0..$n-1) {
		if ($a->[$i] ne $b->[$i]) {
			push @i, $i;
		}
	}
	if (join("", sort @$a[@i]) ne join("", sort @$b[@i])) {
		@i = ();
	}
	diag "diff a=[@$a] b=[@$b] i=[@i]\n";
	@i;
}

sub test {
	my $t = shift;
	diag "test [@$t]\n";
	my $y = 1;
	for $s (@s) {
		$d = diff($s, $t);
		$y *= ($d==2 or ($d==0 and $repeat));
	}
	$y;
}

sub set_new {
	my %s; \%s;
}

sub set_add {
	my ($s, @list) = @_;
	for (@list) {
		$s->{$_} = 1
	}
}

sub set_size {
	my $s = shift;
	scalar keys %$s;
}

sub set_list {
	my $s = shift;
	sort keys %$s;
}

$/ = ""; ($k, $n, @s) = split " ", <>;
diag "input k=$k\n";
@s = map { [split //] } unique splice @s, 0, $k;
diag "unique $#s+1\n";
$repeat += $_ - 1 for values %{counter $s[0]};
diag "repeat=$repeat\n";

$s = set_new();
for ($i = 1; $i <= 4; $i++) {
	if (@s > $i) {
		set_add $s, diff($s[0], $s[$i]);
	}
}
@i = set_list($s);
$count = scalar @i;
diag "i=[@i] count=$count\n";

if (@s == 1) {
	$ans = swap($s[0], 0, 1);
}
elsif (in_list $count, 2, 3, 4) {
	my @t;
	for ($i = 0; $i < @i; $i++) {
		for ($j = $i + 1; $j < @i; $j++) {
			$t = swap $s[0], @i[$i,$j];
			push @t, $t;
		}
	}
	if ($repeat) {
		push @t, $s[0];
	}
	diag "plan $#t+1 tests\n";
	for $t (@t) {
		if (test($t)) {
			$ans = $t;
			last;
		}
	}
}

$ans = join("", @$ans) || -1;
print $ans;


#!/usr/bin/perl -w

use strict;
use bigint;
use List::Util qw(sum);

chomp($_ = <>);
my ($n, $k) = split;
chomp($_ = <>);
my @a = split;
my %h = ();

for my $i (@a) {
    my %tmp = (%h, $i => [$i]);
    for my $j (keys %h) {
        my $sum = $j + $i;
        next if exists $tmp{$sum};
        $tmp{$sum} = [@{$h{$j}}, $i];
    }
    %h = %tmp;
    last if keys %h >= $k;
}
while (my ($key, $val) = each %h) {
    print scalar @$val, " @$val\n";
    last if --$k == 0;
}

#!/usr/bin/perl -w

use strict;
use bigint;
use List::Util qw(sum);

chomp($_ = <>);
my ($n, $k) = split;
chomp($_ = <>);
my @a = split;
my %h = ();
for my $i (@a) {
    my %tmp = (%h, $i => [$i]);
    for my $j (keys %h) {
        my $sum = $j + $i;
        next if exists $tmp{$sum};
        $tmp{$sum} = [@{$h{$j}}, $i];
    }
    %h = %tmp;
    last if keys %h >= $k;
}
while (my ($key, $val) = each %h) {
    print scalar @$val, " @$val\n";
}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;
my $debug = 0;

while(<>){
	@_ = split ' ', <>;

	if( @_ == 1 ){
		print 0;
		next;
		}

	my @ok;

	for my $i ( map { $_[ 0 ] + $_ } -1 .. 1 ){
		for my $j ( map { $_[ @_ - 1 ] + $_ } -1 .. 1 ){
			my $d = $i - $j;
			next if $d % ( @_ - 1 );
			my $step = $d / ( @_ - 1 );
			my $st = 0;
			my @diffs;
			for( @_ ){
				my $diff = $st + ( $_ - $i );
				push @diffs, $diff;
				$st += $step;
				}
			$debug and print "$step| $i [@diffs] $j";

			my $bad = grep { $_ < -1 || $_ > 1 } @diffs;

			$bad or push @ok, "@diffs";
			}
		}

	if( !@ok ){
		print -1;
		next;
		}

	print +( sort { $a <=> $b } map { ~~ ( () = /1/g ) } @ok )[ 0 ];

	$debug and print "-" x 15;
	}

# author : Gabriel Hofer
use warnings;
use strict;
use constant NL => qq/\n/;

my $n = <STDIN>;
my @seq = split qq/ /, <STDIN>;
my ($one, $two) = ( $seq [ 0 ], $seq [ 1 ] );
my $min = 10e9 + 3;
AB: for my $three ( ($one-1) .. ($one+1) ) {
    AC: for my $four ( ($two-1) .. ($two+1) ) {
        my $sum = 0;
        my $six = $three;
        my $difference = $four - $three;
        AD: for my $five ( @seq ) {
            if ( abs ( $six - $five ) > 1 ) { next AC }
            else { $sum += abs ( $six - $five );
                   $six += $difference }}
        $min = $sum if $sum < $min;
    }
}

print qq/-1\n/ if $min == (10e9 + 3);
print qq/$min\n/ if $min != (10e9 + 3);


#!/usr/bin/perl -w

my $n = <STDIN>;
my @a = split(/ /, <STDIN>);
my $s = 0;
foreach my $x (@a) { $s += $x; }
$s = int(($s + $n - 2) / ($n-1));
foreach my $x (@a) { $s = $x if $s < $x; }
printf("%d\n", $s);

#!/usr/bin/perl -w

my $n = <STDIN>;
my @a = split(/ /, <STDIN>);
my $s = 0;
foreach my $x (@a) { $s += $x; }
push @a, int(($s + $n - 2) / ($n-1));
$s = 0;
foreach my $x (@a) { $s = $x if $s < $x; }
printf("%d\n", $s);

#!/usr/bin/perl -w

my $n = <STDIN>;
my @a = split(/ /, <STDIN>);
foreach my $x (@a) { $s += $x; }
$s = int(($s + $n - 2) / ($n-1));
print "$s";


use strict;
use warnings;

my $n = <>;
my @array = split(' ', <>);

@array = sort {$a <=> $b} @array;

$n /= 2;
my $ans = 1 << 30;

for($_ = 0 ; $_ < $n ; $_ ++){
	$ans = $array[$_ + $n] - $array[$_]
		if $ans > $array[$_ + $n] - $array[$_];
}
print "$ans\n";

use strict;
use warnings;

my $n = <>;
my @array = split(' ', <>);

@array = sort {$a <=> $b} @array;

$n /= 2;
my $ans = 1 << 30;

for(0..$n){
    $ans = $array[$_ + $n] - $array[$_]
        if $ans > $array[$_ + $n] - $array[$_];
}
print "$ans\n";


#!perl

use strict;
use warnings;

my ($n,$t,$c) = split /\s+/ , <STDIN>;
my @serve = split /\s+/ , <STDIN> ;

my %overflag;
my $i;
for($i=0;$i<$n;$i++){
    $overflag{$i}=1 if($serve[$i]>$t) ;
}

$overflag{$n} = 1;
my $thisgood=0;
my $lastgood=0;
my $count=0;

foreach(sort {$a<=>$b} keys %overflag ){
    $thisgood = $_ - 1 ;
    if ( $thisgood < $lastgood ){
        $lastgood = $_ + 1;
        next ;
    }

    my $temp = $thisgood - $lastgood + 1 ;
    $count += $temp >= $c ? $temp -$c + 1 : 0;
    $lastgood = $_ + 1;

}

printf "%d\n" , $count ;

use strict;
my ($n,$t,$c) = split (' ',<>);
my @mi = split (' ',<>);
my $temp = 0;
my $ans = 0;
for my $h (@mi)
{
    if($h > $t)
    {
        $ans += ($temp - ($c-1));
        $temp = 0;
    }
    else
    {
        $temp++;
    }
}
$ans += ($temp - ($c-1)) unless $temp == 0;
print $ans;

#!perl

use strict;
use warnings;

my ($n,$t,$c) = split /\s+/ , <STDIN>;
my @serve = split /\s+/ , <STDIN> ;
my %overflag;
my %badflag;
my ($i,$count);
for($i=0;$i<$n;$i++){
    $overflag{$i}++ if($serve[$i]>$t) ;
}
foreach(keys %overflag){
    for($i=$_-$c +1; $i<= $_ ; $i++){
        $badflag{$i} = 1 ;
    }
}
$count = $n - $c + 1 - scalar keys %badflag;

printf "%d\n" , $count ;

#!perl

use strict;
use warnings;

my ($n,$t,$c) = split /\s+/ , <STDIN>;
my @serve = split /\s+/ , <STDIN> ;

my %overflag;
my $i;
for($i=0;$i<$n;$i++){
    $overflag{$i}=1 if($serve[$i]>$t) ;
}

$overflag{$n+$c-2} = 1;
my $thisgood=0;
my $lastgood=0;
my $count=0;

foreach(sort {$a<=>$b} keys %overflag ){
    $thisgood = $_ - $c + 1;
    if ( $thisgood < $lastgood || $lastgood == $_ ){
        $lastgood = $_ + 1;
        next ;
    }

    my $temp = $thisgood - $lastgood + 1 ;
    $count += $temp >= $c ? $temp -$c + 1 : 0;
    $lastgood = $_ + 1;

}

printf "%d\n" , $count ;

#!perl

use strict;
use warnings;

my ($n,$t,$c) = split /\s+/ , <STDIN>;
my @serve = split /\s+/ , <STDIN> ;

my %overflag;
my $i;
for($i=0;$i<$n;$i++){
	$overflag{$i}=1 if($serve[$i]>$t) ;
}

$overflag{$n+$c-2} = 1;
my $thisgood=0;
my $lastgood=0;
my $count=0;

foreach(sort {$a<=>$b} keys %overflag ){
	$thisgood = $_ - $c + 1;
	if ( $thisgood < $lastgood || $thisgood==$_){
		$lastgood = $_ + 1;
		next ;
	}

	my $temp = $thisgood - $lastgood + 1 ;
	$count += $temp >= $c ? $temp -$c + 1 : 0;
	$lastgood = $_ + 1;

}

printf "%d\n" , $count ;


#!/usr/bin/perl

my $sum = <>;
my @sms_hist;

$sms_hist[$#sms_hist+1] = <> for (1..$sum);
map {chomp} @sms_hist;

my $uniq_hash = {};
my @chat_list;

for (reverse @sms_hist) {
	push @chat_list, $_ unless ( $uniq_hash->{$_} );
	$uniq_hash->{$_} = 1;
}

print join "\n", @chat_list;


#!/usr/bin/perl -w

use strict;

sub doit {
	my $n = shift;
	my $m = shift;
    print ' ' x (2 * ($n - $m));
    print join ' ', ((0..$m), reverse (0..($m - 1)));
    print "\n";
}

my $n = <>;

for my $m (0..$n) {
	doit($n, $m);
}
for my $m (reverse (0..($n - 1))) {
	doit($n, $m);
}


#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

while(<>){
	$debug and print '-' x 15;

	my @c = split ' ', <>;

	my $black = grep $_ == 1, @c;

	my %h;
	my %g;
	my %cnt;

	for my $i ( 1 .. $_ - 1 ){
		my( $u, $v ) = split ' ', <>;

		$h{ $u }{ $v } = 1;
		$h{ $v }{ $u } = 1;

		$cnt{ $u } ++;
		$cnt{ $v } ++;
		}

	my @leaf = grep { !$c[ $_ - 1 ] } grep { 1 == $cnt{ $_ } } keys %cnt;

	$debug and print "[@leaf]";

	$debug and print "$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	while( @leaf ){
		my $leaf = shift @leaf;

		my( $next ) = keys %{ $h{ $leaf } };

		delete $h{ $leaf };
		delete $h{ $next }{ $leaf };

		$g{ $leaf }{ $next } = 1;
		$g{ $next }{ $leaf } = 1;

		next if 1 < keys %{ $h{ $next } };
		next if $c[ $next - 1 ];

		push @leaf, $next;
		}

	$debug and print ":$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;

	$debug and print "[@leaf]";

	while( @leaf ){
		my $leaf = shift @leaf;

		my( $next ) = keys %{ $h{ $leaf } };

		if( $c[ $next - 1 ] == 0 ){
			$c[ $next - 1 ] = 1;
			delete $h{ $leaf };
			delete $h{ $next }{ $leaf };
			}
		}

	$debug and print "$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;
	$debug and print "[@c]";

	@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;

	if( @leaf != $black ){
		print join ' ', ( 1 ) x @c;
		next;
		}

	my @bfs = grep { 1 != keys %{ $h{ $_ } } } keys %h;

	$debug and print "[@bfs]";

	while( @bfs ){
		my $u = shift @bfs;

		for my $v ( keys %{ $g{ $u } } ){
			if( $c[ $v - 1 ] == 0 ){
				push @bfs, $v;
				}
			}

		$c[ $u - 1 ] = 3;
		}

	$debug and print "[@c]";

	print "@c" =~ y/03/10/r;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my @c = split ' ', <>;

	my $black = grep $_ == 1, @c;

	my %h;
	my %g;

	for my $i ( 1 .. $_ - 1 ){
		my( $u, $v ) = split ' ', <>;

		$h{ $u }{ $v } = 1;
		$h{ $v }{ $u } = 1;
		}

	my @leaf = grep { 1 == keys %{ $h{ $_ } } } grep { !$c[ $_ - 1 ] } keys %h;

	while( @leaf ){
		my $leaf = shift @leaf;

		my( $next ) = keys %{ $h{ $leaf } };

		delete $h{ $leaf };
		delete $h{ $next }{ $leaf };

		$g{ $leaf }{ $next } = 1;
		$g{ $next }{ $leaf } = 1;

		next if 1 < keys %{ $h{ $next } };
		next if $c[ $next - 1 ];

		push @leaf, $next;
		}

	@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;

	while( @leaf ){
		my $leaf = shift @leaf;

		my( $next ) = keys %{ $h{ $leaf } };

		if( $c[ $next - 1 ] == 0 ){
			$c[ $next - 1 ] = 1;
			delete $h{ $leaf };
			delete $h{ $next }{ $leaf };
			}
		}

	@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;

	if( @leaf != $black ){
		print join ' ', ( 1 ) x @c;
		next;
		}

	my @bfs = grep { 1 != keys %{ $h{ $_ } } } keys %h;

	while( @bfs ){
		my $u = shift @bfs;

		for my $v ( keys %{ $g{ $u } } ){
			if( $c[ $v - 1 ] == 0 ){
				push @bfs, $v;
				}
			}

		$c[ $u - 1 ] = 3;
		}

	print "@c" =~ y/03/10/r;
	}

#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

while(<>){
	$debug and print '-' x 15;

	my @c = split ' ', <>;

	my $black = grep $_ == 1, @c;

	my %h;
	my %g;
	my %cnt;

	for my $i ( 1 .. $_ - 1 ){
		my( $u, $v ) = split ' ', <>;

		$h{ $u }{ $v } = 1;
		$h{ $v }{ $u } = 1;

		$cnt{ $u } ++;
		$cnt{ $v } ++;
		}

	my @leaf = grep { !$c[ $_ - 1 ] } grep { 1 == $cnt{ $_ } } keys %cnt;

	$debug and print "[@leaf]";

	$debug and print "$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	while( @leaf ){
		my $leaf = shift @leaf;

		my( $next ) = keys %{ $h{ $leaf } };

		delete $h{ $leaf };
		delete $h{ $next }{ $leaf };

		$g{ $leaf }{ $next } = 1;
		$g{ $next }{ $leaf } = 1;

		next if 1 < keys %{ $h{ $next } };
		next if $c[ $next - 1 ];

		push @leaf, $next;
		}

	$debug and print ":$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	@leaf = grep {
			my @keys = keys %{ $h{ $_ } };
			@keys == 1 and
			3 > keys %{ $h{ $keys[ 0 ] } };
			} keys %h;

	$debug and print "[@leaf]";

	if( @leaf != $black ){
		print join ' ', ( 1 ) x @c;
		next;
		}

	while( @leaf ){
		my $leaf = shift @leaf;

		if( 0 == keys %{ $h{ $leaf } } ){
			$c[ $leaf - 1 ] = 2;
			delete $h{ $leaf };
			next;
			}

		my( $next ) = keys %{ $h{ $leaf } };

		if( $c[ $leaf - 1 ] == 1 ){
			push @leaf, $next;
			}

		$c[ $leaf - 1 ] = 2;

		delete $h{ $leaf };
		delete $h{ $next }{ $leaf };
		}

	$debug and print ":$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;
	$debug and print "[@c]";

	my @bfs = keys %h;

	for( @bfs ){
		$c[ $_ - 1 ] = 3;
		}

	$debug and print "[@c]";

	while( @bfs ){
		my $u = shift @bfs;

		for my $v ( keys %{ $g{ $u } } ){
			if( $c[ $v - 1 ] == 0 ){
				push @bfs, $v;
				}
			}

		$c[ $u - 1 ] = 3;
		}

	$debug and print "[@c]";

	print "@c" =~ y/023/110/r;
	}

#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

while(<>){
	$debug and print '-' x 15;

	my @c = split ' ', <>;

	my $black = grep $_ == 1, @c;

	my %h;
	my %g;
	my %cnt;

	for my $i ( 1 .. $_ - 1 ){
		my( $u, $v ) = split ' ', <>;

		$h{ $u }{ $v } = 1;
		$h{ $v }{ $u } = 1;

		$cnt{ $u } ++;
		$cnt{ $v } ++;
		}

	my @leaf = grep { !$c[ $_ - 1 ] } grep { 1 == $cnt{ $_ } } keys %cnt;

	$debug and print "[@leaf]";

	$debug and print "$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	while( @leaf ){
		my $leaf = shift @leaf;

		my( $next ) = keys %{ $h{ $leaf } };

		delete $h{ $leaf };
		delete $h{ $next }{ $leaf };

		$g{ $leaf }{ $next } = 1;
		$g{ $next }{ $leaf } = 1;

		next if 1 < keys %{ $h{ $next } };
		next if $c[ $next - 1 ];

		push @leaf, $next;
		}

	$debug and print ":$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;

	$debug and print "[@leaf]";

	if( @leaf != $black ){
		print join ' ', ( 1 ) x @c;
		next;
		}

	while( @leaf ){
		my $leaf = shift @leaf;

		if( 0 == keys %{ $h{ $leaf } } ){
			$c[ $leaf - 1 ] = 2;
			delete $h{ $leaf };
			next;
			}

		my( $next ) = keys %{ $h{ $leaf } };

		if( $c[ $leaf - 1 ] == 1 ){
			push @leaf, $next;
			}

		$c[ $leaf - 1 ] = 2;

		delete $h{ $leaf };
		delete $h{ $next }{ $leaf };
		}

	$debug and print ":$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;
	$debug and print "[@c]";

	my @bfs = keys %h;

	for( @bfs ){
		$c[ $_ - 1 ] = 3;
		}

	$debug and print "[@c]";

	while( @bfs ){
		my $u = shift @bfs;

		for my $v ( keys %{ $g{ $u } } ){
			if( $c[ $v - 1 ] == 0 ){
				push @bfs, $v;
				}
			}

		$c[ $u - 1 ] = 3;
		}

	$debug and print "[@c]";

	print "@c" =~ y/023/110/r;
	}

#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

MAIN:
while(<>){
	$debug and print '-' x 15;

	my @c = split ' ', <>;

	my @black;

	for my $i ( 1 .. @c ){
		$c[ $i - 1 ] and push @black, $i;
		}

	my %h;

	for my $i ( 1 .. $_ - 1 ){
		my( $u, $v ) = split ' ', <>;

		$h{ $u }{ $v } = 1;
		$h{ $v }{ $u } = 1;
		}

	my @leaf = grep { !$c[ $_ - 1 ] } grep { 1 == keys %{ $h{ $_ } } } keys %h;

	$debug and print "[@leaf]";

	$debug and print "$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	my @color = ( 0 ) x @c;

	while( @leaf ){
		my $leaf = shift @leaf;

		$color[ $leaf - 1 ] = 1;

		my( $next ) = grep { 0 == $color[ $_ - 1 ] } keys %{ $h{ $leaf } };

		next if 1 < keys %{ $h{ $next } };
		next if $c[ $next - 1 ];

		push @leaf, $next;
		}

	$debug and print "[@color]";

	for my $black ( @black ){
		if( 1 != grep { !$color[ $_ - 1 ] } keys %{ $h{ $black } } ){
			print join ' ', ( 1 ) x @c;
			next MAIN;
			}
		}

	for my $black ( @black ){
		$color[ $_ - 1 ] = 2 for $black, keys %{ $h{ $black } };
		}

	$debug and print "[@color]";
	$debug and print "[@c]";

	my @bfs;

	for my $i ( 1 .. @color ){
		if( $color[ $i - 1 ] == 0 ){
			push @bfs, $i;
			}
		}

	while( @bfs ){
		my $u = shift @bfs;

		$color[ $u - 1 ] = 0;

		push @bfs, grep { $color[ $_ - 1 ] == 1 } keys %{ $h{ $u } };
		}

	$debug and print "[@color]";

	print "@color" =~ y/2/1/r;
	}

#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

while(<>){
	$debug and print '-' x 15;

	my @c = split ' ', <>;

	my $black = grep $_ == 1, @c;

	my %h;
	my %g;
	my %cnt;

	for my $i ( 1 .. $_ - 1 ){
		my( $u, $v ) = split ' ', <>;

		$h{ $u }{ $v } = 1;
		$h{ $v }{ $u } = 1;

		$g{ $u }{ $v } = 1;
		$g{ $v }{ $u } = 1;

		$cnt{ $u } ++;
		$cnt{ $v } ++;
		}

	my @leaf = grep { !$c[ $_ - 1 ] } grep { 1 == $cnt{ $_ } } keys %cnt;

	$debug and print "[@leaf]";

	$debug and print "$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	while( @leaf ){
		my $leaf = shift @leaf;

		my( $next ) = keys %{ $h{ $leaf } };

		delete $h{ $leaf };
		delete $h{ $next }{ $leaf };

		next if 1 < keys %{ $h{ $next } };
		next if $c[ $next - 1 ];

		push @leaf, $next;
		}

	$debug and print ":$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;

	$debug and print "[@leaf]";

	if( @leaf != $black ){
		print join ' ', ( 1 ) x @c;
		next;
		}

	while( @leaf ){
		my $leaf = shift @leaf;

		my( $next ) = keys %{ $h{ $leaf } };

		if( $c[ $leaf - 1 ] == 1 ){
			push @leaf, $next;
			}

		$c[ $leaf - 1 ] = 2;

		delete $h{ $leaf };
		delete $h{ $next }{ $leaf };
		}

	$debug and print ":$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;
	$debug and print "[@c]";

	my @bfs = keys %h;

	for( @bfs ){
		$c[ $_ - 1 ] = 3;
		}

	$debug and print "[@c]";

	while( @bfs ){
		my $u = shift @bfs;

		for my $v ( keys %{ $g{ $u } } ){
			if( $c[ $v - 1 ] == 0 ){
				push @bfs, $v;
				}
			}

		$c[ $u - 1 ] = 3;
		}

	$debug and print "[@c]";

	print "@c" =~ y/023/110/r;
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	@_ = map ~~<>, 1 .. $_;

	print +( sort { $b <=> $a } map { eval join ' + ', split } @_ )[ 0 ];
	}

#!usr/bin/perl -w
use strict;

my $n=<STDIN>;chomp($n);
my $sum=0;
for my $i(1..$n){
    my($x,$y)=split/ /,<STDIN>;chomp($y);
    $sum=$x+$y if($sum<=$x+$y);
}
print "$sum\n";

print +( sort { $b <=> $a } map { eval join ' + ', split } grep / /, <> )[ 0 ]


#usr/bin/perl -w
use strict;

my ($l,$r)=split/ /,<STDIN>;chomp($r);
print "YES\n";my $n=$l;
while($n<=$r){
    print "$n ";
    $n++;
    print "$n\n";
    $n++;
}

#!/usr/bin/perl

use warnings;
use strict;
#use bigint;

$\ = $/;

while(<>){
	my( $L, $R ) = split;

	print "YES";

	for( my $i = $L; $i < $R; $i += 2 ){
		print join ' ', $i, $i + 1;
		}
	}


#!/usr/bin/perl
use strict;
use warnings;

# 2013/11/26
# [解析性の解釈]
#   障害等の問題の把握、切り分けなどの解析のしやすさ
# 気を付けていること：
# - if 文や関数引数内で計算しない (行ブレークかけた時に計算結果がわかりにくい)
# - 各行はシンプルに (上と同じ理由)
# - perlの場合は use strict と use warnings を付ける (表記の曖昧さを排除するため)
# - 1関数あたり50行程度 (印刷したときに1頁に収まる)

# TICKET1 ... 1回乗車券：$c[0] burles
# TICKET2 ... 特定バス/路面電車指定乗車券：$c[1] burles
# TICKET3 ... バスあるいは路面電車限定乗車券：$c[2] burles
# TICKET4 ... 全てのバス/路面電車乗り放題乗車券：$c[3] burles

use constant ERR_OUTOFRANGE=>-1;
use constant NO_ERR=>0;
use constant TICKET1=>0;
use constant TICKET2=>1;
use constant TICKET3=>2;
use constant TICKET4=>3;

my $ret = NO_ERR;
$ret = main();
exit $ret;

# メイン処理
sub main {
    # 変数初期化
    my $min_cost = 0;
    my $min_cost_bus = 0;
    my $min_cost_trolley = 0;
    my $cost_bus = 0;
    my $cost_trolley = 0;

    # 標準入力からデータの読み込み
    my $line1 = <>;
    my $line2 = <>;
    my $line3 = <>;
    my $line4 = <>;

    # 入力データの整形
    my (@c) = split(/ /, $line1);
    my ($n, $m) = split(/ /, $line2);
    my @a = sort {$b <=> $a} split(/ /, $line3); # 数字が大きい順にソート
    my @b = sort {$b <=> $a} split(/ /, $line4); # 数字が大きい順にソート

    # 入力データのエラーチェック
    foreach(@c) {
        if ($_ < 1 || $_ > 1000) { return ERR_OUTOFRANGE; }
    }
    if ($n < 1 || $n > 1000) { return ERR_OUTOFRANGE; }
    if ($m < 1 || $m > 1000) { return ERR_OUTOFRANGE; }

    # 全てTICKET1(一回乗車券)を使った場合
    $cost_bus += $_ * $c[TICKET1] for @a;
    $cost_trolley += $_ * $c[TICKET1] for @b;
    $min_cost_bus = $cost_bus;
    $min_cost_trolley = $cost_trolley;

    # 乗った回数の多い順からTICKET2(特定バス/路面電車指定乗車券)を使った場合
    # - バス
    foreach(@a) {
        if ($_ < 0 || $_ > 1000) { return ERR_OUTOFRANGE; }
        $cost_bus = $cost_bus - $_ * $c[TICKET1] + $c[TICKET2];
        # TICKET2を使っても安くならない場合は次へ
        if ($cost_bus > $min_cost_bus) { last; }
        $min_cost_bus = $cost_bus;
    }
    # - 路面電車
    foreach(@b) {
        if ($_ < 0 || $_ > 1000) { return ERR_OUTOFRANGE; }
        $cost_trolley = $cost_trolley - $_ * $c[TICKET1] + $c[TICKET2];
        # TICKET2を使っても安くならない場合は次へ
        if ($cost_trolley > $min_cost_trolley) { last; }
        $min_cost_trolley = $cost_trolley;
    }

    # TICKET3(バスあるいは路面電車限定乗車券)を使った場合
    if ($min_cost_bus > $c[TICKET3]) { $min_cost_bus = $c[TICKET3]; }
    if ($min_cost_trolley > $c[TICKET3]) { $min_cost_trolley = $c[TICKET3]; }

    # TICKET4(全てのバス/路面電車乗り放題乗車券)を使った場合
    $min_cost = $min_cost_bus + $min_cost_trolley;
    if ($min_cost > $c[TICKET4]) { $min_cost = $c[TICKET4]; }

    chomp($min_cost);
    print "$min_cost\n";

    return NO_ERR;
}

#!/usr/bin/perl

#今回はログを埋め込んだのみ。
#起動時に-debugをオプションでつけるとデバッグログが出力される。

#解析容易性というと難しいけど、
#一般的にはやっぱりデバッグ用のログ埋め込みと
#要所要所でアサーションを挟んでやることですかね。

#デバッガを使うというのであれば
#1行あたりの命令数を少なくしておくと、
#ブレークポイントが設定しやすくなる。

#exceptionが実装されているような言語なら、
#メソッド分割をしっかりしておくとtraceしやすくなるような気がする。

#ちなみに今回はログを自前実装していますが、
#CPANから取ってくればよいログのライブラリもあります。
#assetも標準では使えないのでコメントアウト。

use strict;
use warnings;

my $debug = 0;

if(@ARGV > 0 && $ARGV[0] eq "-debug"){
    $debug = 1;
}

#use Test::Harness::Assert;

#入力処理
my $line = <STDIN>;
chomp $line;
my @costs = split(/\s+/,$line);

$line = <STDIN>;
chomp $line;
my($num_bus, $num_trolley) = split(/\s+/,$line);

$line = <STDIN>;
chomp $line;
my @using_buses = split(/\s+/,$line);

#assert($num_bus == @using_buses);

$line = <STDIN>;
chomp $line;
my @using_trolley = split(/\s+/,$line);

#assert($num_trolley == @using_trolley);

#コスト計算
my $bus_cost = &calc_cost(@using_buses);
&debuglog("sum of buses cost = $bus_cost\n");
my $trolley_cost = &calc_cost(@using_trolley);
&debuglog("sum of trolleys cost = $trolley_cost\n");
my $sum = $bus_cost + $trolley_cost;

#出力処理
if($sum < $costs[3]){
    print $sum,"\n";
}else{
    print $costs[3],"\n";
}

sub calc_cost{
    my @nums = @_;
    my @one_type_costs;
    my $sum = 0;
    foreach my $num (@nums){
        if($num eq ""){
            next;
        }
        my $cost = &calc_onetype_cost($num);
        &debuglog("one type cost = $cost\n");
        $sum += $cost;
    }

    if($sum < $costs[2]){
        return $sum;
    }else{
        return $costs[2];
    }
}

sub calc_onetype_cost{
    my($num) = @_;
    my $cost;
    if($num * $costs[0] > $costs[1]){
        $cost = $costs[1];
    }else{
        $cost = $num * $costs[0];
    }
    return $cost;
}

sub debuglog{
    my $str = $_[0];
    if($debug == 1){
        print STDERR  "$str\n";
    }
}


#!/bin/perl -w
use strict;
use warnings;
use List::Util qw{max};
use integer;

my @s;
my ($i,$l) = (0,0);
while ($s[$i] = <>) {
    chomp $s[$i++];
}
delete $s[$#s];
my $len = max map length, @s;
print '*' x ($len + 2), "\n";
for (@s) {
    print '*';
    my $s = $len - length $_;
    if ($s % 2) {
	print ' ' x ($s / 2 + $l), $_, ' ' x ($s / 2 + 1 - $l);
	$l = 1 - $l;
    } else {
	print ' ' x ($s / 2), $_, ' ' x ($s / 2);
    }
    print "*\n";
}
print '*' x ($len + 2), "\n";

#!/bin/perl -w
use strict;
use warnings;
use List::Util qw{max};
use integer;

my @s;
my ($i,$l) = (0,1);
while ($s[$i] = <>) {
    chomp $s[$i++];
}
delete $s[$#s];
my $len = max map length, @s;
print '*' x ($len + 2), "\n";
for (@s) {
    print '*';
    my $s = $len - length $_;
    if ($s % 2) {
	print ' ' x ($s / 2 + $l), $_, ' ' x ($s / 2 + 1 - $l);
	$l = 1 - $l;
    } else {
	print ' ' x ($s / 2), $_, ' ' x ($s / 2);
    }
    print "*\n";
}
print '*' x ($len + 2), "\n";


#!perl

($n, $mony) = split / /, <>;

$mony1 = $mony - 1;
$flag = 0;
$maxResult = 0;

foreach my $i (1..$n)
{
    ($dolars, $cents) = split / /, <>;

    if(($dolars <= $mony1) or ($dolars == $mony and $cents == 0))
    {
        $result = 100 - $cents;
        $maxResult = $result if $result > $maxResult and $result < 100;

        $flag = 1;
    }
}

$\ = "\n";

if($flag == 1)
{
    print $maxResult;
}
else
{
    print -1;
}