Interpreter

Time limit1sMemory limit128 MB

Problem

A small computer has 10 registers and 1000 words of RAM. Each register and each RAM word stores a 3-digit integer from 0 to 999. Instructions are encoded as 3-digit integers and are stored in RAM. Every register starts at 000, and the initial RAM contents are given on standard input. Execution begins with the instruction at RAM address 0. Every arithmetic result is reduced modulo 1000.

The instruction encodings are:

  • 100 — halt.
  • 2dn — set register d to n.
  • 3dn — add n to register d.
  • 4dn — multiply register d by n.
  • 5ds — set register d to the value of register s.
  • 6ds — add the value of register s to register d.
  • 7ds — multiply register d by the value of register s.
  • 8da — set register d to the RAM word whose address is held in register a.
  • 9sa — store the value of register s into the RAM word whose address is held in register a.
  • 0ds — jump to the address held in register d, unless register s holds 0 (in which case continue with the next instruction).

Here d, s, and a are single digits (0-9) naming a register, and n is a single digit (0-9).

Input

The input consists of up to 1000 three-digit unsigned integers, one per line, giving the contents of consecutive RAM words starting at address 0. Any RAM word not listed is initialized to 000.

Output

Print a single integer: the number of instructions executed, counting up to and including the halt instruction. The program is guaranteed to halt, in at most 100000 instructions.