#!/usr/bin/perl -w

use v5.16;

chomp (my @mem = split /,/, <>);

sub run {
	my ($m1, $m2, @mem) = @_;
	$mem[1] = $m1;
	$mem[2] = $m2;
	my $pc = 0;
	while (1) {
		if ($mem[$pc] == 1) {
			$mem[ $mem[$pc+3] ] = $mem[ $mem[$pc+1] ] + $mem[ $mem[$pc+2] ];
		} elsif ($mem[$pc] == 2) {
			$mem[ $mem[$pc+3] ] = $mem[ $mem[$pc+1] ] * $mem[ $mem[$pc+2] ];
		} elsif ($mem[$pc] == 99) {
			last;
		}
		$pc += 4;
	}
	return $mem[0];
}

for my $n (0..99) {
	for my $v (0..99) {
		say 100*$n + $v if run($n, $v, @mem) == 19690720;
	}
}

say $mem[0];
