#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=100010;

int n,k,q2;
struct Point{
	int x,y;
	Point(){}
	Point(int _x,int _y):x(_x),y(_y){}
	Point operator - (const Point &b)const{
        return Point(x-b.x,y-b.y);
    }
    bool operator <(const Point &b)const{
    	return x==b.x?y<b.y:x<b.x;
	}
}p1,p2;
//struct Point
//{
//    int x,y;
//    Point(int _x = 0,int _y = 0)
//    {
//        x = _x,y = _y;
//    }
//    Point operator - (const Point &b)const
//    {
//        return Point(x-b.x,y-b.y);
//    }
//    int operator ^(const Point &b)const
//    {
//        return x*b.y - y*b.x;
//    }
//    void input()
//    {
//        scanf("%d%d",&x,&y);
//    }
//};

typedef Point Vector;
typedef Point Query;
vector<Vector> ve,tmp;
vector<Query> query;
map<Vector,int> mps;
int l[maxn],r[maxn];
int ans[maxn];
vector<int> q[maxn];
int sum[maxn];
int gcd(int x,int y)
{
	return x?gcd(y%x,x):y;
}
int lowbit(int k)
{
	return k&(-k);
}
void Add(int pos, int va) {
    while(pos < maxn) {
        sum[pos] += va;
        pos += lowbit(pos);
    }
}

int Sum(int pos) {
    if(pos <= 0) return 0;
    int Sum = 0;
    while(pos > 0) {
        Sum += sum[pos];
        pos -= lowbit(pos);
    }
    return Sum;
}

//void Add(int pos,int val)
//{
//	while(pos<maxn){
//		sum[pos]+=val;
//		pos+=lowbit(pos);
//	}
//}
//int Sum(int pos)
//{
//	int res=0;
//	while(pos){
//		res+=sum[pos];pos-=lowbit(pos);
//	}
//	return res;
// }
int main()
{
	while(~scanf("%d",&n)){
		mps.clear();
		ve.clear();
		memset(sum,0,sizeof(sum));
		ve.push_back(Vector(-1,-1));
		int a,b,len;
		for(int i=1;i<=n;i++){
			scanf("%d",&k);
			tmp.clear();
			for(int j=0;j<k;j++){
				scanf("%d%d",&a,&b);
				tmp.push_back(Point(a,b));
			}
			len=tmp.size();
			l[i]=ve.size();
			for(int i=0;i<len;i++){
				p1=tmp[i];p2=tmp[(i+1)%len];
				p2=p2-p1;
				a=gcd(abs(p2.x),abs(p2.y));
//				a=__gcd(abs(p2.x),abs(p2.y));
				p2.x/=a;p2.y/=a;
				ve.push_back(p2);
			}
			r[i]=ve.size()-1;
		}
//		for(int i=1;i<=n;i++) printf("id:%d l:%d r:%d\n",i,l[i],r[i]);
		scanf("%d",&q2);
		for(int i=0;i<q2;i++){
			scanf("%d%d",&a,&b);
			Query tmp=Query(l[a],r[b]);
//			printf("")
			query.push_back(tmp);
			q[tmp.y].push_back(i);
		}
//		for(int i=1;i<=n;i++)
//			printf("Q[%d].siz:%d \n",i,q[i].size());//
		for(int i=1;i<ve.size();i++){
			if(mps.count(ve[i])){
			//if(mps.find(ve[i])!=mps.end()){
				Add(mps[ve[i]],-1);
			}
			Add(i,1);
			mps[ve[i]]=i;
			for(int j=0;j<q[i].size();j++)
//				printf("r_sum:%d l_sum:%d\n",Sum(query[q[i][j]].y),Sum(query[q[i][j]].x)),
				ans[q[i][j]]=Sum(query[q[i][j]].y)-Sum(query[q[i][j]].x-1);
		}
		for(int i=0;i<q2;i++)
			printf("%d\n",ans[i]);
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=500010;//

int n,k,q2;
struct Point{
	int x,y;
	Point(){}
	Point(int _x,int _y):x(_x),y(_y){}
	Point operator - (const Point &b)const{
        return Point(x-b.x,y-b.y);
    }
    bool operator <(const Point &b)const{
    	return x==b.x?y<b.y:x<b.x;
	}
}p1,p2;

typedef Point Vector;
typedef Point Query;
vector<Vector> ve,tmp;
vector<Query> query;
map<Vector,int> mps;
int l[maxn],r[maxn];
int ans[maxn];
vector<int> q[maxn];
int sum[maxn];
int gcd(int x,int y)
{
	return x?gcd(y%x,x):y;
}
int lowbit(int k)
{
	return k&(-k);
}
//void Add(int pos, int va) {
//    while(pos < maxn) {
//        sum[pos] += va;
//        pos += lowbit(pos);
//    }
//}
//
//int Sum(int pos) {
//    if(pos <= 0) return 0;
//    int Sum = 0;
//    while(pos > 0) {
//        Sum += sum[pos];
//        pos -= lowbit(pos);
//    }
//    return Sum;
//}

void Add(int pos,int val)
{
	while(pos<maxn){
		sum[pos]+=val;
		pos+=lowbit(pos);
	}
}
int Sum(int pos)
{
	int res=0;
	while(pos){
		res+=sum[pos];pos-=lowbit(pos);
	}
	return res;
 }
int main()
{
	while(~scanf("%d",&n)){
		mps.clear();
		ve.clear();
		memset(sum,0,sizeof(sum));
		ve.push_back(Vector(-1,-1));
		int a,b,len;
		for(int i=1;i<=n;i++){
			scanf("%d",&k);
			tmp.clear();
			for(int j=0;j<k;j++){
				scanf("%d%d",&a,&b);
				tmp.push_back(Point(a,b));
			}
			len=tmp.size();
			l[i]=ve.size();
			for(int i=0;i<len;i++){
				p1=tmp[i];p2=tmp[(i+1)%len];
				p2=p2-p1;
				a=gcd(abs(p2.x),abs(p2.y));
//				a=__gcd(abs(p2.x),abs(p2.y));
				p2.x/=a;p2.y/=a;
				ve.push_back(p2);
			}
			r[i]=ve.size()-1;
		}
//		for(int i=1;i<=n;i++) printf("id:%d l:%d r:%d\n",i,l[i],r[i]);
		scanf("%d",&q2);
		for(int i=0;i<q2;i++){
			scanf("%d%d",&a,&b);
			Query tmp=Query(l[a],r[b]);
//			printf("")
			query.push_back(tmp);
			q[tmp.y].push_back(i);
		}
//		for(int i=1;i<=n;i++)
//			printf("Q[%d].siz:%d \n",i,q[i].size());//
		for(int i=1;i<ve.size();i++){
			if(mps.count(ve[i])){
			//if(mps.find(ve[i])!=mps.end()){
				Add(mps[ve[i]],-1);
			}
			Add(i,1);
			mps[ve[i]]=i;
			for(int j=0;j<q[i].size();j++)
//				printf("r_sum:%d l_sum:%d\n",Sum(query[q[i][j]].y),Sum(query[q[i][j]].x)),
				ans[q[i][j]]=Sum(query[q[i][j]].y)-Sum(query[q[i][j]].x-1);
		}
		for(int i=0;i<q2;i++)
			printf("%d\n",ans[i]);
	}
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define PB push_back
#define PII pair<int, int>
const int N = 3e5+10;

int n,q,sz,cnt=0,l,r,x[N],y[N],tr[N],last[N],ans[N];
map<PII, int> id;
vector<int> a[N];
vector<PII> b[N];

void add(int x,int val){
	for(;x<N;x+=x&-x)tr[x]+=val;
}
int sum(int x){
	int res = 0;
	for(;x;x-=x&-x)res+=tr[x];
	return res;
}

int main(){
	scanf("%d",&n);
	rep(i,1,n){
		scanf("%d",&sz);
		rep(j,1,sz)scanf("%d%d",x+j,y+j);
		rep(j,1,sz){
			int dx=x[j%sz+1]-x[j],dy=y[j%sz+1]-y[j];
			int tmp = abs(__gcd(dx, dy));
			dx /= tmp, dy /= tmp;
			if(!id[{dx,dy}])id[{dx,dy}]=++cnt;
			a[i].PB(id[{dx,dy}]);
		}
	}
	scanf("%d",&q);
	rep(i,1,q)scanf("%d%d",&l,&r),b[r].PB({i,l});
	rep(i,1,n){
		for(int id:a[i]){
			if(last[id])add(last[id],-1);
			add(i,1);
			last[id] = i;
		}
		for(auto x:b[i])
			ans[x.first] = sum(i) - sum(x.second-1);
	}
	rep(i,1,q)printf("%d\n",ans[i]);
	return 0;
}


//This Code was made by Chinese_zjc_.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <map>
#include <set>
#include <ctime>
#include <list>
// #include<windows.h>
#define int long long
#define double long double
using namespace std;
const double PI = acos(-1);
const double eps = 0.0000000001;
const int INF = 0x3fffffffffffffff;
int n, x, y, cnt, tmp, id, lst[300005], q, sum[300005], ans[100005];
struct frac
{
    int a, b;
    frac(int A, int B)
    {
        int gcd = llabs(__gcd(A, B));
        a = A / gcd;
        b = B / gcd;
    }
    bool operator<(const frac &__Val) const
    {
        return a == __Val.a ? b < __Val.b : a < __Val.a;
    }
};
struct point
{
    int x, y;
};
struct L
{
    int l, r;
    vector<point> p;
} p[100005];
frac get(point A, point B)
{
    return {A.y - B.y, A.x - B.x};
}
map<frac, int> appear;
struct query
{
    int l, r, p;
    bool operator<(const query &__Val) const
    {
        return r == __Val.r ? l < __Val.l : r < __Val.r;
    }
} b[100005];
int lowbit(int now)
{
    return now & -now;
}
void add(int pos, int val)
{
    if (!pos)
    {
        return;
    }
    while (pos <= cnt)
    {
        sum[pos] += val;
        pos += lowbit(pos);
    }
}
int query(int now)
{
    int res = 0;
    while (now)
    {
        res += sum[now];
        now ^= lowbit(now);
    }
    return res;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cin >> tmp;
        for (int j = 0; j < tmp; ++j)
        {
            cin >> x >> y;
            p[i].p.push_back({x, y});
        }
        p[i].l = cnt + 1;
        p[i].r = (cnt += tmp);
        for (int j = 0; j < tmp; ++j)
        {
            frac nxt = {p[i].p[j].x - p[i].p[(j + 1) % tmp].x, p[i].p[j].y - p[i].p[(j + 1) % tmp].y};
            lst[p[i].l + j] = appear[nxt];
            appear[nxt] = p[i].l + j;
        }
    }
    cin >> q;
    for (int i = 1; i <= q; ++i)
    {
        cin >> b[i].l >> b[i].r;
        b[i].l = p[b[i].l].l;
        b[i].r = p[b[i].r].r;
        b[i].p = i;
    }
    sort(b + 1, b + 1 + q);
    for (int i = 1, tmp = 0; i <= q; ++i)
    {
        while (tmp <= b[i].r)
        {
            add(tmp, 1);
            add(lst[tmp], -1);
            ++tmp;
        }
        ans[b[i].p] = query(b[i].r) - query(b[i].l - 1);
    }
    for (int i = 1; i <= q; ++i)
    {
        cout << ans[i] << endl;
    }
    return 0;
}


/**
 *　　　　　　  　　┏┓　　 　┏┓
 * 　　　　　  　　┏┛┗━━━━━━━┛┗━━━┓
 * 　　　　　  　　┃　　　　　　　┃
 * 　　　　　  　　┃　　　━　　 　┃
 * 　　　　　  　　┃　＞　　　＜　┃
 * 　　　　　  　　┃　　　　　　　┃
 * 　　　　　  　　┃...　⌒　... 　┃
 * 　　　　　　  　┃              ┃
 * 　　　　　　  　┗━┓          ┏━┛
 * 　　　　　　　　　┃          ┃　Code is far away from bug with the animal protecting
 * 　　　　　　　　　┃          ┃   神兽保佑,代码无bug
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┗━━━┓
 * 　　　　　　　　　┃              ┣┓
 * 　　　　　　　　　┃              ┏┛
 * 　　　　　　　　　┗┓┓┏━━━━━━━━┳┓┏┛
 * 　　　　　　　　　　┃┫┫       ┃┫┫
 * 　　　　　　　　　　┗┻┛       ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<set>
#include<bitset>
#include<complex>
#include<cstdlib>
#include<assert.h>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define mid (x+y>>1)
#define sqr(x) ((x)*(x))
#define NM 300005
#define nm 200005
const double pi=acos(-1);
const ll inf=998244353;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}

struct P{
    int x,y,v;
    bool operator<(const P&o)const{return y<o.y||(y==o.y&&x<o.x);}
    bool operator==(const P&o)const{return x==o.x&&y==o.y;}
}b[NM],c[NM];
int n,m,f[NM],p[NM],tr[NM],ans[NM],v[NM],a[NM],tot;

void add(int x,int t){for(;x<=n;x+=lowbit(x))tr[x]+=t;}
int sum(int x,int s=0){for(;x;x-=lowbit(x))s+=tr[x];return s;}

int main(){
    n=read();
    inc(i,1,n){
	m=read();f[i]=f[i-1]+m;
	inc(i,1,m)b[i].x=read(),b[i].y=read();
	b[m+1]=b[1];
	inc(i,1,m){
	    c[++tot].y=b[i+1].y-b[i].y;
	    c[tot].x=b[i+1].x-b[i].x;
	    int t=__gcd(abs(c[tot].x),abs(c[tot].y));
	    if(t)c[tot]=P{-c[tot].y/t,c[tot].x/t};else c[tot].x=c[tot].y=0;
	}
    }
    n=f[n];
    inc(i,1,tot)b[i]=c[i];
    sort(b+1,b+1+tot);tot=unique(b+1,b+1+tot)-b-1;
    inc(i,1,n)a[i]=lower_bound(b+1,b+1+tot,c[i])-b;
    inc(i,1,n)p[i]=v[a[i]],v[a[i]]=i;
    m=read();inc(i,1,m)b[i].x=f[read()-1],b[i].y=f[read()],b[i].v=i;
    sort(b+1,b+1+m);
    inc(i,1,m){
	inc(j,b[i-1].y+1,b[i].y){
	    if(p[j])add(p[j],-1);
	    add(j,1);
	}
	ans[b[i].v]=sum(b[i].y)-sum(b[i].x);
    }
    inc(i,1,m)printf("%d\n",ans[i]);
    return 0;
}


/**
 *　　　　　　  　　┏┓　　 　┏┓
 * 　　　　　  　　┏┛┗━━━━━━━┛┗━━━┓
 * 　　　　　  　　┃　　　　　　　┃
 * 　　　　　  　　┃　　　━　　 　┃
 * 　　　　　  　　┃　＞　　　＜　┃
 * 　　　　　  　　┃　　　　　　　┃
 * 　　　　　  　　┃...　⌒　... 　┃
 * 　　　　　　  　┃              ┃
 * 　　　　　　  　┗━┓          ┏━┛
 * 　　　　　　　　　┃          ┃　Code is far away from bug with the animal protecting
 * 　　　　　　　　　┃          ┃   神兽保佑,代码无bug
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┗━━━┓
 * 　　　　　　　　　┃              ┣┓
 * 　　　　　　　　　┃              ┏┛
 * 　　　　　　　　　┗┓┓┏━━━━━━━━┳┓┏┛
 * 　　　　　　　　　　┃┫┫       ┃┫┫
 * 　　　　　　　　　　┗┻┛       ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<set>
#include<bitset>
#include<complex>
#include<cstdlib>
#include<assert.h>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define mid (x+y>>1)
#define sqr(x) ((x)*(x))
#define NM 300005
#define nm 200005
const double pi=acos(-1);
const ll inf=998244353;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}

struct P{
    int x,y,v;
    bool operator<(const P&o)const{return y<o.y||(y==o.y&&x<o.x);}
    bool operator==(const P&o)const{return x==o.x&&y==o.y;}
}b[NM],c[NM];
int n,m,f[NM],p[NM],tr[NM],ans[NM],v[NM],a[NM],tot;

void add(int x,int t){for(;x<=n;x+=lowbit(x))tr[x]+=t;}
int sum(int x,int s=0){for(;x;x-=lowbit(x))s+=tr[x];return s;}

int main(){
    n=read();
    inc(i,1,n){
	m=read();f[i]=f[i-1]+m;
	inc(i,1,m)b[i].x=read(),b[i].y=read();
	b[m+1]=b[1];
	inc(i,1,m){
	    c[++tot].y=b[i+1].y-b[i].y;
	    c[tot].x=b[i+1].x-b[i].x;
	    int t=__gcd(abs(c[tot].x),abs(c[tot].y));
	    if(t)c[i]=P{-c[tot].y/t,c[tot].x/t};else c[tot].x=c[tot].y=0;
	}
    }
    n=f[n];
    inc(i,1,tot)b[i]=c[i];
    sort(b+1,b+1+tot);m=unique(b+1,b+1+tot)-b-1;
    inc(i,1,tot)a[i]=lower_bound(b+1,b+1+tot,c[i])-b;
    inc(i,1,tot)p[i]=v[a[i]],v[a[i]]=i;
    m=read();inc(i,1,m)b[i].x=f[read()-1]+1,b[i].y=f[read()],b[i].v=i;
    sort(b+1,b+1+m);
    inc(i,1,m){
	inc(j,b[i-1].y+1,b[i].y){
	    if(p[j])add(p[j],-1);
	    add(j,1);
	}
	ans[b[i].v]=sum(b[i].y)-sum(b[i].x-1);
    }
    inc(i,1,m)printf("%d\n",ans[i]);
    return 0;
}


#include <bits/stdc++.h>
const int N=1e6+5;
int n,k,q,id,num,len,now,a[N],lb[N],rb[N],ans[N],pos[N],cnt[N];
std::vector<int> px,py;
std::map<std::pair<int, int>,int> map;
struct Query{
	int i,l,r;
}que[N];
bool operator <(const Query &u, const Query &v){return pos[u.l]==pos[v.l]?u.r<v.r:u.l<v.l;}
int getid(int x, int y){
	int gcd=std::abs(std::__gcd(x, y));
	x/=gcd;y/=gcd;
	if (map.count({x, y})) return map[ {x, y}];
	return map[{x,y}]=id++;
}
void insert(int x){
	cnt[x]++;
	if (cnt[x]==1) now++;
}
void erase(int x){
	cnt[x]--;
	if (cnt[x]==0) now--;
}
int main() {
	std::cin>>n;
	for (int i=0;i<n;i++) {
		std::cin>>k;
		px.clear(); py.clear();
		for (int j=0;j<k;j++) {
			int k1,k2;
			std::cin>>k1>>k2;
			px.push_back(k1);
			py.push_back(k2);
		}
		lb[i]=num;
		for (int j=0;j<k;j++) a[num++]=getid(px[(j+1)%k]-px[j],py[(j+1)%k]-py[j]);
		rb[i]=num-1;
	}
	len=sqrt(num);
	for (int i=0;i<num;i++) pos[i]=i/len;
	std::cin>>q;
	for (int i=0;i<q;i++) {
		que[i].i=i;
		int k1,k2;
		std::cin>>k1>>k2;
		que[i].l=lb[k1-1];
		que[i].r=rb[k2-1];
	}
	std::sort(que,que+q);
	for (int i=0,l=0,r=-1;i<q;i++) {
		while (r<que[i].r) insert(a[++r]);
		while (r>que[i].r) erase(a[r--]);
		while (l>que[i].l) insert(a[--l]);
		while (l<que[i].l) erase(a[l++]);
		ans[que[i].i]=now;
	}
	for (int i=0;i<q;i++)  printf("%d\n",ans[i]);
	return 0;
}


#include <bits/stdc++.h>
const int N=1e6+5;
int n,k,q,id,num,len,now,a[N],lb[N],rb[N],ans[N],pos[N],cnt[N];
std::vector<int> px,py;
std::map<std::pair<int, int>,int> map;
struct Query{
	int i,l,r;
}que[N];
bool operator <(const Query &u, const Query &v){return pos[u.l]==pos[v.l]?u.r<v.r:u.l<v.l;}
int getid(int x, int y){
	int gcd=std::abs(std::__gcd(x, y));
	x/=gcd;y/=gcd;
	if (map.count({x, y})) return map[ {x, y}];
	return map[{x,y}]=id++;
}
void insert(int x){
	cnt[x]++;
	if (cnt[x]==1) now++;
}
void erase(int x){
	cnt[x]--;
	if (cnt[x]==0) now--;
}
int main() {
	std::ios::sync_with_stdio(0);
	std::cin>>n;
	for (int i=0;i<n;i++) {
		std::cin>>k;
		px.clear(); py.clear();
		for (int j=0;j<k;j++) {
			int k1,k2;
			std::cin>>k1>>k2;
			px.push_back(k1);
			py.push_back(k2);
		}
		lb[i]=num;
		for (int j=0;j<k;j++) a[num++]=getid(px[(j+1)%k]-px[j],py[(j+1)%k]-py[j]);
		rb[i]=num-1;
	}
	len=sqrt(num);
	for (int i=0;i<num;i++) pos[i]=i/len;
	std::cin>>q;
	for (int i=0;i<q;i++) {
		que[i].i=i;
		int k1,k2;
		std::cin>>k1>>k2;
		que[i].l=lb[k1-1];
		que[i].r=rb[k2-1];
	}
	std::sort(que,que+q);
	for (int i=0,l=0,r=-1;i<q;i++) {
		while (r<que[i].r) insert(a[++r]);
		while (r>que[i].r) erase(a[r--]);
		while (l>que[i].l) insert(a[--l]);
		while (l<que[i].l) erase(a[l++]);
		ans[que[i].i]=now;
	}
	for (int i=0;i<q;i++)  printf("%d\n",ans[i]);
	return 0;
}


#include <bits/stdc++.h>
#define inc(i,a,b) for(register int i=a;i<=b;i++)
using namespace std;
int n,tot,l[1000010],r[1000010],cnt,col[1000010],m;
class point{
	public:
	int x,y;
}k[700010];
int gcd(int a,int b){
	if(!b) return a;
	return gcd(b,a%b);
}
map<pair<int,int>,int> mmap;
class node2{
	public:
	int id,l,r;
}query[1000010];
int pre[1000010],last[1000010];
bool cmp(node2 x,node2 y){
	if(x.r==y.r){
		return x.l<y.l;
	}
	return x.r<y.r;
}
long long c[1000010];
int lowbit(int x){return x&(-x);}
void add(int u,int v){
	if(!u) return;
	while(u<=tot){
		c[u]+=v;
		u+=lowbit(u);
	}
}
long long ask(int x){
	long long res=0;
	while(x){
		res+=c[x];
		x-=lowbit(x);
	}
	return res;
}
long long ans[1000010];
int main(){
	cin>>n;
	inc(i,1,n){
		int x; scanf("%d",&x);
		l[i]=tot+1; r[i]=l[i]+x-1;
		inc(j,1,x) scanf("%d%d",&k[j].x,&k[j].y);
		inc(j,1,x){
			int goal=j-1; if(goal==0) goal=x;
			pair<int,int> tmp=make_pair(k[j].y-k[goal].y,k[j].x-k[goal].x);
			int d=gcd(abs(tmp.first),abs(tmp.second));
			tmp.first/=d; tmp.second/=d;
			if(!mmap.count(tmp)) mmap[tmp]=++cnt;
			col[++tot]=mmap[tmp];
		}
	}
	cin>>m;
	inc(i,1,m){
		scanf("%d%d",&query[i].l,&query[i].r);
		query[i].l=l[query[i].l];
		query[i].r=r[query[i].r];
		query[i].id=i;
	}
	inc(i,1,tot) pre[i]=last[col[i]],last[col[i]]=i;
	sort(query+1,query+1+m,cmp);
	int j=1;
	inc(i,1,m){
		while(j<=query[i].r) add(j,1),add(pre[j],-1),j++;
		ans[query[i].id]=ask(query[i].r)-ask(query[i].l-1);
	}
	inc(i,1,m){
		printf("%lld\n",ans[i]);
	}
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<fstream>
#include<cmath>
using namespace std;
#define dzx(x) ((x%mod+mod)%mod)
inline int read(){
	char ch=getchar();
	int res=0,f=1;
	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
	for(;ch>='0'&&ch<='9';ch=getchar())res=(res<<3)+(res<<1)+(ch-'0');
	return res*f;
}
pair<int,int> h[600005];
struct LINE{
	int alfx,alfy;
	int bg,id,ne;
}line[600000];
struct QUERY{
	int l,r,id;
}bet[100005];
bool cmp(QUERY x,QUERY y){return x.l<y.l||(x.l==y.l&&x.r<y.r);}
int n,k,tot,toth,last[600000],ans[100005];
int sum[100005];
bool vis[600005];
//inline int gcd(int x,int y){while(y)x^=y^=x^=y^=x%=y;return x;}
int gcd(int x,int y){return y==0?x:gcd(y,x%y);}
inline int M_abs(int x){return x>0?x:-x;}
inline int lowbit(int x){return (x)&(-x);}
inline void add(int x,int y){for(;x<=n;x+=lowbit(x))sum[x]+=y;}
int query(int x){int res=0;for(;x;x-=lowbit(x))res+=sum[x];return res;}
int main(){
	n=read();
	for(int i=1,xs,ys,lastx,lasty;i<=n;++i){
		k=read();
		for(int j=1,x,y;j<=k;++j){
			x=read(),y=read();
			if(j>1){
				int d=M_abs(gcd(x-lastx,y-lasty));
				h[++toth].first=line[++tot].alfx=(x-lastx)/d;
				h[toth].second=line[tot].alfy=(y-lasty)/d;
				line[tot].bg=i;
			}
			else xs=x,ys=y;
			lastx=x,lasty=y;
		}
		int d=M_abs(gcd(ys-lasty,xs-lastx));
		h[++toth].first=line[++tot].alfx=(xs-lastx)/d;
		h[toth].second=line[tot].alfy=(ys-lasty)/d;
		line[tot].bg=i;
	}
	//h-toth line-tot bg-n
	sort(h+1,h+toth+1);
	toth=unique(h+1,h+toth+1)-h;
	for(int i=1;i<=tot;++i){
		line[i].id=lower_bound(h+1,h+toth+1,make_pair(line[i].alfx,line[i].alfy))-h;
		if(!vis[line[i].id])vis[line[i].id]=1,add(line[i].bg,1);
	}
	int numq=read();
	for(int i=1;i<=numq;++i)bet[i].l=read(),bet[i].r=read(),bet[i].id=i;
	sort(bet+1,bet+1+numq,cmp);
	for(int i=tot;i>=1;--i){
		if(!last[line[i].id])line[i].ne=n+1;
		else line[i].ne=line[last[line[i].id]].bg;
		last[line[i].id]=i;
	}
	for(int i=1,pos=1;i<=numq;++i){
		for(;line[pos].bg<bet[i].l&&pos<=tot;++pos)add(line[pos].ne,1);
		ans[bet[i].id]=query(bet[i].r)-query(bet[i].l-1);
	}
	for(int i=1;i<=numq;++i)printf("%d\n",ans[i]);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<fstream>
#include<cmath>
using namespace std;
#define dzx(x) ((x%mod+mod)%mod)
inline int read(){
	char ch=getchar();
	int res=0,f=1;
	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
	for(;ch>='0'&&ch<='9';ch=getchar())res=(res<<3)+(res<<1)+(ch-'0');
	return res*f;
}
pair<int,int> h[600005];
struct LINE{
	int alfx,alfy;
	int bg,id,ne;
}line[600000];
struct QUERY{
	int l,r,id;
}bet[100005];
bool cmp(QUERY x,QUERY y){return x.l<y.l||(x.l==y.l&&x.r<y.r);}
int n,k,tot,toth,last[600000],ans[100005];
int sum[100005];
bool vis[600005];
inline int gcd(int x,int y){while(y)y^=x^=y^=x%=y;return x;}
//int gcd(int x,int y){return y==0?x:gcd(y,x%y);}
inline int M_abs(int x){return x>0?x:-x;}
inline int lowbit(int x){return (x)&(-x);}
inline void add(int x,int y){for(;x<=n;x+=lowbit(x))sum[x]+=y;}
int query(int x){int res=0;for(;x;x-=lowbit(x))res+=sum[x];return res;}
int main(){
	n=read();
	for(int i=1,xs,ys,lastx,lasty;i<=n;++i){
		k=read();
		for(int j=1,x,y;j<=k;++j){
			x=read(),y=read();
			if(j>1){
				int d=M_abs(gcd(x-lastx,y-lasty));
				h[++toth].first=line[++tot].alfx=(x-lastx)/d;
				h[toth].second=line[tot].alfy=(y-lasty)/d;
				line[tot].bg=i;
			}
			else xs=x,ys=y;
			lastx=x,lasty=y;
		}
		int d=M_abs(gcd(ys-lasty,xs-lastx));
		h[++toth].first=line[++tot].alfx=(xs-lastx)/d;
		h[toth].second=line[tot].alfy=(ys-lasty)/d;
		line[tot].bg=i;
	}
	//h-toth line-tot bg-n
	sort(h+1,h+toth+1);
	toth=unique(h+1,h+toth+1)-h;
	for(int i=1;i<=tot;++i){
		line[i].id=lower_bound(h+1,h+toth+1,make_pair(line[i].alfx,line[i].alfy))-h;
		if(!vis[line[i].id])vis[line[i].id]=1,add(line[i].bg,1);
	}
	int numq=read();
	for(int i=1;i<=numq;++i)bet[i].l=read(),bet[i].r=read(),bet[i].id=i;
	sort(bet+1,bet+1+numq,cmp);
	for(int i=tot;i>=1;--i){
		if(!last[line[i].id])line[i].ne=n+1;
		else line[i].ne=line[last[line[i].id]].bg;
		last[line[i].id]=i;
	}
	for(int i=1,pos=1;i<=numq;++i){
		for(;line[pos].bg<bet[i].l&&pos<=tot;++pos)add(line[pos].ne,1);
		ans[bet[i].id]=query(bet[i].r)-query(bet[i].l-1);
	}
	for(int i=1;i<=numq;++i)printf("%d\n",ans[i]);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<fstream>
#include<cmath>
using namespace std;
#define dzx(x) ((x%mod+mod)%mod)
inline int read(){
	char ch=getchar();
	int res=0,f=1;
	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
	for(;ch>='0'&&ch<='9';ch=getchar())res=(res<<3)+(res<<1)+(ch-'0');
	return res*f;
}
pair<int,int> h[600005];
struct LINE{
	int alfx,alfy;
	int bg,id,ne;
}line[600000];
struct QUERY{
	int l,r,id;
}bet[100005];
bool cmp(QUERY x,QUERY y){return x.l<y.l;}
int n,k,tot,toth,last[600000],ans[100005];
int sum[100005];
bool vis[600005];
inline int gcd(int x,int y){while(y)x^=y^=x^=y^=x%=y;return x;}
inline int M_abs(int x){return x>0?x:-x;}
inline int lowbit(int x){return (x)&(-x);}
inline void add(int x,int y){for(;x<=n;x+=lowbit(x))sum[x]+=y;}
int query(int x){
	int res=0;
	for(;x;x-=lowbit(x))res+=sum[x];
	return res;
}
int main(){
	n=read();
	for(int i=1,xs,ys,lastx,lasty;i<=n;++i){
		k=read();
		for(int j=1,x,y;j<=k;++j){
			x=read(),y=read();
			if(j>1){
				int d=M_abs(gcd(x-lastx,y-lasty));
				h[++toth].first=line[++tot].alfx=(x-lastx)/d;
				h[toth].second=line[tot].alfy=(y-lasty)/d;
				line[tot].bg=i;
				//cout<<line[tot].alfx<<" xxxx"<<line[tot].alfy<<endl;
			}
			else xs=x,ys=y;
			lastx=x,lasty=y;
		}
		int d=M_abs(gcd(ys-lasty,xs-lastx));
		h[++toth].first=line[++tot].alfx=(xs-lastx)/d;
		h[toth].second=line[tot].alfy=(ys-lasty)/d;
		//cout<<line[tot].alfx<<" xxxx"<<line[tot].alfy<<endl;
		line[tot].bg=i;
	}
	sort(h+1,h+toth+1);
	toth=unique(h+1,h+toth+1)-h;
	for(int i=1;i<=tot;++i){
		line[i].id=lower_bound(h+1,h+toth+1,make_pair(line[i].alfx,line[i].alfy))-h;
		if(!vis[line[i].id])vis[line[i].id]=1,add(line[i].bg,1);
		//cout<<line[i].alfx<<" "<<line[i].alfy<<" "<<line[i].id<<endl;
	}
	int numq=read();
	for(int i=1;i<=numq;++i)bet[i].l=read(),bet[i].r=read(),bet[i].id=i;
	sort(bet+1,bet+1+numq,cmp);
	for(int i=tot;i>=1;--i){
		if(!last[line[i].id])line[i].ne=tot+1;
		else line[i].ne=last[line[i].id];
		last[line[i].id]=i;
	}
	line[tot+1].bg=n+1;
	int pos=1;
	for(int i=1;i<=numq;++i){
		for(;line[pos].bg<bet[i].l;++pos){
			//cout<<line[pos].alfx<<" "<<line[pos].alfy<<" "<<line[pos].ne<<endl;
			add(line[line[pos].ne].bg,1);
		}
		ans[bet[i].id]=query(bet[i].r)-query(bet[i].l-1);
	}
	for(int i=1;i<=numq;++i)printf("%d\n",ans[i]);
	return 0;
}


#include<bits/stdc++.h>
#define N 300005
using namespace std;
typedef long long LL;
typedef pair<int,int> P;
template <typename T> void read(T &x){
	int f=1;x=0;char c=getchar();
	for (;!isdigit(c);c=getchar()) if (c=='-') f=-f;
	for (; isdigit(c);c=getchar()) x=x*10+c-'0';
	x*=f;
}
int n,m,len,a[N],cnt[N],sum,ans[N],l,r,L[N],R[N];
int x[N],y[N],tot;P t;
map<P,int> M;
struct Query{int l,r,f;}q[N];
bool operator <(const Query &x,const Query &y){
	if (x.l/len!=y.l/len) return x.l<y.l;
	return (x.l/len&1)?x.r<y.r:x.r>y.r;
}
int main(){
	read(n);len=500;
	for (int i=1,k,g;i<=n;++i){
		read(k);L[i]=R[i-1]+1;R[i]=R[i-1]+k;
		for (int j=L[i];j<=R[i];++j) read(x[j]),read(y[j]);
		x[R[i]+1]=x[L[i]];y[R[i]+1]=y[L[i]];
		for (int j=L[i];j<=R[i];++j){
			g=__gcd(abs(x[j+1]-x[j]),abs(y[j+1]-y[j]));
			t=make_pair((x[j+1]-x[j])/g,(y[j+1]-y[j])/g);
			if (M[t]) a[j]=M[t];else M[t]=a[j]=++tot;
		}
	}
	read(m);
	for (int i=1,X,Y;i<=m;++i){
		read(X);read(Y);
		q[i]=(Query){L[X],R[Y],i};
	}
	sort(q+1,q+m+1);l=q[1].l;r=l-1;
	for (int i=1;i<=m;++i){
		while (l>q[i].l) sum+=cnt[a[--l]]++==0;
		while (r<q[i].r) sum+=cnt[a[++r]]++==0;
		while (l<q[i].l) sum-=--cnt[a[l++]]==0;
		while (r>q[i].r) sum-=--cnt[a[r--]]==0;
		ans[q[i].f]=sum;
	}
	for (int i=1;i<=m;++i) printf("%d\n",ans[i]);
	return 0;
}


#include<bits/stdc++.h>
#define it register int
#define ct const int
#define il inline
using namespace std;
#define mkp make_pair
const int N=1000005;
map<pair<int,int> ,int> mp;
vector<pair<int,int> > g[N];
int n,m,a[N],b[N],l[N],r[N],cnt,ans[N],tr[N],lst[N],Q,d;
il void gcd(ct a,ct b){return !b?d=a,void():gcd(b,a%b);}
il void add(it x,ct num){while(x) tr[x]+=num,x-=(x&-x);}
il int que(it x){it ans=0;while(x<=cnt) ans+=tr[x],x+=(x&-x);return ans;}
int main(){
	scanf("%d",&n);it i,j,x,y;
	for(i=1;i<=n;++i){
		scanf("%d",&m),l[i]=cnt+1;
		for(j=1;j<=m;++j) scanf("%d%d",&a[i],&b[i]);
		for(j=1;j<=m;++j) ++cnt,x=a[j%m+1]-a[j],y=b[j%m+1]-b[j],gcd(x,y),d<0?d=-d:0,(!d?d=1:0),x/=d,y/=d,mp.count(mkp(x,y))?lst[cnt]=mp[mkp(x,y)]:0,mp[mkp(x,y)]=cnt;
		r[i]=cnt;
	}
	scanf("%d",&Q);
	for(i=1;i<=Q;++i) scanf("%d%d",&x,&y),g[r[y]].push_back(mkp(l[x],i));
	for(i=1;i<=cnt;++i){
		add(lst[i],-1),add(i,1);
		for(const auto &j:g[i]) ans[j.second]=que(j.first);//printf("%d\n",ans[j.second]);
	}
	for(i=1;i<=Q;++i) printf("%d\n",ans[i]);
	return 0;
}


#include<bits/stdc++.h>
#define it register int
#define ct const int
#define il inline
using namespace std;
#define mkp make_pair
const int N=1000005;
map<pair<int,int> ,int> mp;
vector<pair<int,int> > g[N];
int n,m,a[N],b[N],l[N],r[N],cnt,ans[N],tr[N],lst[N],Q,d;
il void gcd(ct a,ct b){return !b?d=a,void():gcd(b,a%b);}
il void add(it x,ct num){while(x) tr[x]+=num,x-=(x&-x);}
il int que(it x){it ans=0;while(x<=cnt) ans+=tr[x],x+=(x&-x);return ans;}
int main(){
	scanf("%d",&n);it i,j,x,y;
	for(i=1;i<=n;++i){
		scanf("%d",&m),l[i]=cnt+1;
		for(j=1;j<=m;++j) scanf("%d%d",&a[j],&b[j]);
		for(j=1;j<=m;++j) ++cnt,x=a[j%m+1]-a[j],y=b[j%m+1]-b[j],gcd(x,y),d<0?d=-d:0,(!d?d=1:0),x/=d,y/=d,mp.count(mkp(x,y))?lst[cnt]=mp[mkp(x,y)]:0,mp[mkp(x,y)]=cnt;
		r[i]=cnt;
	}
	scanf("%d",&Q);
	for(i=1;i<=Q;++i) scanf("%d%d",&x,&y),g[r[y]].push_back(mkp(l[x],i));
	for(i=1;i<=cnt;++i){
		add(lst[i],-1),add(i,1);
		for(const auto &j:g[i]) ans[j.second]=que(j.first);//printf("%d\n",ans[j.second]);
	}
	for(i=1;i<=Q;++i) printf("%d\n",ans[i]);
	return 0;
}


#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;

const int B = 550;
const int N = 100000;
const int M = 300000;

struct query{
	int l, r, i;
	friend bool operator < (const query &a, const query &b) {
		return (a.l / B == b.l / B) ? (a.r < b.r) : (a.l < b.l);
	}
}qry[N + 5];

int ans[N + 5], cnt[M + 5], nwres;
void add(int x, int d) {
	if( (!cnt[x]) && d == 1 ) nwres++;
	cnt[x] += d;
	if( (!cnt[x]) && d == -1 ) nwres--;
}

struct point{
	ll x, y;

	friend point operator - (const point &a, const point &b) {return (point){a.x - b.x, a.y - b.y};}
	friend ll operator ^ (const point &a, const point &b) {return a.x*b.y - a.y*b.x;}
	friend bool operator < (const point &a, const point &b) {
		if( a.y >= 0 && b.y < 0 ) return false;
		else if( a.y < 0 && b.y >= 0 ) return true;
		else if( a.y == 0 && b.y == 0 ) return a.x > 0 && b.x < 0;
		else return (a ^ b) > 0;
	}
	friend bool operator == (const point &a, const point &b) {return (a ^ b) == 0;}
}p[M + 5], t[M + 5];

int a[M + 5], le[N + 5], ri[N + 5];
int main() {
	int n, q, s = 0; scanf("%d", &n);
	for(int i=1,m;i<=n;i++) {
		scanf("%d", &m), le[i] = s, ri[i] = s + m - 1;
		for(int j=0;j<m;j++) scanf("%lld%lld", &t[j].x, &t[j].y);
		for(int j=0;j<m;j++) p[s++] = (t[j] - t[(j + 1 == m) ? 0 : j + 1]);
	}
	for(int i=0;i<s;i++) t[i] = p[i];
	sort(p, p + s); int pcnt = unique(p, p + s) - p;
	for(int i=0;i<s;i++) a[i] = lower_bound(p, p + pcnt, t[i]) - p;

	scanf("%d", &q);
	for(int i=1;i<=q;i++) {
		scanf("%d%d", &qry[i].l, &qry[i].r), qry[i].i = i;
		qry[i].l = le[qry[i].l], qry[i].r = ri[qry[i].r];
	}
	sort(qry + 1, qry + q + 1);
	for(int i=1,L=0,R=-1;i<=q;i++) {
		while( L > qry[i].l ) add(a[--L], 1); while( R < qry[i].r ) add(a[++R], 1);
		while( L < qry[i].l ) add(a[L++], -1); while( R > qry[i].r ) add(a[R--], -1);
		ans[qry[i].i] = nwres;
	}
	for(int i=1;i<=q;i++) printf("%d\n", ans[i]);
}


#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int B = 550;
const int N = 100000;
const int M = 300000;

struct query{
	int l, r, i;
	friend bool operator < (const query &a, const query &b) {
		return (a.l / B == b.l / B) ? (a.r < b.r) : (a.l < b.l);
	}
}qry[N + 5];

int ans[N + 5], cnt[M + 5], nwres;
void add(int x, int d) {
	if( (!cnt[x]) && d == 1 ) nwres++;
	cnt[x] += d;
	if( (!cnt[x]) && d == -1 ) nwres--;
}

struct point{
	int x, y;

	friend point operator - (const point &a, const point &b) {return (point){a.x - b.x, a.y - b.y};}
	friend bool operator < (const point &a, const point &b) {return (a.x == b.x) ? (a.y < b.y) : (a.x < b.x);}
	friend bool operator == (const point &a, const point &b) {return (a.x == b.x) && (a.y == b.y);}
}p[M + 5], t[M + 5];

int a[M + 5], le[N + 5], ri[N + 5];
int main() {
	int n, q, s = 0; scanf("%d", &n);
	for(int i=1,m;i<=n;i++) {
		scanf("%d", &m), le[i] = s, ri[i] = s + m - 1;
		for(int j=0;j<m;j++) scanf("%d%d", &t[j].x, &t[j].y);
		for(int j=0;j<m;j++) p[s++] = (t[j] - t[(j + 1 == m) ? 0 : j + 1]);
	}
	for(int i=0;i<s;i++) t[i] = p[i];
	sort(p, p + s); int pcnt = unique(p, p + s) - p;
	for(int i=0;i<s;i++) a[i] = lower_bound(p, p + pcnt, t[i]) - p;

	scanf("%d", &q);
	for(int i=1;i<=q;i++) {
		scanf("%d%d", &qry[i].l, &qry[i].r), qry[i].i = i;
		qry[i].l = le[qry[i].l], qry[i].r = ri[qry[i].r];
	}
	sort(qry + 1, qry + q + 1);
	for(int i=1,L=0,R=-1;i<=q;i++) {
		while( L > qry[i].l ) add(a[--L], 1); while( R < qry[i].r ) add(a[++R], 1);
		while( L < qry[i].l ) add(a[L++], -1); while( R > qry[i].r ) add(a[R--], -1);
		ans[qry[i].i] = nwres;
	}
	for(int i=1;i<=q;i++) printf("%d\n", ans[i]);
}


#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;

const int B = 550;
const int N = 100000;
const int M = 300000;
const double EPS = 1E-9;

struct query{
	int l, r, i;
	friend bool operator < (const query &a, const query &b) {
		return (a.l / B == b.l / B) ? (a.r < b.r) : (a.l < b.l);
	}
}qry[N + 5];

int ans[N + 5], cnt[M + 5], nwres;
void add(int x, int d) {
	if( (!cnt[x]) && d == 1 ) nwres++;
	cnt[x] += d;
	if( (!cnt[x]) && d == -1 ) nwres--;
}

struct point{
	int x, y;

	friend point operator - (const point &a, const point &b) {return (point){a.x - b.x, a.y - b.y};}
	friend ll operator ^ (const point &a, const point &b) {return a.x*b.y - a.y*b.x;}
	friend bool operator < (const point &a, const point &b) {
		if( a.y >= 0 && b.y < 0 ) return false;
		else if( a.y < 0 && b.y >= 0 ) return true;
		else if( a.y == 0 && b.y == 0 ) return a.x > 0 && b.x < 0;
		else return (a ^ b) > 0;
	}
	friend bool operator == (const point &a, const point &b) {return (a ^ b) == 0;}
}p[M + 5], t[M + 5];

int a[M + 5], le[N + 5], ri[N + 5];
int main() {
	int n, q, s = 0; scanf("%d", &n);
	for(int i=1,m;i<=n;i++) {
		scanf("%d", &m), le[i] = s, ri[i] = s + m - 1;
		for(int j=0;j<m;j++) scanf("%d%d", &t[j].x, &t[j].y);
		for(int j=0;j<m;j++) p[s++] = (t[j] - t[(j + 1 == m) ? 0 : j + 1]);
	}
	for(int i=0;i<s;i++) t[i] = p[i];
	sort(p, p + s); int pcnt = unique(p, p + s) - p;
	for(int i=0;i<s;i++) a[i] = lower_bound(p, p + pcnt, t[i]) - p;

	scanf("%d", &q);
	for(int i=1;i<=q;i++) {
		scanf("%d%d", &qry[i].l, &qry[i].r), qry[i].i = i;
		qry[i].l = le[qry[i].l], qry[i].r = ri[qry[i].r];
	}
	sort(qry + 1, qry + q + 1);
	for(int i=1,L=0,R=-1;i<=q;i++) {
		while( L > qry[i].l ) add(a[--L], 1); while( R < qry[i].r ) add(a[++R], 1);
		while( L < qry[i].l ) add(a[L++], -1); while( R > qry[i].r ) add(a[R--], -1);
		ans[qry[i].i] = nwres;
	}
	for(int i=1;i<=q;i++) printf("%d\n", ans[i]);
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int B = 550;
const int N = 100000;
const int M = 300000;
const double EPS = 1E-9;

struct query{
	int l, r, i;
	friend bool operator < (const query &a, const query &b) {
		return (a.l / B == b.l / B) ? (a.r < b.r) : (a.l < b.l);
	}
}qry[N + 5];

int ans[N + 5], cnt[M + 5], nwres;
void add(int x, int d) {
	if( (!cnt[x]) && d == 1 ) nwres++;
	cnt[x] += d;
	if( (!cnt[x]) && d == -1 ) nwres--;
}

int dcmp(double x) {return fabs(x) <= EPS ? 0 : (x < 0 ? -1 : 1);}

struct point{
	int x, y;

	friend point operator - (const point &a, const point &b) {return (point){a.x - b.x, a.y - b.y};}
	friend bool operator < (const point &a, const point &b) {return dcmp(atan2(a.y, a.x) - atan2(b.y, b.x)) < 0;}
	friend bool operator == (const point &a, const point &b) {return (1LL*a.x*b.y == 1LL*a.y*b.x);}
}p[M + 5], t[M + 5];

int a[M + 5], le[N + 5], ri[N + 5];
int main() {
	int n, q, s = 0; scanf("%d", &n);
	for(int i=1,m;i<=n;i++) {
		scanf("%d", &m), le[i] = s, ri[i] = s + m - 1;
		for(int j=0;j<m;j++) scanf("%d%d", &t[j].x, &t[j].y);
		for(int j=0;j<m;j++) p[s++] = (t[j] - t[(j + 1 == m) ? 0 : j + 1]);
	}
	for(int i=0;i<s;i++) t[i] = p[i];
	sort(p, p + s); int pcnt = unique(p, p + s) - p;
	for(int i=0;i<s;i++) a[i] = lower_bound(p, p + pcnt, t[i]) - p;

	scanf("%d", &q);
	for(int i=1;i<=q;i++) {
		scanf("%d%d", &qry[i].l, &qry[i].r), qry[i].i = i;
		qry[i].l = le[qry[i].l], qry[i].r = ri[qry[i].r];
	}
	sort(qry + 1, qry + q + 1);
	for(int i=1,L=0,R=-1;i<=q;i++) {
		while( L > qry[i].l ) add(a[--L], 1); while( R < qry[i].r ) add(a[++R], 1);
		while( L < qry[i].l ) add(a[L++], -1); while( R > qry[i].r ) add(a[R--], -1);
		ans[qry[i].i] = nwres;
	}
	for(int i=1;i<=q;i++) printf("%d\n", ans[i]);
}


#include <bits/stdc++.h>
using namespace std;
#define fo(i,a,b) for (int i = (a); i < (b); i++)
#define pb push_back
typedef long long ll;
#define N 234567

ll pw(ll a, ll b, ll md) {
    if (b==0) return 1;
    ll res = pw(a,b/2,md);
    res = (res*res)%md;
    if (b%2) res = (res*a)%md;
    return res;
}
ll gcd(ll a, ll b) {
    if (a > b) swap(a,b);
    if (a==0) return b;
    return gcd(b%a,a);
}

int tot(ll m) {
    int res = 0;
    fo(i,1,m) if (gcd(i,m)==1) res++;
    return res;
}

int bl[N], n; ll m, om;
int np[N];
vector<int> ps;

int pf[N], pp[N], nps;
int nxt[N], cnt[N], sn[N];
int go(int fct) {
    //printf("%d\n", fct);
    if (fct == m) return 1;
    if (sn[fct]) return cnt[fct];
    sn[fct] = 1;
    int rem = m/fct;
    fo(i,1,rem) if (!bl[i * fct] && gcd(i, rem)==1) cnt[fct]++;
    int bst = 0;
    fo(i,0,nps) if (fct % pp[i] != 0) {
        int to = go(fct * pf[i]);
        if (to > bst) bst = to, nxt[fct] = fct * pf[i];
    }
    cnt[fct] += bst;
    return cnt[fct];
}
int main() {
    fo(i,2,N) if (!np[i]) {
        ps.pb(i);
        for (ll j = i * 1ll * i; j < N; j += i) np[j] = 1;
    }

    scanf("%d %lld", &n, &m), om = m;
    int a;
    fo(i,0,n) scanf("%d", &a), bl[a] = 1;

    for (int p : ps) if (m%p == 0) {
        int ppp = 1, tm = m;
        while (tm % p == 0) ppp *= p, tm /= p;
        pf[nps] = p;
        pp[nps] = ppp;
        nps++;
    }
    go(1);

    ll cmlt = 1, tmlt = 1, cval = 1, ctot = tot(m);
    vector<int> res;

    while (m > 1) {
        //printf("st %lld\n", m);
        //fo(i,1,m) printf("%d %d %lld\n", i, bl[i*cmlt], gcd(i,m));
        fo(i,1,m) if (!bl[i * cmlt] && gcd(i,m) == 1) {
            res.pb((tmlt * i * pw(cval, ctot-1, m))%om);
            cval = i;
            tmlt = 1;
        }
        // adv
    /*    int bst = -1, bc = -1;
        for (ll p : ps) {
            if (p >= m) break;
            if (m % p != 0) continue;
            int cc = 0;
            for (int j = p; j < m; j += p) if (!bl[j]) cc++;
            if (cc > bc) bc = cc, bst = p;
        }*/
        int to = nxt[cmlt], ff = to / cmlt;
        m /= ff;
//        cval = (bst * cval)%m;
        ctot = tot(m);
        cmlt = to;
        tmlt = ff;
    }
    if (!bl[0]) res.pb(0);

    printf("%d\n", res.size());
    for (int r : res) printf("%d ", r);
    puts("");

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fo(i,a,b) for (int i = (a); i < (b); i++)
#define pb push_back
typedef long long ll;
#define N 234567

ll pw(ll a, ll b, ll md) {
    if (b==0) return 1;
    ll res = pw(a,b/2,md);
    res = (res*res)%md;
    if (b%2) res = (res*a)%md;
    return res;
}
ll gcd(ll a, ll b) {
    if (a > b) swap(a,b);
    if (a==0) return b;
    return gcd(b%a,a);
}

int tot(ll m) {
    int res = 0;
    fo(i,1,m) if (gcd(i,m)==1) res++;
    return res;
}

int bl[N], n; ll m, om;
int np[N];
vector<int> ps;

int pf[N], pp[N], nps;
int nxt[N], cnt[N], sn[N];
int go(int fct) {
    //printf("%d\n", fct);
    if (fct == m) return 1;
    if (sn[fct]) return cnt[fct];
    sn[fct] = 1;
    int rem = m/fct;
    fo(i,1,rem) if (!bl[i * fct] && gcd(i, rem)==1) cnt[fct]++;
    int bst = 0;
    fo(i,0,nps) if (fct % pp[i] != 0) {
        int to = go(fct * pf[i]);
        if (to > bst) bst = to, nxt[fct] = fct * pf[i];
    }
    cnt[fct] += bst;
    return cnt[fct];
}
int main() {
    fo(i,2,N) if (!np[i]) {
        ps.pb(i);
        for (ll j = i * 1ll * i; j < N; j += i) np[j] = 1;
    }

    scanf("%d %lld", &n, &m), om = m;
    int a;
    fo(i,0,n) scanf("%d", &a), bl[a] = 1;

    for (int p : ps) if (m%p == 0) {
        int ppp = 1, tm = m;
        while (tm % p == 0) ppp *= p, tm /= p;
        pf[nps] = p;
        pp[nps] = ppp;
        nps++;
    }
    go(1);

    ll cmlt = 1, tmlt = 1, cval = 1, ctot = tot(m);
    vector<int> res;

    while (1) {
        //printf("st %lld\n", m);
        //fo(i,1,m) printf("%d %d %lld\n", i, bl[i*cmlt], gcd(i,m));
        fo(i,1,m) if (!bl[i * cmlt] && gcd(i,m) == 1) {
            res.pb((tmlt * i * pw(cval, ctot-1, m))%om);
            cval = i;
            tmlt = 1;
        }
        // adv
    /*    int bst = -1, bc = -1;
        for (ll p : ps) {
            if (p >= m) break;
            if (m % p != 0) continue;
            int cc = 0;
            for (int j = p; j < m; j += p) if (!bl[j]) cc++;
            if (cc > bc) bc = cc, bst = p;
        }*/
        int to = nxt[cmlt], ff = to / cmlt;
        if (to == om) break;
        m /= ff;
//        cval = (bst * cval)%m;
        ctot = tot(m);
        cmlt = to;
        tmlt = ff;
    }
    res.pb(0);

    printf("%d\n", res.size());
    for (int r : res) printf("%d ", r);
    puts("");

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fo(i,a,b) for (int i = (a); i < (b); i++)
typedef long long ll;
#define pb push_back

#define N 234567

int n, bl[N]; ll m;
int sn[N], nxt[N], cnt[N];

ll pw(ll a, ll b, ll md) {
    if (b==0) return 1;
    ll res = pw(a,b/2,md);
    res = (res*res)%md;
    if (b%2) res = (res*a)%md;
    return res;
}
ll gcd(ll a, ll b) {
    if (a > b) swap(a,b);
    if (!a) return b;
    return gcd(b%a,a);
}
int tot(ll m) {
    int res = 0;
    fo(i,1,m) if (gcd(i,m)==1) res++;
    return res;
}

int go(int fct) {
    if (sn[fct]) return cnt[fct];
    sn[fct] = 1;
    ll rem = m/fct;
    fo(i,0,rem) if (gcd(i,rem)==1) cnt[fct]++;
    int bst = 0;
    fo(i,2,rem+1) if (rem % i == 0) {
        int adv = go(fct * i);
        if (adv > bst) bst = adv, nxt[fct] = i;
    }
    cnt[fct] += bst;
    return cnt[fct];
}

int main() {
    scanf("%d %lld", &n, &m);
    ll om = m;
    int a;
    fo(i,0,n) scanf("%d", &a), bl[a] = 1;

    go(1);

    ll cval = 1, cmlt = 1, tmlt = 1, ctot = tot(m);
    vector<int> res;
    while (1) {
        fo(i,0,m) if (!bl[cmlt * i] && gcd(i,m) == 1) {
            res.pb((tmlt * i * pw(cval,ctot-1,m))%om);
            tmlt = 1;
            cval = i;
        }
        if (m==1) break;
        int fto = nxt[om/m];
        m /= fto;
        cmlt *= fto;
        tmlt *= fto;
        ctot = tot(m);
    }

    printf("%d\n", int(res.size()));
    for (int x : res) printf("%d ", x);
    puts("");

    return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Majk
 */

#include <vector>
#include <iostream>
#include <unordered_map>
#include <map>
#include <iomanip>
#include <set>
#include <functional>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <string>
#include <sstream>
#include <queue>
using namespace std;

#define x first
#define y second
constexpr int MOD = 1000000007;

typedef std::pair<int,int> pii;
typedef long long ll;
typedef unsigned int ui;

template<typename T>std::istream&operator>>(std::istream&i,vector<T>&t) {for(auto&v:t){i>>v;}return i;}
template<typename T>std::ostream&operator<<(std::ostream&o,const vector<T>&t) {for(size_t i=0;i<t.size();++i){o<<t[i]<<" \n"[i == t.size()-1];}return o;}
template<typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using maxheap = priority_queue<T, vector<T>, less<T>>;

int logceil(ll x) {int b=0;while(x){x>>=1;++b;}return b;}

namespace std {
    template<typename T,typename U>struct hash<pair<T,U>>{hash<T>t;hash<U>u;size_t operator()(const pair<T,U>&p)const{return t(p.x)^(u(p.y)<<7);}};
}
template<typename T,typename F>T bsh(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){l=m+1;r=m;}else{h=m-1;}}return r;}
template<typename T,typename F>T bsl(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){h=m-1;r=m;}else{l=m+1;}}return r;}
template<typename T> T gcd(T a,T b) { if (a<b) swap(a,b); return b?gcd(b,a%b):a; }

template<typename T>class vector2:public vector<vector<T>>{public:vector2(){} vector2(size_t a,size_t b,T t=T()):vector<vector<T>>(a,vector<T>(b,t)){}};
template<typename T>class vector3:public vector<vector<vector<T>>>{public:vector3(){} vector3(size_t a,size_t b,size_t c,T t=T()):vector<vector<vector<T>>>(a,vector<vector<T>>(b,vector<T>(c,t))){}};
// #include "../l/mod.h"

class TaskC {
public:
	int egcd(int a, int b, int N) {
		ll t=0,nt=1,r=b,nr=a,s=0,ns=1;
		while(nr){
			ll q=r/nr;
			t-=q*nt;
			r-=q*nr;
			s-=q*ns;
			swap(t,nt);
			swap(r,nr);
			swap(s,ns);
		}
		t=(t<0)?t+N:t;

		return t;
	}

    void solve(istream& cin, ostream& cout) {
		int N, M; cin >> N >> M;
		if (M == 1) { cout << "1\n0\n"; return; }
		vector<bool> A(M, true);
		for (int i = 0; i < N; ++i) {
			int a; cin >> a; A[a] = false;
		}
		if (N == M-1) {
			for (int i = 0; i < M; ++i) {
				if (A[i]) {
					cout << 1 << '\n' << i << '\n';
					return;
				}
			}
		}

		vector<int> F;
		for (int i = 2, m = M; i < M && m != 1; ++i) while (m%i==0) { m/=i; F.push_back(i); }

		map<int, vector<int>> V;
		for (int i = 1; i < M; ++i) {
			if (!A[i]) continue;
			int g = i;
			int G = 1;
			for (int v:F) {
				if (g % v == 0) { g /= v; G *= v; }
			}
			V[G].push_back(i);
		}

		vector<pair<int, vector<int>>> G;
		for (auto it = V.begin(); it != V.end(); ++it) {
			G.push_back(*it);
		}

		size_t GG = G.size();
		vector<vector<int>> E(GG);
		vector<int> S(GG, 0), B(GG, 0), P(GG, -1);
		for (int i = 0; i < GG; ++i) {
			for (int j = 0; j < GG; ++j) {
				if (i!=j && G[j].x%G[i].x==0) {
					E[i].push_back(j);
					S[j]++;
				}
			}
		}

		queue<int> Q;
		for (int i = 0; i < GG; ++i) if (!S[i]) Q.push(i);
		while (!Q.empty()) {
			int q = Q.front(); Q.pop();
			B[q] += G[q].y.size();
			for (int r: E[q]) {
				if (B[q] > B[r]) {
					B[r] = B[q];
					P[r] = q;
				}
				if (--S[r]==0) Q.push(r);
			}
		}

		pii b = {B[0], 0};
		for (int i = 0; i < GG; ++i) { b = max(b, {B[i], i}); }
		int r = b.y;
		vector<int> R;
		while (r != -1) { R.push_back(r);r = P[r]; }
		reverse(R.begin(), R.end());

		vector<pii> Z;
		for (int r:R) for (int q:G[r].y) Z.push_back({q, G[r].x});

		vector<int> ZZ;
		int p = 1, pr = 1;
		for (pii z:Z) {
			int q = egcd(p, M / z.y, M / z.y);
			ZZ.push_back(((ll)q*(ll)z.x / pr)%M);
			p = z.x / z.y; pr = z.y;
		}

		if (A[0]) ZZ.push_back(0);
		cout << ZZ.size() << endl;
		cout << ZZ;
	}
};

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	TaskC solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Majk
 */

#include <vector>
#include <iostream>
#include <unordered_map>
#include <map>
#include <iomanip>
#include <set>
#include <functional>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <string>
#include <sstream>
#include <queue>
using namespace std;

#define x first
#define y second
constexpr int MOD = 1000000007;

typedef std::pair<int,int> pii;
typedef long long ll;
typedef unsigned int ui;

template<typename T>std::istream&operator>>(std::istream&i,vector<T>&t) {for(auto&v:t){i>>v;}return i;}
template<typename T>std::ostream&operator<<(std::ostream&o,const vector<T>&t) {for(size_t i=0;i<t.size();++i){o<<t[i]<<" \n"[i == t.size()-1];}return o;}
template<typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using maxheap = priority_queue<T, vector<T>, less<T>>;

int logceil(ll x) {int b=0;while(x){x>>=1;++b;}return b;}

namespace std {
    template<typename T,typename U>struct hash<pair<T,U>>{hash<T>t;hash<U>u;size_t operator()(const pair<T,U>&p)const{return t(p.x)^(u(p.y)<<7);}};
}
template<typename T,typename F>T bsh(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){l=m+1;r=m;}else{h=m-1;}}return r;}
template<typename T,typename F>T bsl(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){h=m-1;r=m;}else{l=m+1;}}return r;}
template<typename T> T gcd(T a,T b) { if (a<b) swap(a,b); return b?gcd(b,a%b):a; }

template<typename T>class vector2:public vector<vector<T>>{public:vector2(){} vector2(size_t a,size_t b,T t=T()):vector<vector<T>>(a,vector<T>(b,t)){}};
template<typename T>class vector3:public vector<vector<vector<T>>>{public:vector3(){} vector3(size_t a,size_t b,size_t c,T t=T()):vector<vector<vector<T>>>(a,vector<vector<T>>(b,vector<T>(c,t))){}};
#ifndef MOD_H
#define MOD_H
template <unsigned int N> class Field {
    typedef unsigned int ui;
    typedef unsigned long long ull;
	inline ui pow(ui a, ui p){ui r=1,e=a;while(p){if(p&1){r=((ull)r*e)%N;}e=((ull)e*e)%N;p>>=1;}return r;}
	/*extended GCD(slow):ll t=0,nt=1,r=N,nr=a;while(nr){ll q=r/nr;t-=q*nt;swap(t,nt);r-=q*nr;swap(r,nr);}assert(r<=1);return(t<0)?t+N:t;*/
	inline ui inv(ui a){return pow(a,N-2);}
public:
    inline Field(int x = 0) : v(x) {}
	inline Field<N> pow(int p){return (*this)^p; }
	inline Field<N> operator^(int p){return {(int)pow(v,(ui)p)};}
    inline Field<N>&operator+=(const Field<N>&o) {if ((ll)v+o.v >= N) v = (ll)v+o.v-N; else v = v+o.v; return *this; }
    inline Field<N>&operator-=(const Field<N>&o) {if (v<o.v) v = N-o.v+v; else v=v-o.v; return *this; }
    inline Field<N>&operator*=(const Field<N>&o) {v=(ull)v*o.v % N; return *this; }
    inline Field<N>&operator/=(const Field<N>&o) { return *this*=inv(o.v); }
    inline Field<N> operator+(const Field<N>&o) {Field<N>r{*this};return r+=o;}
    inline Field<N> operator-(const Field<N>&o) {Field<N>r{*this};return r-=o;}
    inline Field<N> operator*(const Field<N>&o) {Field<N>r{*this};return r*=o;}
    inline Field<N> operator/(const Field<N>&o) {Field<N>r{*this};return r/=o;}
    inline Field<N> operator-() {if(v) return {(int)(N-v)}; else return {0};};
    inline Field<N>& operator++() { ++v; if (v==N) v=0; return *this; }
    inline Field<N> operator++(int) { Field<N>r{*this}; ++*this; return r; }
    inline Field<N>& operator--() { --v; if (v==-1) v=N-1; return *this; }
    inline Field<N> operator--(int) { Field<N>r{*this}; --*this; return r; }
    inline bool operator==(const Field<N>&o) { return o.v==v; }
    inline explicit operator ui() const { return v; }
	inline static vector<Field<N>>fact(int t){vector<Field<N>>F(t+1,1);for(int i=2;i<=t;++i){F[i]=F[i-1]*i;}return F;}
	inline static vector<Field<N>>invfact(int t){vector<Field<N>>F(t+1,1);for(int i=2;i<=t;++i){F[i]=F[i-1]/i;}return F;}
private: ui v;
};
template<unsigned int N>istream &operator>>(std::istream&is,Field<N>&f){unsigned int v;is>>v;f=v;return is;}
template<unsigned int N>ostream &operator<<(std::ostream&os,const Field<N>&f){return os<<(unsigned int)f;}
template<unsigned int N>Field<N> operator+(int i,const Field<N>&f){return Field<N>(i)+f;}
template<unsigned int N>Field<N> operator-(int i,const Field<N>&f){return Field<N>(i)-f;}
template<unsigned int N>Field<N> operator*(int i,const Field<N>&f){return Field<N>(i)*f;}
template<unsigned int N>Field<N> operator/(int i,const Field<N>&f){return Field<N>(i)/f;}

typedef Field<1000000007> FieldMod;

struct Ring {
	static int div(int p, int q, int N) {
		ll t=0,nt=1,r=N,nr=q;
		while(nr){ ll q=r/nr;t-=q*nt;r-=q*nr;swap(t,nt);swap(r,nr); }
		t=(t<0)?t+N:t;
		if (gcd(p,N)%r) { return 0; }
		return (int)((ll)t*(ll)p/r)%N;
	}
};
#endif

class TaskC {
public:

	void solve(istream& cin, ostream& cout) {
		int N, M; cin >> N >> M;
		if (M == 1) { cout << "1\n0\n"; return; }

		vector<bool> A(M, true);
		for (int i = 0; i < N; ++i) {
			int a; cin >> a; A[a] = false;
		}

		map<int, vector<int>> V;
		for (int i = 1; i < M; ++i) {
			if (!A[i]) continue;
			V[gcd(M, i)].push_back(i);
		}

		if (V.size()==0) { cout << "1\n0\n"; return; }

		vector<pair<int, vector<int>>> G;
		for (auto it = V.begin(); it != V.end(); ++it) G.push_back(*it);

		// TODO: extract longest path in DAG
		size_t GG = G.size();
		vector<vector<int>> E(GG);
		vector<int> S(GG, 0), B(GG, 0), P(GG, -1);
		for (int i = 0; i < GG; ++i) {
			for (int j = 0; j < GG; ++j) {
				if (i!=j && G[j].x%G[i].x==0) {
					E[i].push_back(j);
					S[j]++;
				}
			}
		}

		queue<int> Q;
		for (int i = 0; i < GG; ++i) if (!S[i]) Q.push(i);
		while (!Q.empty()) {
			int q = Q.front(); Q.pop();
			B[q] += G[q].y.size();
			for (int r: E[q]) {
				if (B[q] > B[r]) {
					B[r] = B[q];
					P[r] = q;
				}
				if (!--S[r]) Q.push(r);
			}
		}

		pii b = {B[0], 0};
		for (int i = 0; i < GG; ++i) { b = max(b, {B[i], i}); }
		int r = b.y;
		deque<int> R;
		while (r != -1) { R.push_front(r); r = P[r]; }

		vector<int> Ans;
		int p = 1;
		for (int r:R) for (int q:G[r].y) Ans.push_back(Ring::div(q, p, M)), p = q;

		if (A[0]) Ans.push_back(0);
		cout << Ans.size() << endl;
		cout << Ans;
	}
};

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	TaskC solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Majk
 */

#include <vector>
#include <iostream>
#include <unordered_map>
#include <map>
#include <iomanip>
#include <set>
#include <functional>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <string>
#include <sstream>
#include <queue>
using namespace std;

#define x first
#define y second
constexpr int MOD = 1000000007;

typedef std::pair<int,int> pii;
typedef long long ll;
typedef unsigned int ui;

template<typename T>std::istream&operator>>(std::istream&i,vector<T>&t) {for(auto&v:t){i>>v;}return i;}
template<typename T>std::ostream&operator<<(std::ostream&o,const vector<T>&t) {for(size_t i=0;i<t.size();++i){o<<t[i]<<" \n"[i == t.size()-1];}return o;}
template<typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using maxheap = priority_queue<T, vector<T>, less<T>>;

int logceil(ll x) {int b=0;while(x){x>>=1;++b;}return b;}

namespace std {
    template<typename T,typename U>struct hash<pair<T,U>>{hash<T>t;hash<U>u;size_t operator()(const pair<T,U>&p)const{return t(p.x)^(u(p.y)<<7);}};
}
template<typename T,typename F>T bsh(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){l=m+1;r=m;}else{h=m-1;}}return r;}
template<typename T,typename F>T bsl(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){h=m-1;r=m;}else{l=m+1;}}return r;}
template<typename T> T gcd(T a,T b) { if (a<b) swap(a,b); return b?gcd(b,a%b):a; }

template<typename T>class vector2:public vector<vector<T>>{public:vector2(){} vector2(size_t a,size_t b,T t=T()):vector<vector<T>>(a,vector<T>(b,t)){}};
template<typename T>class vector3:public vector<vector<vector<T>>>{public:vector3(){} vector3(size_t a,size_t b,size_t c,T t=T()):vector<vector<vector<T>>>(a,vector<vector<T>>(b,vector<T>(c,t))){}};
// #include "../l/mod.h"

class TaskC {
public:
	vector<int> P;

	int egcd(int a, int b, int N) {
		ll t=0,nt=1,r=b,nr=a,s=0,ns=1;
		while(nr){
			ll q=r/nr;
			t-=q*nt;
			r-=q*nr;
			s-=q*ns;
			swap(t,nt);
			swap(r,nr);
			swap(s,ns);
		}
		t=(t<0)?t+N:t;

		return t;
	}

    void solve(istream& cin, ostream& cout) {
		int N, M; cin >> N >> M;
		if (M == 1) { cout << "1\n0\n"; return; }
		vector<bool> A(M, true);
		for (int i = 0; i < N; ++i) {
			int a; cin >> a; A[a] = false;
		}

		P = vector<int>(M+1, 0);
		for (int i = 2; i <= M; ++i) {
			if (P[i] == 0) {
				for (int j = i; j <= M; j+=i) {
					P[j] = i;
				}
			}
		}
		vector<int> F;
		int m = M;
		while (m!=1) {
			F.push_back(P[m]);
			m /= P[m];
		}

		cerr << F;

		map<int, vector<int>> V;
		for (int i = 1; i < M; ++i) {
			if (!A[i]) continue;
			int g = i;
			int G = 1;
			for (int v:F) {
				if (g % v == 0) { g /= v; G *= v; }
			}
			V[G].push_back(i);
		}

		vector<pair<int, vector<int>>> G;
		int orig = -1;
		for (auto it = V.begin(); it != V.end(); ++it) {
			if (it->x==1) orig = G.size();
			G.push_back(*it);
		}
		assert(orig!=-1);

		int GG = G.size();
		vector<vector<int>> E(GG);
		vector<int> S(GG, 0);
		vector<int> B(GG, 0);
		vector<int> P(GG, -1);
		for (int i = 0; i < GG; ++i) {
			for (int j = 0; j < GG; ++j) {
				if (i!=j && G[j].x%G[i].x==0) {
					E[i].push_back(j);
					S[j]++;
				}
			}
		}

//		cerr << B;
//		for (int i = 0; i < GG; ++i) {
//			cerr << G[i].y.size() << ' ';
//		} cerr << endl;

		queue<int> Q; Q.push(orig);
		while (!Q.empty()) {
			int q = Q.front(); Q.pop();
			B[q] += G[q].y.size();
			for (int r: E[q]) {
				if (B[q] > B[r]) {
					B[r] = B[q];
					P[r] = q;
				}
				if (--S[r]==0) Q.push(r);
			}
		}

		pii b = {B[0], 0};
		for (int i = 0; i < GG; ++i) { b = max(b, {B[i], i}); } int r = b.y;
		vector<int> R;while (r != -1) { R.push_back(r);r = P[r]; }
		reverse(R.begin(), R.end());
		vector<pii> Z;
		for (int r:R) for (int q:G[r].y) Z.push_back({q, G[r].x});

		vector<int> ZZ, ZZZ;
		int p = 1;
		for (pii z:Z) {
			int q = egcd(p, M / z.y, M / z.y);
			ZZ.push_back((q*z.x)%M);
			ZZZ.push_back((q*p*z.x)%M);
			p = z.x;
		}
//		for (int i = 0; i < Z.size(); ++i) {
//			cerr << Z[i].x << ' ' << ZZ[i] << ' ' << ZZZ[i] << endl;
//		}
		if (A[0]) {
			ZZ.push_back(0);
		}
//		cerr << ZZZ;
		cout << ZZ.size() << endl;
		cout << ZZ;
	}
};

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	TaskC solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Majk
 */

#include <vector>
#include <iostream>
#include <unordered_map>
#include <map>
#include <iomanip>
#include <set>
#include <functional>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <string>
#include <sstream>
#include <queue>
using namespace std;

#define x first
#define y second
constexpr int MOD = 1000000007;

typedef std::pair<int,int> pii;
typedef long long ll;
typedef unsigned int ui;

template<typename T>std::istream&operator>>(std::istream&i,vector<T>&t) {for(auto&v:t){i>>v;}return i;}
template<typename T>std::ostream&operator<<(std::ostream&o,const vector<T>&t) {for(size_t i=0;i<t.size();++i){o<<t[i]<<" \n"[i == t.size()-1];}return o;}
template<typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using maxheap = priority_queue<T, vector<T>, less<T>>;

int logceil(ll x) {int b=0;while(x){x>>=1;++b;}return b;}

namespace std {
    template<typename T,typename U>struct hash<pair<T,U>>{hash<T>t;hash<U>u;size_t operator()(const pair<T,U>&p)const{return t(p.x)^(u(p.y)<<7);}};
}
template<typename T,typename F>T bsh(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){l=m+1;r=m;}else{h=m-1;}}return r;}
template<typename T,typename F>T bsl(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){h=m-1;r=m;}else{l=m+1;}}return r;}
template<typename T> T gcd(T a,T b) { if (a<b) swap(a,b); return b?gcd(b,a%b):a; }

template<typename T>class vector2:public vector<vector<T>>{public:vector2(){} vector2(size_t a,size_t b,T t=T()):vector<vector<T>>(a,vector<T>(b,t)){}};
template<typename T>class vector3:public vector<vector<vector<T>>>{public:vector3(){} vector3(size_t a,size_t b,size_t c,T t=T()):vector<vector<vector<T>>>(a,vector<vector<T>>(b,vector<T>(c,t))){}};
// #include "../l/mod.h"

class TaskC {
public:
	vector<int> P;

	int egcd(int a, int b, int N) {
		ll t=0,nt=1,r=b,nr=a,s=0,ns=1;
		while(nr){
			ll q=r/nr;
			t-=q*nt;
			r-=q*nr;
			s-=q*ns;
			swap(t,nt);
			swap(r,nr);
			swap(s,ns);
		}
		t=(t<0)?t+N:t;

		return t;
	}

    void solve(istream& cin, ostream& cout) {
		int N, M; cin >> N >> M;
		if (M == 1) { cout << "1\n0\n"; return; }
		vector<bool> A(M, true);
		for (int i = 0; i < N; ++i) {
			int a; cin >> a; A[a] = false;
		}

		P = vector<int>(M+1, 0);
		for (int i = 2; i <= M; ++i) {
			if (P[i] == 0) {
				for (int j = i; j <= M; j+=i) {
					P[j] = i;
				}
			}
		}
		vector<int> F;
		int m = M;
		while (m!=1) {
			F.push_back(P[m]);
			m /= P[m];
		}

		map<int, vector<int>> V;
		for (int i = 1; i < M; ++i) {
			if (!A[i]) continue;
			int g = i;
			int G = 1;
			for (int v:F) {
				if (g % v == 0) { g /= v; G *= v; }
			}
			V[G].push_back(i);
		}

		vector<pair<int, vector<int>>> G;
		int orig = -1;
		for (auto it = V.begin(); it != V.end(); ++it) {
			if (it->x==1) orig = G.size();
			G.push_back(*it);
		}
		assert(orig!=-1);

		int GG = G.size();
		vector<vector<int>> E(GG);
		vector<int> S(GG, 0);
		vector<int> B(GG, 0);
		vector<int> P(GG, -1);
		for (int i = 0; i < GG; ++i) {
			for (int j = 0; j < GG; ++j) {
				if (i!=j && G[j].x%G[i].x==0) {
					E[i].push_back(j);
					S[j]++;
				}
			}
		}

		queue<int> Q; Q.push(orig);
		while (!Q.empty()) {
			int q = Q.front(); Q.pop();
			B[q] += G[q].y.size();
			for (int r: E[q]) {
				if (B[q] > B[r]) {
					B[r] = B[q];
					P[r] = q;
				}
				if (--S[r]==0) Q.push(r);
			}
		}

		pii b = {B[0], 0};
		for (int i = 0; i < GG; ++i) { b = max(b, {B[i], i}); } int r = b.y;
		vector<int> R;while (r != -1) { R.push_back(r);r = P[r]; }
		reverse(R.begin(), R.end());
		vector<pii> Z;
		for (int r:R) for (int q:G[r].y) Z.push_back({q, G[r].x});

		vector<int> ZZ;
		int p = 1;
		for (pii z:Z) {
			int q = egcd(p, M / z.y, M / z.y);
			ZZ.push_back(((ll)q*(ll)z.x)%M);
			//cerr << ((ll)q*(ll)z.x)%M * ((ll)p)%M << '(' << z.x << ')' << ' ';
			p = z.x;
		}
		// cerr << endl;
		if (A[0]) {
			ZZ.push_back(0);
		}
		cout << ZZ.size() << endl;
		cout << ZZ;
	}
};

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	TaskC solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Majk
 */

#include <vector>
#include <iostream>
#include <unordered_map>
#include <map>
#include <iomanip>
#include <set>
#include <functional>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <string>
#include <sstream>
#include <queue>
using namespace std;

#define x first
#define y second
constexpr int MOD = 1000000007;

typedef std::pair<int,int> pii;
typedef long long ll;
typedef unsigned int ui;

template<typename T>std::istream&operator>>(std::istream&i,vector<T>&t) {for(auto&v:t){i>>v;}return i;}
template<typename T>std::ostream&operator<<(std::ostream&o,const vector<T>&t) {for(size_t i=0;i<t.size();++i){o<<t[i]<<" \n"[i == t.size()-1];}return o;}
template<typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using maxheap = priority_queue<T, vector<T>, less<T>>;

int logceil(ll x) {int b=0;while(x){x>>=1;++b;}return b;}

namespace std {
    template<typename T,typename U>struct hash<pair<T,U>>{hash<T>t;hash<U>u;size_t operator()(const pair<T,U>&p)const{return t(p.x)^(u(p.y)<<7);}};
}
template<typename T,typename F>T bsh(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){l=m+1;r=m;}else{h=m-1;}}return r;}
template<typename T,typename F>T bsl(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){h=m-1;r=m;}else{l=m+1;}}return r;}
template<typename T> T gcd(T a,T b) { if (a<b) swap(a,b); return b?gcd(b,a%b):a; }

template<typename T>class vector2:public vector<vector<T>>{public:vector2(){} vector2(size_t a,size_t b,T t=T()):vector<vector<T>>(a,vector<T>(b,t)){}};
template<typename T>class vector3:public vector<vector<vector<T>>>{public:vector3(){} vector3(size_t a,size_t b,size_t c,T t=T()):vector<vector<vector<T>>>(a,vector<vector<T>>(b,vector<T>(c,t))){}};
// #include "../l/mod.h"

class TaskC {
public:
	int egcd(int a, int N) {
		ll t=0,nt=1,r=N,nr=a;
		while(nr){
			ll q=r/nr;
			t-=q*nt;
			r-=q*nr;
			swap(t,nt);
			swap(r,nr);
		}
		t=(t<0)?t+N:t;
		return t;
	}

	void solve(istream& cin, ostream& cout) {
		int N, M; cin >> N >> M;
		if (M == 1) { cout << "1\n0\n"; return; }
		vector<bool> A(M, true);
		for (int i = 0; i < N; ++i) {
			int a; cin >> a; A[a] = false;
		}
		if (N == M-1) {
			for (int i = 0; i < M; ++i) {
				if (A[i]) {
					cout << 1 << '\n' << i << '\n';
					return;
				}
			}
		}

		vector<int> F;
		for (int i = 2, m = M; i < M && m != 1; ++i) while (m%i==0) { m/=i; F.push_back(i); }

		map<int, vector<int>> V;
		for (int i = 1; i < M; ++i) {
			if (!A[i]) continue;
			int g = i;
			int G = 1;
			for (int v:F) {
				if (g % v == 0) { g /= v; G *= v; }
			}
			V[G].push_back(i);
		}

		vector<pair<int, vector<int>>> G;
		for (auto it = V.begin(); it != V.end(); ++it) {
			G.push_back(*it);
		}

		size_t GG = G.size();
		vector<vector<int>> E(GG);
		vector<int> S(GG, 0), B(GG, 0), P(GG, -1);
		for (int i = 0; i < GG; ++i) {
			for (int j = 0; j < GG; ++j) {
				if (i!=j && G[j].x%G[i].x==0) {
					E[i].push_back(j);
					S[j]++;
				}
			}
		}

		queue<int> Q;
		for (int i = 0; i < GG; ++i) if (!S[i]) Q.push(i);
		while (!Q.empty()) {
			int q = Q.front(); Q.pop();
			B[q] += G[q].y.size();
			for (int r: E[q]) {
				if (B[q] > B[r]) {
					B[r] = B[q];
					P[r] = q;
				}
				if (--S[r]==0) Q.push(r);
			}
		}

		pii b = {B[0], 0};
		for (int i = 0; i < GG; ++i) { b = max(b, {B[i], i}); }
		int r = b.y;
		vector<int> R;
		while (r != -1) { R.push_back(r);r = P[r]; }
		reverse(R.begin(), R.end());

		vector<pii> Z;
		for (int r:R) for (int q:G[r].y) Z.push_back({q, G[r].x});

		// cerr << egcd(2, 4) << ' ' << egcd(1,2) << endl;
		//cerr << (6 * egcd(6, 16) * 8 / gcd(6, 16)) % 16 << ' ' << egcd(3, 8) << ' ' << egcd(3, 16) << endl;

		vector<int> ZZ;
		int p = 1, pr = 1;
		for (pii z:Z) {
			int q = egcd(p / pr, M / z.y);
			//cerr << ((z.x * egcd(p, M) / gcd(p, M)) % M  * p) %M << ' ' <<  z.x << endl;
			// cerr << q << ' ' << egcd(p, M) / gcd(p, M / z.y * pr) << endl;
			//cerr << q << ' ' << pr*egcd(p * pr / pr, M * pr /z.y) << endl;
			ZZ.push_back(((ll)egcd(p,M)*(ll)z.x / gcd(p, M))%M);
			p = z.x; pr = z.y;
		}

		if (A[0]) ZZ.push_back(0);
		cout << ZZ.size() << endl;
		cout << ZZ;
	}
};

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	TaskC solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Majk
 */

#include <vector>
#include <iostream>
#include <unordered_map>
#include <map>
#include <iomanip>
#include <set>
#include <functional>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <string>
#include <sstream>
#include <queue>
using namespace std;

#define x first
#define y second
constexpr int MOD = 1000000007;

typedef std::pair<int,int> pii;
typedef long long ll;
typedef unsigned int ui;

template<typename T>std::istream&operator>>(std::istream&i,vector<T>&t) {for(auto&v:t){i>>v;}return i;}
template<typename T>std::ostream&operator<<(std::ostream&o,const vector<T>&t) {for(size_t i=0;i<t.size();++i){o<<t[i]<<" \n"[i == t.size()-1];}return o;}
template<typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using maxheap = priority_queue<T, vector<T>, less<T>>;

int logceil(ll x) {int b=0;while(x){x>>=1;++b;}return b;}

namespace std {
    template<typename T,typename U>struct hash<pair<T,U>>{hash<T>t;hash<U>u;size_t operator()(const pair<T,U>&p)const{return t(p.x)^(u(p.y)<<7);}};
}
template<typename T,typename F>T bsh(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){l=m+1;r=m;}else{h=m-1;}}return r;}
template<typename T,typename F>T bsl(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){h=m-1;r=m;}else{l=m+1;}}return r;}
template<typename T> T gcd(T a,T b) { if (a<b) swap(a,b); return b?gcd(b,a%b):a; }

template<typename T>class vector2:public vector<vector<T>>{public:vector2(){} vector2(size_t a,size_t b,T t=T()):vector<vector<T>>(a,vector<T>(b,t)){}};
template<typename T>class vector3:public vector<vector<vector<T>>>{public:vector3(){} vector3(size_t a,size_t b,size_t c,T t=T()):vector<vector<vector<T>>>(a,vector<vector<T>>(b,vector<T>(c,t))){}};
#ifndef MOD_H
#define MOD_H
template <unsigned int N> class Field {
    typedef unsigned int ui;
    typedef unsigned long long ull;
	inline ui pow(ui a, ui p){ui r=1,e=a;while(p){if(p&1){r=((ull)r*e)%N;}e=((ull)e*e)%N;p>>=1;}return r;}
	/*extended GCD(slow):ll t=0,nt=1,r=N,nr=a;while(nr){ll q=r/nr;t-=q*nt;swap(t,nt);r-=q*nr;swap(r,nr);}assert(r<=1);return(t<0)?t+N:t;*/
	inline ui inv(ui a){return pow(a,N-2);}
public:
    inline Field(int x = 0) : v(x) {}
	inline Field<N> pow(int p){return (*this)^p; }
	inline Field<N> operator^(int p){return {(int)pow(v,(ui)p)};}
    inline Field<N>&operator+=(const Field<N>&o) {if ((ll)v+o.v >= N) v = (ll)v+o.v-N; else v = v+o.v; return *this; }
    inline Field<N>&operator-=(const Field<N>&o) {if (v<o.v) v = N-o.v+v; else v=v-o.v; return *this; }
    inline Field<N>&operator*=(const Field<N>&o) {v=(ull)v*o.v % N; return *this; }
    inline Field<N>&operator/=(const Field<N>&o) { return *this*=inv(o.v); }
    inline Field<N> operator+(const Field<N>&o) {Field<N>r{*this};return r+=o;}
    inline Field<N> operator-(const Field<N>&o) {Field<N>r{*this};return r-=o;}
    inline Field<N> operator*(const Field<N>&o) {Field<N>r{*this};return r*=o;}
    inline Field<N> operator/(const Field<N>&o) {Field<N>r{*this};return r/=o;}
    inline Field<N> operator-() {if(v) return {(int)(N-v)}; else return {0};};
    inline Field<N>& operator++() { ++v; if (v==N) v=0; return *this; }
    inline Field<N> operator++(int) { Field<N>r{*this}; ++*this; return r; }
    inline Field<N>& operator--() { --v; if (v==-1) v=N-1; return *this; }
    inline Field<N> operator--(int) { Field<N>r{*this}; --*this; return r; }
    inline bool operator==(const Field<N>&o) { return o.v==v; }
    inline explicit operator ui() const { return v; }
	inline static vector<Field<N>>fact(int t){vector<Field<N>>F(t+1,1);for(int i=2;i<=t;++i){F[i]=F[i-1]*i;}return F;}
	inline static vector<Field<N>>invfact(int t){vector<Field<N>>F(t+1,1);for(int i=2;i<=t;++i){F[i]=F[i-1]/i;}return F;}
private: ui v;
};
template<unsigned int N>istream &operator>>(std::istream&is,Field<N>&f){unsigned int v;is>>v;f=v;return is;}
template<unsigned int N>ostream &operator<<(std::ostream&os,const Field<N>&f){return os<<(unsigned int)f;}
template<unsigned int N>Field<N> operator+(int i,const Field<N>&f){return Field<N>(i)+f;}
template<unsigned int N>Field<N> operator-(int i,const Field<N>&f){return Field<N>(i)-f;}
template<unsigned int N>Field<N> operator*(int i,const Field<N>&f){return Field<N>(i)*f;}
template<unsigned int N>Field<N> operator/(int i,const Field<N>&f){return Field<N>(i)/f;}

typedef Field<1000000007> FieldMod;

struct Ring {
	static int div(int p, int q, int N) {
		ll t=0,nt=1,r=N,nr=q;
		while(nr){ ll q=r/nr;t-=q*nt;r-=q*nr;swap(t,nt);swap(r,nr); }
		t=(t<0)?t+N:t;
		r=(r<0)?r+N:r;
		if (gcd(p,N)%r) { return 0; }
		return (int)(((ll)t*(ll)p/r)%N);
	}
};
#endif

class TaskC {
public:

	void solve(istream& cin, ostream& cout) {
		int N, M; cin >> N >> M;
		if (M == 1) { cout << "1\n0\n"; return; }
		vector<bool> A(M, true);
		for (int i = 0; i < N; ++i) {
			int a; cin >> a; A[a] = false;
		}
		if (N == M-1) {
			for (int i = 0; i < M; ++i) {
				if (A[i]) {
					cout << 1 << '\n' << i << '\n';
					return;
				}
			}
		}

		map<int, vector<int>> V;
		for (int i = 1; i < M; ++i) {
			if (!A[i]) continue;
			V[gcd(M, i)].push_back(i);
		}

		vector<pair<int, vector<int>>> G;
		for (auto it = V.begin(); it != V.end(); ++it) G.push_back(*it);

		size_t GG = G.size();
		vector<vector<int>> E(GG);
		vector<int> S(GG, 0), B(GG, 0), P(GG, -1);
		for (int i = 0; i < GG; ++i) {
			for (int j = 0; j < GG; ++j) {
				if (i!=j && G[j].x%G[i].x==0) {
					E[i].push_back(j);
					S[j]++;
				}
			}
		}

		queue<int> Q;
		for (int i = 0; i < GG; ++i) if (!S[i]) Q.push(i);
		while (!Q.empty()) {
			int q = Q.front(); Q.pop();
			B[q] += G[q].y.size();
			for (int r: E[q]) {
				if (B[q] > B[r]) {
					B[r] = B[q];
					P[r] = q;
				}
				if (--S[r]==0) Q.push(r);
			}
		}

		pii b = {B[0], 0};
		for (int i = 0; i < GG; ++i) { b = max(b, {B[i], i}); }
		int r = b.y;
		deque<int> R;
		while (r != -1) { R.push_front(r); r = P[r]; }

		vector<int> Ans;
		int p = 1;
		for (int r:R) for (int q:G[r].y) Ans.push_back(Ring::div(q, p, M)), p = q;

		if (A[0]) Ans.push_back(0);
		cout << Ans.size() << endl;
		cout << Ans;
	}
};

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	TaskC solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Majk
 */

#include <vector>
#include <iostream>
#include <unordered_map>
#include <map>
#include <iomanip>
#include <set>
#include <functional>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <string>
#include <sstream>
#include <queue>
using namespace std;

#define x first
#define y second
constexpr int MOD = 1000000007;

typedef std::pair<int,int> pii;
typedef long long ll;
typedef unsigned int ui;

template<typename T>std::istream&operator>>(std::istream&i,vector<T>&t) {for(auto&v:t){i>>v;}return i;}
template<typename T>std::ostream&operator<<(std::ostream&o,const vector<T>&t) {for(size_t i=0;i<t.size();++i){o<<t[i]<<" \n"[i == t.size()-1];}return o;}
template<typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using maxheap = priority_queue<T, vector<T>, less<T>>;

int logceil(ll x) {int b=0;while(x){x>>=1;++b;}return b;}

namespace std {
    template<typename T,typename U>struct hash<pair<T,U>>{hash<T>t;hash<U>u;size_t operator()(const pair<T,U>&p)const{return t(p.x)^(u(p.y)<<7);}};
}
template<typename T,typename F>T bsh(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){l=m+1;r=m;}else{h=m-1;}}return r;}
template<typename T,typename F>T bsl(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){h=m-1;r=m;}else{l=m+1;}}return r;}
template<typename T> T gcd(T a,T b) { if (a<b) swap(a,b); return b?gcd(b,a%b):a; }

template<typename T>class vector2:public vector<vector<T>>{public:vector2(){} vector2(size_t a,size_t b,T t=T()):vector<vector<T>>(a,vector<T>(b,t)){}};
template<typename T>class vector3:public vector<vector<vector<T>>>{public:vector3(){} vector3(size_t a,size_t b,size_t c,T t=T()):vector<vector<vector<T>>>(a,vector<vector<T>>(b,vector<T>(c,t))){}};
#ifndef MOD_H
#define MOD_H
template <unsigned int N> class Field {
    typedef unsigned int ui;
    typedef unsigned long long ull;
	inline ui pow(ui a, ui p){ui r=1,e=a;while(p){if(p&1){r=((ull)r*e)%N;}e=((ull)e*e)%N;p>>=1;}return r;}
	/*extended GCD(slow):ll t=0,nt=1,r=N,nr=a;while(nr){ll q=r/nr;t-=q*nt;swap(t,nt);r-=q*nr;swap(r,nr);}assert(r<=1);return(t<0)?t+N:t;*/
	inline ui inv(ui a){return pow(a,N-2);}
public:
    inline Field(int x = 0) : v(x) {}
	inline Field<N> pow(int p){return (*this)^p; }
	inline Field<N> operator^(int p){return {(int)pow(v,(ui)p)};}
    inline Field<N>&operator+=(const Field<N>&o) {if ((ll)v+o.v >= N) v = (ll)v+o.v-N; else v = v+o.v; return *this; }
    inline Field<N>&operator-=(const Field<N>&o) {if (v<o.v) v = N-o.v+v; else v=v-o.v; return *this; }
    inline Field<N>&operator*=(const Field<N>&o) {v=(ull)v*o.v % N; return *this; }
    inline Field<N>&operator/=(const Field<N>&o) { return *this*=inv(o.v); }
    inline Field<N> operator+(const Field<N>&o) {Field<N>r{*this};return r+=o;}
    inline Field<N> operator-(const Field<N>&o) {Field<N>r{*this};return r-=o;}
    inline Field<N> operator*(const Field<N>&o) {Field<N>r{*this};return r*=o;}
    inline Field<N> operator/(const Field<N>&o) {Field<N>r{*this};return r/=o;}
    inline Field<N> operator-() {if(v) return {(int)(N-v)}; else return {0};};
    inline Field<N>& operator++() { ++v; if (v==N) v=0; return *this; }
    inline Field<N> operator++(int) { Field<N>r{*this}; ++*this; return r; }
    inline Field<N>& operator--() { --v; if (v==-1) v=N-1; return *this; }
    inline Field<N> operator--(int) { Field<N>r{*this}; --*this; return r; }
    inline bool operator==(const Field<N>&o) { return o.v==v; }
    inline explicit operator ui() const { return v; }
	inline static vector<Field<N>>fact(int t){vector<Field<N>>F(t+1,1);for(int i=2;i<=t;++i){F[i]=F[i-1]*i;}return F;}
	inline static vector<Field<N>>invfact(int t){vector<Field<N>>F(t+1,1);for(int i=2;i<=t;++i){F[i]=F[i-1]/i;}return F;}
private: ui v;
};
template<unsigned int N>istream &operator>>(std::istream&is,Field<N>&f){unsigned int v;is>>v;f=v;return is;}
template<unsigned int N>ostream &operator<<(std::ostream&os,const Field<N>&f){return os<<(unsigned int)f;}
template<unsigned int N>Field<N> operator+(int i,const Field<N>&f){return Field<N>(i)+f;}
template<unsigned int N>Field<N> operator-(int i,const Field<N>&f){return Field<N>(i)-f;}
template<unsigned int N>Field<N> operator*(int i,const Field<N>&f){return Field<N>(i)*f;}
template<unsigned int N>Field<N> operator/(int i,const Field<N>&f){return Field<N>(i)/f;}

typedef Field<1000000007> FieldMod;

struct Ring {
	static int div(int p, int q, int N) {
		ll t=0,nt=1,r=N,nr=q;
		while(nr){ ll q=r/nr;t-=q*nt;r-=q*nr;swap(t,nt);swap(r,nr); }
		t=(t<0)?t+N:t;
		r=(r<0)?r+N:r;
		if (gcd(p,N)%r) { return 0; }
		return (int)((ll)t*(ll)p/r)%N;
	}
};
#endif

class TaskC {
public:

	void solve(istream& cin, ostream& cout) {
		int N, M; cin >> N >> M;
		if (M == 1) { cout << "1\n0\n"; return; }
		vector<bool> A(M, true);
		for (int i = 0; i < N; ++i) {
			int a; cin >> a; A[a] = false;
		}
		if (N == M-1) {
			for (int i = 0; i < M; ++i) {
				if (A[i]) {
					cout << 1 << '\n' << i << '\n';
					return;
				}
			}
		}

		map<int, vector<int>> V;
		for (int i = 1; i < M; ++i) {
			if (!A[i]) continue;
			V[gcd(M, i)].push_back(i);
		}

		vector<pair<int, vector<int>>> G;
		for (auto it = V.begin(); it != V.end(); ++it) G.push_back(*it);

		size_t GG = G.size();
		vector<vector<int>> E(GG);
		vector<int> S(GG, 0), B(GG, 0), P(GG, -1);
		for (int i = 0; i < GG; ++i) {
			for (int j = 0; j < GG; ++j) {
				if (i!=j && G[j].x%G[i].x==0) {
					E[i].push_back(j);
					S[j]++;
				}
			}
		}

		queue<int> Q;
		for (int i = 0; i < GG; ++i) if (!S[i]) Q.push(i);
		while (!Q.empty()) {
			int q = Q.front(); Q.pop();
			B[q] += G[q].y.size();
			for (int r: E[q]) {
				if (B[q] > B[r]) {
					B[r] = B[q];
					P[r] = q;
				}
				if (--S[r]==0) Q.push(r);
			}
		}

		pii b = {B[0], 0};
		for (int i = 0; i < GG; ++i) { b = max(b, {B[i], i}); }
		int r = b.y;
		deque<int> R;
		while (r != -1) { R.push_front(r); r = P[r]; }

		vector<int> Ans;
		int p = 1;
		for (int r:R) for (int q:G[r].y) Ans.push_back(Ring::div(q, p, M)), p = q;

		if (A[0]) Ans.push_back(0);
		cout << Ans.size() << endl;
		cout << Ans;
	}
};

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	TaskC solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define fi first
#define se second
#define ll long long
#define pq priority_queue
#define mp make_pair
#define pii pair<int,int>
#define mod 998244353

int lowbit(int x) {return x&(-x);}

int n,m;
bool forb[200010];
vector <int> num[200010];
int pre[200010];
int f[200010];
int ans[200010];
int phi;

int pow(int base,int d) {
	int res=1;
	while (d) {
		if (d&1) res=1ll*res*base%m;
		base=1ll*base*base%m;
		d>>=1;
	}
	return res;
}

int solve(int x,int y) {
	return 1ll*pow(x,phi-1)*y%m;
}

int main(){
	memset(pre,-1,sizeof(pre));
	memset(forb,false,sizeof(forb));
	scanf("%d%d",&n,&m);
	for (int i=0;i<n;i++) {
		int x;
		scanf("%d",&x);
		forb[x]=true;
	}
	phi=m;
	int tmp=m;
	for (int i=2;i<=m;i++) {
		if (tmp%i==0) phi=phi/i*(i-1);
		while (tmp%i==0) tmp/=i;
	}
	for (int i=0;i<m;i++) {
		if (forb[i]) continue;
		num[__gcd(i,m)].pb(i);
	}
	for (int i=1;i<=m;i++) {
		if (m%i>0) continue;
		f[i]+=num[i].size();
		for (int j=i*2;j<=m;j+=i) {
			if (m%j>0) continue;
			if (f[j]<f[i]) {
				f[j]=f[i];
				pre[j]=i;
			}
		}
//		cout<<i<<" "<<f[i]<<endl;
	}
	int cnt=0;
	int cur=m;
	while (cur!=-1) {
		for (int i=0;i<num[cur].size();i++)
			ans[cnt++]=num[cur][i];
		cur=pre[cur];
	}
	cout<<cnt<<endl<<ans[cnt-1]<<" ";
	for (int i=cnt-1;i>0;i--) cout<<solve(ans[i],ans[i-1])<<" ";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define fi first
#define se second
#define ll long long
#define pq priority_queue
#define mp make_pair
#define pii pair<int,int>
#define mod 998244353

int lowbit(int x) {return x&(-x);}

int n,m;
bool forb[200010];
vector <int> num[200010];
int pre[200010];
int f[200010];
int ans[200010];
int phi;

int pow(int base,int d) {
	int res=1;
	while (d) {
		if (d&1) res=1ll*res*base%m;
		base=base*base%m;
		d>>=1;
	}
	return res;
}

int solve(int x,int y) {
	return 1ll*pow(x,phi-1)*y%m;
}

int main(){
	memset(pre,-1,sizeof(pre));
	memset(forb,false,sizeof(forb));
	scanf("%d%d",&n,&m);
	for (int i=0;i<n;i++) {
		int x;
		scanf("%d",&x);
		forb[x]=true;
	}
	phi=m;
	int tmp=m;
	for (int i=2;i<=m;i++) {
		if (tmp%i==0) phi=phi/i*(i-1);
		while (tmp%i==0) tmp/=i;
	}
	for (int i=0;i<m;i++) {
		if (forb[i]) continue;
		num[__gcd(i,m)].pb(i);
	}
	for (int i=1;i<=m;i++) {
		if (m%i>0) continue;
		f[i]+=num[i].size();
		for (int j=i*2;j<=m;j+=i) {
			if (m%j>0) continue;
			if (f[j]<f[i]) {
				f[j]=f[i];
				pre[j]=i;
			}
		}
		cout<<i<<" "<<f[i]<<endl;
	}
	int cnt=0;
	int cur=m;
	while (cur!=-1) {
		for (int i=0;i<num[cur].size();i++)
			ans[cnt++]=num[cur][i];
		cur=pre[cur];
	}
	cout<<cnt<<endl<<ans[cnt-1]<<" ";
	for (int i=cnt-1;i>0;i--) cout<<solve(ans[i-1],ans[i])<<" ";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define fi first
#define se second
#define ll long long
#define pq priority_queue
#define mp make_pair
#define pii pair<int,int>
#define mod 998244353

int lowbit(int x) {return x&(-x);}

int n,m;
bool forb[200010];
vector <int> num[200010];
int pre[200010];
int f[200010];
int ans[200010];
int phi;

int pow(int base,int d) {
	int res=1;
	while (d) {
		if (d&1) res=1ll*res*base%m;
		base=base*base%m;
		d>>=1;
	}
	return res;
}

int solve(int x,int y) {
	return 1ll*pow(x,phi-1)*y%m;
}

int main(){
	memset(pre,-1,sizeof(pre));
	memset(forb,false,sizeof(forb));
	scanf("%d%d",&n,&m);
	for (int i=0;i<n;i++) {
		int x;
		scanf("%d",&x);
		forb[x]=true;
	}
	phi=m;
	int tmp=m;
	for (int i=2;i<=m;i++) {
		if (tmp%i==0) phi=phi/i*(i-1);
		while (tmp%i==0) tmp/=i;
	}
	for (int i=0;i<m;i++) {
		if (forb[i]) continue;
		num[__gcd(i,m)].pb(i);
	}
	for (int i=1;i<=m;i++) {
		if (m%i>0) continue;
		f[i]+=num[i].size();
		for (int j=i*2;j<=m;j+=i) {
			if (m%j>0) continue;
			if (f[j]<f[i]) {
				f[j]=f[i];
				pre[j]=i;
			}
		}
//		cout<<i<<" "<<f[i]<<endl;
	}
	int cnt=0;
	int cur=m;
	while (cur!=-1) {
		for (int i=0;i<num[cur].size();i++)
			ans[cnt++]=num[cur][i];
		cur=pre[cur];
	}
	cout<<cnt<<endl<<ans[cnt-1]<<" ";
	for (int i=cnt-1;i>0;i--) cout<<solve(ans[i-1],ans[i])<<" ";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define fi first
#define se second
#define ll long long
#define pq priority_queue
#define mp make_pair
#define pii pair<int,int>
#define mod 998244353

int lowbit(int x) {return x&(-x);}

int n,m;
bool forb[200010];
vector <int> num[200010];
int pre[200010];
int f[200010];
int ans[200010];
int phi;

int pow(int base,int d) {
	int res=1;
	while (d) {
		if (d&1) res=1ll*res*base%m;
		base=1ll*base*base%m;
		d>>=1;
	}
	return res;
}

int solve(int x,int y) {
	int tmp=__gcd(x,y);
	x/=tmp,y/=tmp;
	return 1ll*pow(x,phi-1)*y%m;
}

int main(){
	memset(pre,-1,sizeof(pre));
	memset(forb,false,sizeof(forb));
	scanf("%d%d",&n,&m);
	for (int i=0;i<n;i++) {
		int x;
		scanf("%d",&x);
		forb[x]=true;
	}
	phi=m;
	int tmp=m;
	for (int i=2;i<=m;i++) {
		if (tmp%i==0) phi=phi/i*(i-1);
		while (tmp%i==0) tmp/=i;
	}
	for (int i=0;i<m;i++) {
		if (forb[i]) continue;
		num[__gcd(i,m)].pb(i);
	}
	for (int i=1;i<=m;i++) {
		if (m%i>0) continue;
		f[i]+=num[i].size();
		for (int j=i*2;j<=m;j+=i) {
			if (m%j>0) continue;
			if (f[j]<f[i]) {
				f[j]=f[i];
				pre[j]=i;
			}
		}
//		cout<<i<<" "<<f[i]<<endl;
	}
	int cnt=0;
	int cur=m;
	while (cur!=-1) {
		for (int i=0;i<num[cur].size();i++)
			ans[cnt++]=num[cur][i];
		cur=pre[cur];
	}
	cout<<cnt<<endl<<ans[cnt-1]<<" ";
	for (int i=cnt-1;i>0;i--) cout<<solve(ans[i],ans[i-1])<<" ";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define fi first
#define se second
#define ll long long
#define pq priority_queue
#define mp make_pair
#define pii pair<int,int>
#define mod 998244353

int lowbit(int x) {return x&(-x);}

int n,m;
bool forb[200010];
vector <int> num[200010];
int pre[200010];
int f[200010];
int ans[200010];
int phi;

int pow(int base,int d) {
	int res=1;
	while (d) {
		if (d&1) res=1ll*res*base%m;
		base=base*base%m;
		d>>=1;
	}
	return res;
}

int solve(int x,int y) {
	return 1ll*pow(x,phi-1)*y%m;
}

int main(){
	memset(pre,-1,sizeof(pre));
	memset(forb,false,sizeof(forb));
	scanf("%d%d",&n,&m);
	for (int i=0;i<n;i++) {
		int x;
		scanf("%d",&x);
		forb[x]=true;
	}
	phi=m;
	int tmp=m;
	for (int i=2;i<=m;i++) {
		if (tmp%i==0) phi=phi/i*(i-1);
		while (tmp%i==0) tmp/=i;
	}
	for (int i=0;i<m;i++) {
		if (forb[i]) continue;
		num[__gcd(i,m)].pb(i);
	}
	for (int i=1;i<=m;i++) {
		if (m%i>0) continue;
		f[i]+=num[i].size();
		for (int j=i*2;j<=m;j+=i) {
			if (m%j>0) continue;
			if (f[j]<f[i]) {
				f[j]=f[i];
				pre[j]=i;
			}
		}
//		cout<<i<<" "<<f[i]<<endl;
	}
	int cnt=0;
	int cur=m;
	while (cur!=-1) {
		for (int i=0;i<num[cur].size();i++)
			ans[cnt++]=num[cur][i];
		cur=pre[cur];
	}
	cout<<cnt<<endl<<ans[cnt-1]<<" ";
	for (int i=cnt-1;i>0;i--) cout<<solve(ans[i],ans[i-1])<<" ";
	return 0;
}


c#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read(){
	ll a=0,b=getchar(),c=1;
	while(!isdigit(b))c=b=='-'?-1:1,b=getchar();
	while(isdigit(b))a=a*10+b-'0',b=getchar();
	return a*c;
}
int gcd(int a,int b){
	return b==0?a:gcd(b,a%b);
}
void gao(ll a,ll b,ll &x,ll &y){
	if(b==0){
		x=1,y=0;
		return ;
	}
	gao(b,a%b,x,y);
	ll t=x;
	x=y,y=t-a/b*y;
}
const int N=2e5+5;
int n,m,b[N],g[N],c[N],p[N];
int main(){
	n=read(),m=read();
	for(int i=1;i<=n;i++)
		b[read()]=1;
	for(int i=1;i<m;i++)
		g[i]=gcd(i,m),c[g[i]]+=b[i]==0;
	for(int i=m-1;i>0;i--){
		for(int j=i+i;j<m;j+=i)
			if(c[j]>c[p[i]])p[i]=j;
		c[i]+=c[p[i]];
	}
	c[1]+=b[0]==0;
	printf("%d\n",c[1]);
	ll a=1,pre=1;
	for(int k=1;k;k=p[k])
		for(int i=1;i<m;i++)
			if(g[i]==k and !b[i]){
				ll x,y;
				gao(a,m,x,y);
				x=((x%m+m)%m*i/pre)%m;
				pre=k,a=a*x%m;
				printf("%lld ",x);
			}
	if(b[0]==0)puts("0");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read(){
	ll a=0,b=getchar(),c=1;
	while(!isdigit(b))c=b=='-'?-1:1,b=getchar();
	while(isdigit(b))a=a*10+b-'0',b=getchar();
	return a*c;
}
int gcd(int a,int b){
	return b==0?a:gcd(b,a%b);
}
void gao(ll a,ll b,ll &x,ll &y){
	if(b==0){
		x=1,y=0;
		return ;
	}
	gao(b,a%b,x,y);
	ll t=x;
	x=y,y=t-a/b*y;
}
const int N=2e5+5;
int n,m,b[N],g[N],c[N],p[N];
int main(){
	n=read(),m=read();
	for(int i=1;i<=n;i++)
		b[read()]=1;
	for(int i=1;i<m;i++)
		g[i]=gcd(i,m),c[g[i]]+=b[i]==0;
	for(int i=m-1;i>0;i--){
		for(int j=i+i;j<m;j+=i)
			if(c[j]>c[p[i]])p[i]=j;
		c[i]+=c[p[i]];
	}
	c[1]+=b[0]==0;
	printf("%d\n",c[1]);
	ll a=1,pre=1;
	for(int k=1;k;k=p[k])
		for(int i=1;i<m;i++)
			if(g[i]==k and !b[i]){
				ll x,y;
				gao(a,m,x,y);
				x=((x%m+m)%m*i/pre)%m;
				pre=k,a=a*x%m;
				printf("%lld ",x);
			}
	if(b[0]==0)puts("0");
	return 0;
}


#ifndef __clang__
#pragma GCC optimize "-O3"
#pragma GCC target "tune=native"
#endif
#include <stdio.h>
#include <bits/stdc++.h>

#define DESTRUCT2(p, a, b)                      \
  auto a = get<0>(p);                           \
  auto b = get<1>(p);

#define DESTRUCT3(p, a, b, c)                   \
  auto a = get<0>(p);                           \
  auto b = get<1>(p);                           \
  auto c = get<2>(p);

#define DESTRUCT4(p, a, b, c, d)                \
  auto a = get<0>(p);                           \
  auto b = get<1>(p);                           \
  auto c = get<2>(p);                           \
  auto d = get<3>(p);

#define FOR(i, n)     for(lli i = 0; i < (lli)(n); ++i)
#define FORU(i, j, k) for(lli i = (j); i <= (lli)(k); ++i)
#define FORD(i, j, k) for(lli i = (j); i >= (lli)(k); --i)

#define SQ(x) ((x)*(x))

#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back

using namespace std;

template<typename... As>
struct tpl : public std::tuple<As...> {
  using std::tuple<As...>::tuple;

  template<typename T = tuple<As...> >
  typename tuple_element<0, T>::type const&
  x() const { return get<0>(*this); }
  template<typename T = tuple<As...> >
  typename tuple_element<0, T>::type&
  x() { return get<0>(*this); }

  template<typename T = tuple<As...> >
  typename tuple_element<1, T>::type const&
  y() const { return get<1>(*this); }
  template<typename T = tuple<As...> >
  typename tuple_element<1, T>::type&
  y() { return get<1>(*this); }

  template<typename T = tuple<As...> >
  typename tuple_element<2, T>::type const&
  z() const { return get<2>(*this); }
  template<typename T = tuple<As...> >
  typename tuple_element<2, T>::type&
  z() { return get<2>(*this); }

  template<typename T = tuple<As...> >
  typename tuple_element<3, T>::type const&
  w() const { return get<3>(*this); }
  template<typename T = tuple<As...> >
  typename tuple_element<3, T>::type&
  w() { return get<3>(*this); }
};

using lli   = long long int;
using llu   = long long unsigned;

using pii   = tpl<lli, lli>;
using piii  = tpl<lli, lli, lli>;
using piiii = tpl<lli, lli, lli, lli>;
using vi    = vector<lli>;
using vii   = vector<pii>;
using viii  = vector<piii>;
using vvi   = vector<vi>;
using vvii  = vector<vii>;
using vviii = vector<viii>;

template<class T>
using min_queue = priority_queue<T, vector<T>, greater<T> >;
template<class T>
using max_queue = priority_queue<T>;

template<size_t... I>
struct my_index_sequence {
  using type = my_index_sequence;
  static constexpr array<size_t, sizeof...(I)> value = { {I...} };
};

namespace my_index_sequence_detail {
  template<typename I, typename J> struct concat;
  template<size_t... I, size_t... J>
  struct concat<my_index_sequence<I...>, my_index_sequence<J...> > :
    my_index_sequence<I..., (sizeof...(I)+J)...> { };
  template<size_t N> struct make_index_sequence :
    concat<typename make_index_sequence<N/2>::type, typename make_index_sequence<N-N/2>::type>::type { };
  template <> struct make_index_sequence<0> : my_index_sequence<>{};
  template <> struct make_index_sequence<1> : my_index_sequence<0>{};
}

template<class... A>
using my_index_sequence_for = typename my_index_sequence_detail::make_index_sequence<sizeof...(A)>::type;

template<class T, size_t... I>
void print_tuple(ostream& s, T const& a, my_index_sequence<I...>){
  using swallow = int[];
  (void)swallow{0, (void(s << (I == 0? "" : ", ") << get<I>(a)), 0)...};
}

template<class T>
ostream& print_collection(ostream& s, T const& a);
template<class... A>
ostream& operator<<(ostream& s, tpl<A...> const& a);
template<class... A>
ostream& operator<<(ostream& s, tuple<A...> const& a);
template<class A, class B>
ostream& operator<<(ostream& s, pair<A, B> const& a);

template<class T, size_t I>
ostream& operator<<(ostream& s, array<T, I> const& a) { return print_collection(s, a); }
template<class T>
ostream& operator<<(ostream& s, vector<T> const& a) { return print_collection(s, a); }
template<class T, class U>
ostream& operator<<(ostream& s, multimap<T, U> const& a) { return print_collection(s, a); }
template<class T>
ostream& operator<<(ostream& s, multiset<T> const& a) { return print_collection(s, a); }
template<class T, class U>
ostream& operator<<(ostream& s, map<T, U> const& a) { return print_collection(s, a); }
template<class T>
ostream& operator<<(ostream& s, set<T> const& a) { return print_collection(s, a); }

template<class T>
ostream& print_collection(ostream& s, T const& a){
  s << '[';
  for(auto it = begin(a); it != end(a); ++it){
    s << *it;
    if(it != prev(end(a))) s << " ";
  }
  return s << ']';
}

template<class... A>
ostream& operator<<(ostream& s, tpl<A...> const& a){
  s << '(';
  print_tuple(s, a, my_index_sequence_for<A...>{});
  return s << ')';
}

template<class... A>
ostream& operator<<(ostream& s, tuple<A...> const& a){
  s << '(';
  print_tuple(s, a, my_index_sequence_for<A...>{});
  return s << ')';
}

template<class A, class B>
ostream& operator<<(ostream& s, pair<A, B> const& a){
  return s << "(" << get<0>(a) << ", " << get<1>(a) << ")";
}

namespace std {
  namespace {
    template <class T>
    inline void hash_combine(size_t& seed, T const& v) {
      seed ^= hash<T>()(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
    template <class Tuple, size_t Index = tuple_size<Tuple>::value - 1>
    struct HashValueImpl {
      static void apply(size_t& seed, Tuple const& tuple) {
        HashValueImpl<Tuple, Index-1>::apply(seed, tuple);
        hash_combine(seed, get<Index>(tuple));
      }
    };
    template <class Tuple>
    struct HashValueImpl<Tuple, 0> {
      static void apply(size_t& seed, Tuple const& tuple) {
        hash_combine(seed, get<0>(tuple));
      }
    };
  }
  template <typename ... TT>
  struct hash<tuple<TT...>> {
    size_t operator()(tuple<TT...> const& tt) const {
      size_t seed = 0;
      HashValueImpl<tuple<TT...> >::apply(seed, tt);
      return seed;
    }
  };
  template <typename ... TT>
  struct hash<tpl<TT...>> {
    size_t operator()(tpl<TT...> const& tt) const {
      size_t seed = 0;
      HashValueImpl<tuple<TT...> >::apply(seed, tt);
      return seed;
    }
  };
}

lli read_positive(){
  char c; lli x=0;
  do { c = getchar(); } while(c<'0' || c>'9');
  while(c>='0'&&c<='9') {
    x=10*x+(c-'0');
    c = getchar();
  }
  return x;
}

//------------------------------------------------------------------------------

// --- BEGIN SNIPPET MODULAR ---

inline lli fexp(lli a, lli b, lli m) {
  lli r = 1;
  while(b){
    if(b&1) r = (r*a)%m;
    a = (a*a)%m;
    b /= 2;
  }
  return r;
}

inline lli invmod(lli a, lli m) {
  return fexp(a, m-2, m);
}

pii egcd(lli a, lli b) {
  if(a%b==0) {
    return mt(0,1);
  }else{
    pii p = egcd(b,a%b);
    int u=p.x(), v=p.y();
    // u*b + v*r = g
    // a = q*b + r
    // r = a-q*b
    // u*a + v*(a-q*b) = g
    // (u+v)*a - (q*v)*b = g
    return mt(v, u-(a/b)*v);
  }
}

// --- END SNIPPET MODULAR ---

int solveInv(lli a, lli b, lli m) {
  // solve x*a = b [m]
  if(b==0) return 0;
  pii p = egcd(a,m);
  lli g=__gcd(m,a);
  //cerr << b << " " << a << " " << m << " " << p << " " << (((b/g)*p.x())%m+m)%m << endl;
  return (((b/g)*p.x())%m+m)%m;
}

const int N=2e5+3;
bool E[N];
int main(){
  ios::sync_with_stdio(0); cin.tie(0);

  srand(time(0));

#ifdef LOCAL
  while(1) {
#endif
    int n,m;
#ifndef LOCAL
    cin>>n>>m;
    FOR(i,m) E[i]=0;
    FOR(i,n) {
      int x; cin>>x; E[x]=1;
    }
#else
    m=10+rand()%200000;
    FOR(i,m) E[i]=rand()%2;
    n=0; FOR(i,m) if(E[i]) n++;
#endif

    map<int,vi> M;
    FOR(x,m) if(E[x] == 0) {
      int g=__gcd<int>(m,x);
      if(x==0) g=0;
      M[g].pb(x);
    }
    map<int,vi> G;
    for(auto const& p:M) for(auto const& q:M) if(p.first!=q.first&&q.first!=0&&p.first%q.first==0) {
          G[q.first].pb(p.first);
        }
    map<int,int> SZ;
    function<void(int)> dfs = [&](int i){
      int bc=0;
      for(int j:G[i]) { dfs(j); bc=max(bc,SZ[j]); }
      SZ[i]=bc+M[i].size();
    };
    for(auto const& p:M) if(SZ.count(p.first)==0) {
        dfs(p.first);
      }
    int best=0, bestVal=-1;
    for(auto const& p:SZ) if(p.second>bestVal) { best=p.first; bestVal=p.second; }

    vi collect;
    function<void(int)> dfs2 = [&](int i) {
      int best=-1, bestVal=-1;
      for(int j:G[i]) if(SZ[j]>bestVal) { bestVal=SZ[j]; best=j; }
      if(best!=-1) dfs2(best);
      collect.insert(end(collect), all(M[i]));
    };
    dfs2(best);
    reverse(all(collect));
    if((int)collect.size() != bestVal) {
      cout << collect.size() << ' ' << bestVal << endl;
      cout << n << " " << m << endl;
      cout << M << endl;
      cout << SZ << endl;
      exit(1);
    }
    lli cur=1;
    cout << collect.size() << endl;
    for(int x:collect) {
      int y=solveInv(cur,x,m);
      (cur*=y)%=m;
#ifndef LOCAL
      cout << y << ' ';
#endif
      if(cur!=x) exit(1);
      assert(cur==x);
    }
    cout << endl;
#ifdef LOCAL
  }
#endif

  return 0;
}