By adding x or subtracting x, we can obtain any number in the same residue class so we only care about aimodx. To maximize the mex, we need to obtain consecutive numbers starting from 0 as many as possible. In the perspective of modular, that means we need 1,2,3,4,…,x,1,2,3,4,…,x,…. Therefore, we should store the size of each residue class and try to increase the answer when we have a new number.