Consider the sequence a1,a2,a3,a4... such that a1=2 and for every positive integer n, an+1=an+pn, where pn is the largest prime factor of an. The first few terms of the sequence are 2,4,6,9,12,15,20. What is the largest value of n such that an is a four-digit number?