#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cctype>
using namespace std;
int M, N;
int used[100010];
int isPrime(int n){
if(n <= 1) return 0;
int sqr = (int)sqrt(1.0*n);
for(int i = 2; i <= sqr; i++){
if(n%i == 0) return 0;
}
return 1;
}
int prob(int pos){
int add = 1;
int newpos = (pos + add*add)%M;
while(newpos != pos){
if(used[newpos] == 0)
return newpos;
add++;
newpos = (pos + add*add)%M;
}
return -1;
}
int main(){
memset(used, 0, sizeof(used));
scanf("%d %d", &M, &N);
if(!isPrime(M)){
for(int m = M+1; m < 10100; m++){
if(isPrime(m)){
M = m; break;
}
}
}
int temp, count = 0;;
for(int i = 0; i < N; i++){
scanf("%d", &temp);
int pos = temp%M;
if(used[pos] == 1)
pos = prob(pos);
if(pos >= 0){
printf("%d", pos);
used[pos] = 1;
}else putchar('-');
if(i < N-1) putchar(' ');
}
return 0;
}