#!/usr/bin/python
from sys import stdin
memory = [ int(x) for x in stdin.read().split() ]
try:
  while True:
    ip = memory[0]
    a, b, c = memory[ip], memory[ip+1], memory[ip+2]
    memory[a] -= memory[b]
    if memory[a] <= 0: memory[0] = c
    else: memory[0] += 3
except:
  print memory[1]
