from functools import partial

def disc(positions, init, dist, t):
	return (init + dist + t) % positions

def falls_through(*discs):
	n = 0
	while not all(f(n) == 0 for f in discs):
		n += 1
	return n

def test():
	assert 4 == disc(5, 4, 0, 0)
	assert 0 == disc(5, 4, 0, 1)
	assert 1 == disc(2, 1, 0, 0)
	assert 0 == disc(2, 1, 0, 1)

	one = partial(disc, 5, 4, 1)
	two = partial(disc, 2, 1, 2)

	assert 5 == falls_through(one, two)

def main():
	one = partial(disc, 5, 2, 1)
	two = partial(disc, 13, 7, 2)
	three = partial(disc, 17, 10, 3)
	four = partial(disc, 3, 2, 4)
	five = partial(disc, 19, 9, 5)
	six = partial(disc, 7, 0, 6)
	seven = partial(disc, 11, 0, 7)

	print (falls_through(one, two, three, four, five, six))
	print (falls_through(one, two, three, four, five, six, seven))

test()
main()