Since {a1,a2,…,an}={1,2,…,n} we conclude that ai−aj : n only if i=j. From the problem conditions it follows that ak+1=ak2+εk−nbk where bk∈Z and εk=±1. We have ak+1−al+1=(ak−al)(ak+al)+(εk−εl)−n(bk−bl). It follows that if ak+al=n then εk=εl otherwise ak+1−al+1⋮n - contradiction. The condition εk=εl means that εk=−εl. Further, one of the ai equals n. Let, say, am=n. Then the set {a1,a2,…,an}\{am} can be divided into 2n−1 pairs (ak,al) such that ak+al=n. For any such pairs of indices k,l we have εk+εl=0. Now add all the equalities for k=1,2,…,n. Then ∑k=2n+1ak=∑k=1nak2−n∑k=1nbk+εm, or 1+2+…+n=12+22+…+n2−n∑k=1nbk+εm whence nk=1∑nbk=6n(n+1)(2n+1)−2n(n+1)+εm=3n(n+1)(n−1)+εm Note that if n is not divisible by 3 then the number 3n(n+1)(n−1) is divisible by n (since 3(n+1)(n−1) is integer). It follows that εm⋮n which is impossible. Hence n is divisible by 3 and it follows that εm is divisible by the number 3n. The latter is possible only for n=3 because εm=±1. It remains to verify that n=3 satisfies the problem conditions. Indeed, let a1=1,a2=2,a3=3. Then a12−a2+1=0⋮3,a22−a3−1=0⋮3 and a32−a1+1=9⋮3.