#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll exgcd(ll a, ll b, ll& x, ll& y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  ll gcd = exgcd(b, a % b, y, x);
  y -= a / b * x;
  return gcd;
}

int main(int argc, const char* argv[]) {
  cin.sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<vector<int>> b(m);
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    a[t]++;
  }
  for (int i = 1; i < m; i++) {
    if (!a[i]) {
      b[__gcd(i, m)].push_back(i);
    }
  }
  vector<vector<int>> g(m);
  for (int i = 1; i < m; i++) {
    if (!b[i].empty()) {
      for (int j = i + i; j < m; j += i) {
        if (!b[j].empty()) {
          g[i].push_back(j);
        }
      }
    }
  }
  vector<int> dp(m), pre(m);
  int p = m - 1;
  for (int i = m - 1; i >= 1; i--) {
    dp[i] = b[i].size();
    for (auto v : g[i]) {
      if (dp[v] + b[i].size() > dp[i]) {
        dp[i] = dp[v] + b[i].size();
        pre[i] = v;
      }
    }
    if (dp[i] > dp[p]) {
      p = i;
    }
  }
  vector<int> ans;
  for (int i = p; i; i = pre[i]) {
    for (auto x : b[i]) {
      ans.push_back(x);
    }
  }
  if (!ans.empty()) {
    ll now = ans[0];
    for (int i = 1; i < ans.size(); i++) {
      ll x = 0, y = 0;
      int g = exgcd(now, m, x, y);
      x *= (ans[i] / g);
      x = (x % m + m) % m;
      ans[i] = x;
      now = now * x % m;
    }
  }
  if (!a[0]) {
    ans.push_back(0);
  }
  for (auto x : ans) {
    cout << x << " ";
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll exgcd(ll a, ll b, ll& x, ll& y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  ll gcd = exgcd(b, a % b, y, x);
  y -= a / b * x;
  return gcd;
}

int main(int argc, const char* argv[]) {
  cin.sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<vector<int>> b(m);
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    a[t]++;
  }
  for (int i = 1; i < m; i++) {
    if (!a[i]) {
      b[__gcd(i, m)].push_back(i);
    }
  }
  vector<vector<int>> g(m);
  for (int i = 1; i < m; i++) {
    if (!b[i].empty()) {
      for (int j = i + i; j < m; j += i) {
        if (!b[j].empty()) {
          g[i].push_back(j);
        }
      }
    }
  }
  vector<int> dp(m), pre(m);
  int p = m - 1;
  for (int i = m - 1; i >= 1; i--) {
    dp[i] = b[i].size();
    for (auto v : g[i]) {
      if (dp[v] + b[i].size() > dp[i]) {
        dp[i] = dp[v] + b[i].size();
        pre[i] = v;
      }
    }
    if (dp[i] > dp[p]) {
      p = i;
    }
  }
  vector<int> ans;
  for (int i = p; i; i = pre[i]) {
    for (auto x : b[i]) {
      ans.push_back(x);
    }
  }
  if (!ans.empty()) {
    ll now = ans[0];
    for (int i = 1; i < ans.size(); i++) {
      ll x = 0, y = 0;
      int g = exgcd(now, m, x, y);
      x *= (ans[i] / g);
      x = (x % m + m) % m;
      ans[i] = x;
      now = now * x % m;
    }
  }
  if (!a[0]) {
    ans.push_back(0);
  }
  cout << ans.size() << endl;
  for (auto x : ans) {
    cout << x << " ";
  }
  return 0;
}


#include <cstdio>
#include <vector>
#include <algorithm>

typedef long long i64;

const int N = 200000 + 10;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int m;

bool flag[N];

void init() {
  int n;
  scanf("%d%d", &n, &m);
  while (n--) {
    int a;
    scanf("%d", &a);
    if (a == 0) a = m;
    flag[a] = true;
  }
}

void exgcd(int a, int b, int &x, int &y) {
  if (!b) {
    x = 1, y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= a / b * x;
}

int f[N], mem[N];
std::pair<int, int> g[N];

std::vector<int> ans;

int solve(int a, int b) {
  //  b * x \equiv a (mod m)
  int x, y;
  exgcd(b, m, x, y);
  return (i64)a / mem[b] * x % m;
}

int main() {
  init();
  static int d[N], pos[N];
  int tot = 0;
  for (int i = 1; i <= m; ++i) if (m % i == 0) d[++tot] = i, pos[i] = tot;
  for (int i = 1; i <= m; ++i) mem[i] = gcd(i, m);
  for (int i = 1; i <= m; ++i) if (!flag[i]) ++f[pos[mem[i]]];
  for (int i = 1; i <= tot; ++i)
    for (int j = i + 1; j <= tot; ++j)
      if (d[j] % d[i] == 0) g[j] = std::max(g[j], std::make_pair(g[i].first + f[i], i));
  for (int i = tot; i; i = g[i].second)
    for (int j = 1; j <= m; ++j)
      if (!flag[j] && pos[mem[j]] == i) ans.push_back(j);
  std::reverse(ans.begin(), ans.end());
  printf("%d\n", ans.size());
  for (int i = 0; i < ans.size(); ++i) printf("%d ", (solve(ans[i], i ? ans[i - 1] : 1) % m + m) % m);
  return 0;
}


#include <cstdio>
#include <vector>
#include <algorithm>

typedef long long i64;

const int N = 200000 + 10;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int m;

bool flag[N];

void init() {
  int n;
  scanf("%d%d", &n, &m);
  while (n--) {
    int a;
    scanf("%d", &a);
    if (a == 0) a = m;
    flag[a] = true;
  }
}

void exgcd(int a, int b, i64 &x, i64 &y) {
  if (!b) {
    x = 1, y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= a / b * x;
}

int f[N], mem[N];
std::pair<int, int> g[N];

std::vector<int> ans;

int inv(int a, int mod) {
  a %= mod;
  i64 x, y;
  exgcd(a, mod, x, y);
  return x % mod;
}

int solve(int a, int b) {
  int x = mem[a], y = m / mem[a];
  int res = (i64)a * inv(b, y) % y;
  res = (i64)res * x % m * inv(x, y) % m;
  return res;
}

int main() {
  init();
  static int d[N], pos[N];
  int tot = 0;
  for (int i = 1; i <= m; ++i) if (m % i == 0) d[++tot] = i, pos[i] = tot;
  for (int i = 1; i <= m; ++i) mem[i] = gcd(i, m);
  for (int i = 1; i <= m; ++i) if (!flag[i]) ++f[pos[mem[i]]];
  for (int i = 1; i <= tot; ++i)
    for (int j = i + 1; j <= tot; ++j)
      if (d[j] % d[i] == 0) g[j] = std::max(g[j], std::make_pair(g[i].first + f[i], i));
  for (int i = tot; i; i = g[i].second)
    for (int j = 1; j <= m; ++j)
      if (!flag[j] && pos[mem[j]] == i) ans.push_back(j);
  std::reverse(ans.begin(), ans.end());
  printf("%d\n", ans.size());
  for (int i = 0; i < ans.size(); ++i) printf("%d ", (solve(ans[i], i ? ans[i - 1] : 1) % m + m) % m);
  return 0;
}


#include <cstdio>
#include <vector>
#include <algorithm>

typedef long long i64;

const int N = 200000 + 10;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int m;

bool flag[N];

void init() {
  int n;
  scanf("%d%d", &n, &m);
  while (n--) {
    int a;
    scanf("%d", &a);
    if (a == 0) a = m;
    flag[a] = true;
  }
}

void exgcd(int a, int b, int &x, int &y) {
  if (!b) {
    x = 1, y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= a / b * x;
}

int f[N], mem[N];
std::pair<int, int> g[N];

std::vector<int> ans;

int inv(int a, int mod) {
  int x, y;
  exgcd(a, mod, x, y);
  return x;
}

int solve(int a, int b) {
  int x = mem[a], y = mem[b];
  a /= x, b /= y;
  int res = x / y;
  res = (i64)res * inv(b, m) % m * a % m;
  return res;
}

int main() {
  init();
  static int d[N], pos[N];
  int tot = 0;
  for (int i = 1; i <= m; ++i) if (m % i == 0) d[++tot] = i, pos[i] = tot;
  for (int i = 1; i <= m; ++i) mem[i] = gcd(i, m);
  for (int i = 1; i <= m; ++i) if (!flag[i]) ++f[pos[mem[i]]];
  for (int i = 1; i <= tot; ++i)
    for (int j = i + 1; j <= tot; ++j)
      if (d[j] % d[i] == 0) g[j] = std::max(g[j], std::make_pair(g[i].first + f[i], i));
  for (int i = tot; i; i = g[i].second)
    for (int j = 1; j <= m; ++j)
      if (!flag[j] && pos[mem[j]] == i) ans.push_back(j);
  std::reverse(ans.begin(), ans.end());
  printf("%d\n", ans.size());
  for (int i = 0; i < ans.size(); ++i) printf("%d ", (solve(ans[i], i ? ans[i - 1] : 1) % m + m) % m);
  return 0;
}


#include <cstdio>
#include <vector>
#include <algorithm>

typedef long long i64;

const int N = 200000 + 10;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int m;

bool flag[N];

void init() {
  int n;
  scanf("%d%d", &n, &m);
  while (n--) {
    int a;
    scanf("%d", &a);
    if (a == 0) a = m;
    flag[a] = true;
  }
}

void exgcd(int a, int b, i64 &x, i64 &y) {
  if (!b) {
    x = 1, y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= a / b * x;
}

int f[N], mem[N];
std::pair<int, int> g[N];

std::vector<int> ans;

int inv(int a, int mod) {
  i64 x, y;
  exgcd(a, mod, x, y);
  return x % mod;
}

int solve(int a, int b) {
  int x = mem[a], y = m / mem[a];
  int res = (i64)a * inv(b, y) % y;
  res = (i64)res * x % m * inv(x, y) % m;
  return res;
}

int main() {
  init();
  static int d[N], pos[N];
  int tot = 0;
  for (int i = 1; i <= m; ++i) if (m % i == 0) d[++tot] = i, pos[i] = tot;
  for (int i = 1; i <= m; ++i) mem[i] = gcd(i, m);
  for (int i = 1; i <= m; ++i) if (!flag[i]) ++f[pos[mem[i]]];
  for (int i = 1; i <= tot; ++i)
    for (int j = i + 1; j <= tot; ++j)
      if (d[j] % d[i] == 0) g[j] = std::max(g[j], std::make_pair(g[i].first + f[i], i));
  for (int i = tot; i; i = g[i].second)
    for (int j = 1; j <= m; ++j)
      if (!flag[j] && pos[mem[j]] == i) ans.push_back(j);
  std::reverse(ans.begin(), ans.end());
  printf("%d\n", ans.size());
  for (int i = 0; i < ans.size(); ++i) printf("%d ", (solve(ans[i], i ? ans[i - 1] : 1) % m + m) % m);
  return 0;
}


#include <cstdio>
#include <vector>
#include <algorithm>

typedef long long i64;

const int N = 200000 + 10;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int m;

bool flag[N];

void init() {
  int n;
  scanf("%d%d", &n, &m);
  while (n--) {
    int a;
    scanf("%d", &a);
    if (a == 0) a = m;
    flag[a] = true;
  }
}

void exgcd(int a, int b, int &x, int &y) {
  if (!b) {
    x = 1, y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= a / b * x;
}

int f[N], mem[N];
std::pair<int, int> g[N];

std::vector<int> ans;

int inv(int a, int mod) {
  int x, y;
  exgcd(a, mod, x, y);
  return x;
}

int solve(int a, int b) {
  int x = mem[a], y = m / mem[a];
  int res = (i64)a * inv(b, y) % y;
  res = (i64)res * x % m * inv(x, y) % m;
  return res;
}

int main() {
  init();
  static int d[N], pos[N];
  int tot = 0;
  for (int i = 1; i <= m; ++i) if (m % i == 0) d[++tot] = i, pos[i] = tot;
  for (int i = 1; i <= m; ++i) mem[i] = gcd(i, m);
  for (int i = 1; i <= m; ++i) if (!flag[i]) ++f[pos[mem[i]]];
  for (int i = 1; i <= tot; ++i)
    for (int j = i + 1; j <= tot; ++j)
      if (d[j] % d[i] == 0) g[j] = std::max(g[j], std::make_pair(g[i].first + f[i], i));
  for (int i = tot; i; i = g[i].second)
    for (int j = 1; j <= m; ++j)
      if (!flag[j] && pos[mem[j]] == i) ans.push_back(j);
  std::reverse(ans.begin(), ans.end());
  printf("%d\n", ans.size());
  for (int i = 0; i < ans.size(); ++i) printf("%d ", (solve(ans[i], i ? ans[i - 1] : 1) % m + m) % m);
  //  for (int i = 0; i < ans.size(); ++i) fprintf(stderr, "%d ", ans[i]);
  return 0;
}


// In the name of Allah
#include<bits/stdc++.h>
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define ll long long
#define ld long double

using namespace std;

const int MAXN = 3e5 + 10;
vector<int> a[MAXN], ans;
bool mark[MAXN];
int dp[MAXN], par[MAXN], phi[MAXN];

ll power(ll x, ll y, ll mod){
	if(y == 0) return 1;
	ll mid = power(x, y / 2, mod);
	if(y & 1) return mid * mid % mod * x % mod;
	else return mid * mid % mod;
}

int main(){
	ios::sync_with_stdio(false); cin.tie(0);
	int n, m, num;
	cin >> n >> m;
	for(int i = 1; i <= m; i ++){
		phi[i] += i;
		for(int j = 2 * i; j <= m; j += i)
			phi[j] -= phi[i];
	}
	for(int i = 0; i < n; i ++) cin >> num, mark[num] = true;
	for(int i = 0; i < m; i ++){
		if(mark[i] == true) continue;
		a[__gcd(i, m)].pb(i);
	}
	fill(par, par + MAXN, -1);
	for(int i = m; i > 0; i --){
		dp[i] = a[i].size();
		for(int j = 2; i * j <= m; j ++){
			if((int)a[i].size() + dp[i * j] > dp[i]){
				dp[i] = (int)a[i].size() + dp[i * j];
				par[i] = i * j;
			}
		}
		reverse(a[i].begin(), a[i].end());
	}
	int x = 1, last = -1, tmp = 1;
	while(x != -1){
		bool is = false;
		for(auto u : a[x]){
			if(ans.size() == 0)
				ans.pb(u), is = true;
			else if(is == false){
				ll val, val2 = __gcd(u, m) / __gcd(last, m);
				val = val2 * last % m;

				ans.pb(u / x * power(val / x, phi[m / x] - 1, m / x) % (m / x) * val2 % m);
			}
			else{
				ll val = u / x * power(last / x, phi[m / x] - 1, m / x) % (m / x);
				ans.pb(val);
			}
			is = true;
			last = u;
		}
		x = par[x];
	}
	cout << endl;
	cout << ans.size() << endl;
	for(auto u : ans)
		cout << u << ' ';
	cout << endl;
	return 0;
}


// In the name of Allah
#include<bits/stdc++.h>
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define ll long long
#define ld long double

using namespace std;

const int MAXN = 3e5 + 10;
vector<int> a[MAXN], ans;
bool mark[MAXN];
int dp[MAXN], par[MAXN], phi[MAXN];

ll power(ll x, ll y, ll mod){
	if(y == 0) return 1;
	ll mid = power(x, y / 2, mod);
	if(y & 1) return mid * mid % mod * x % mod;
	else return mid * mid % mod;
}

int main(){
	ios::sync_with_stdio(false); cin.tie(0);
	int n, m, num;
	cin >> n >> m;
	for(int i = 1; i <= m; i ++){
		phi[i] += i;
		for(int j = 2 * i; j <= m; j += i)
			phi[j] -= phi[i];
	}
	for(int i = 0; i < n; i ++) cin >> num, mark[num] = true;
	for(int i = 0; i < m; i ++){
		if(mark[i] == true) continue;
		a[__gcd(i, m)].pb(i);
	}
	fill(par, par + MAXN, -1);
	for(int i = m; i > 0; i --){
		dp[i] = a[i].size();
		for(int j = 2; i * j <= m; j ++){
			if((int)a[i].size() + dp[i * j] > dp[i]){
				dp[i] = (int)a[i].size() + dp[i * j];
				par[i] = i * j;
			}
		}
		reverse(a[i].begin(), a[i].end());
	}
	int x = 1, last = -1, lastg = -1;
	while(x != -1){
		bool is = false;
		for(auto u : a[x]){
			if(ans.size() == 0)
				ans.pb(u), is = true;
			else if(is == false){
				ans.pb(u / lastg * power(last, phi[m / lastg] - 1, m / lastg) % (m / lastg));
			}
			else{
				ll val = u / x * power(last / x, phi[m / x] - 1, m / x) % (m / x);
				ans.pb(val);
			}
			is = true;
			last = u;
			lastg = x;
		}
		x = par[x];
	}
	cout << ans.size() << endl;
	for(auto u : ans)
		cout << u << ' ';
	cout << endl;
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<vector>
#define ll long long
using namespace std;
const int maxn=200010, inf=1e9;
struct edge{int too, pre;}e[maxn*20];
int n, m, x, tot, d, now;
int flag[maxn], deg[maxn], last[maxn], st[maxn], dist[maxn], cnt[maxn], nxt[maxn];
vector<int>v[maxn];

template<typename T>
inline void read(T &k)
{
	int f=1; k=0; char c=getchar();
	while(c<'0' || c>'9') c=='-'&&(f=-1), c=getchar();
	while(c<='9' && c>='0') k=k*10+c-'0', c=getchar();
	k*=f;
}

int gcd(int a, int b){return b?gcd(b, a%b):a;}

inline void add(int x, int y){deg[y]++; e[++tot]=(edge){y, last[x]}; last[x]=tot;}

inline void toposort()
{
	int top=0;
	for(int i=1;i<m;i++) if(!deg[i]) st[++top]=i;
	for(int i=1;i<m;i++) dist[i]=cnt[i];
	while(top)
	{
		int now=st[top--];
		for(int i=last[now], too;i;i=e[i].pre)
		{
			deg[too=e[i].too]--;
			if(!deg[too]) st[++top]=too;
			if(dist[too]<dist[now]+cnt[too])
			{
				nxt[too]=now;
				dist[too]=dist[now]+cnt[too];
			}
		}
	}
}

int exgcd(int a, int b, int &x, int &y)
{
	if(!b) return x=1, y=0, a;
	int d=exgcd(b, a%b, x, y);
	int tmp=x; x=y; y=tmp-a/b*y;
	return d;
}

inline int calc(int pre, int now)
{
	int x, y;
	int d=exgcd(pre, m, x, y);
	int b=m/d;
	x=((x%b)+b)%b; x*=now/d;
	return x%m;

}

int main()
{
	read(n); read(m);
	for(int i=1;i<=n;i++) read(x), flag[x]=1;
	for(int i=1;i<m;i++)
	if(!flag[i]) cnt[d=gcd(i, m)]++, v[d].push_back(i);
	for(int i=1;i<m;i++)
	{
		for(int j=i+i;j<m;j+=i)
		if(cnt[j]) add(j, i);
	}
	toposort(); int ans=0;
	for(int i=1;i<m;i++)
	if(dist[i]>ans) ans=dist[i], now=i;
	printf("%d\n", ans+(!flag[0]));
	int pre=1;
	while(now)
	{
		for(int i=0;i<v[now].size();i++)
		printf("%d ", calc(pre, v[now][i])), pre=v[now][i];
		now=nxt[now];
	}
	if(!flag[0]) puts("0");
}


#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<vector>
#define ll long long
using namespace std;
const int maxn=200010, inf=1e9;
struct edge{int too, pre;}e[maxn*20];
int n, m, x, tot, d, now;
int flag[maxn], deg[maxn], last[maxn], st[maxn], dist[maxn], cnt[maxn], nxt[maxn];
vector<int>v[maxn];

template<typename T>
inline void read(T &k)
{
	int f=1; k=0; char c=getchar();
	while(c<'0' || c>'9') c=='-'&&(f=-1), c=getchar();
	while(c<='9' && c>='0') k=k*10+c-'0', c=getchar();
	k*=f;
}

int gcd(int a, int b){return b?gcd(b, a%b):a;}

inline void add(int x, int y){deg[y]++; e[++tot]=(edge){y, last[x]}; last[x]=tot;}

inline void toposort()
{
	int top=0;
	for(int i=1;i<m;i++) if(!deg[i]) st[++top]=i;
	for(int i=1;i<m;i++) dist[i]=cnt[i];
	while(top)
	{
		int now=st[top--];
		for(int i=last[now], too;i;i=e[i].pre)
		{
			deg[too=e[i].too]--;
			if(!deg[too]) st[++top]=too;
			if(dist[too]<dist[now]+cnt[too])
			{
				nxt[too]=now;
				dist[too]=dist[now]+cnt[too];
			}
		}
	}
}

int exgcd(int a, int b, int &x, int &y)
{
	if(!b) return x=1, y=0, a;
	int d=exgcd(b, a%b, x, y);
	int tmp=x; x=y; y=tmp-a/b*y;
	return d;
}

inline int calc(int pre, int now)
{
	int x, y;
	int d=exgcd(pre, m, x, y);
	int b=m/d;
	x=((x%b)+b)%b; x=1ll*x*(now/d)%m;
	return x;

}

int main()
{
	read(n); read(m);
	for(int i=1;i<=n;i++) read(x), flag[x]=1;
	for(int i=1;i<m;i++)
	if(!flag[i]) cnt[d=gcd(i, m)]++, v[d].push_back(i);
	for(int i=1;i<m;i++)
	{
		for(int j=i+i;j<m;j+=i)
		if(cnt[j]) add(j, i);
	}
	toposort(); int ans=0;
	for(int i=1;i<m;i++)
	if(dist[i]>ans) ans=dist[i], now=i;
	printf("%d\n", ans+(!flag[0]));
	int pre=1;
	while(now)
	{
		for(int i=0;i<v[now].size();i++)
		printf("%d ", calc(pre, v[now][i])), pre=v[now][i];
		now=nxt[now];
	}
	if(!flag[0]) puts("0");
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed<<setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
const int MOD = (int) 1e9 + 7;
const int INF = (int) 1e9;
const ll LINF = (ll) 1e18;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}
template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}
inline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;}
inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;}
inline int sign(ld x, ld y) {return sign(x - y);}
#define db(x) cerr << #x << " = " << (x) << ", ";
#define endln cerr << "\n";
#define chkpt cerr << "-----\n";

template<class T> pair<T, T> euclid(T a, T b) {
	if (!b) return make_pair(1, 0);
	pair<T, T> r = euclid(b, a % b);
	return make_pair(r.second, r.first - a / b * r.second);
}

template<class T> invert(T a, T p) {
	pair<T, T> r = euclid(a, p);
	return (r.fi % p + p) % p;
}

const int maxn = 2e5 + 5;
int n, m;
int f[maxn];
vi g[maxn];
vi dvs[maxn];
int dp[maxn];
int nxt[maxn];

int func(int k) {
	int& res = dp[k];
	if (~res) return res;
	res = 0;
	FOR(i, 0, sz(dvs[k])) {
		int nk = dvs[k][i];
		if (chkmax(res, func(nk))) {
			nxt[k] = nk;
		}
	}
	res += f[k];
	return res;
}

int calc(int x, int y, int p) {
	int d = __gcd(x, y);
	x /= d, y /= d, p /= d;
	return (long long) x * invert(y, p) % p;
}

void solve() {
	FOR(i, 1, maxn) {
		for (int j = i + i; j < maxn; j += i) {
			dvs[j].pb(i);
		}
	}
	cin >> n >> m;
	set<int> st;
	FOR(i, 1, m + 1) {
		st.insert(i);
	}
	FOR(i, 0, n) {
		int x; cin >> x;
		if (!x) x = m;
		if (present(st, x)) {
			st.erase(x);
		}
	}
	FORall(it, st) {
		int x = __gcd(*it, m);
		f[x]++, g[x].pb(*it);
	}
	ms(dp, -1), ms(nxt, -1);
	pi ans;
	FOR(i, 1, m + 1) if (f[i]) {
		chkmax(ans, mp(func(i), i));
	}
	cout << ans.fi << "\n";
	vi v;
	int ptr = ans.se;
	while (ptr != -1) {
		FOR(j, 0, sz(g[ptr])) {
			v.pb(g[ptr][j]);
		}
		ptr = nxt[ptr];
	}
	reverse(all(v));
	vi vv = v;
	FOR(i, 1, sz(v)) vv[i] = calc(v[i], v[i - 1], m);
	FOR(i, 0, sz(vv)) cout << vv[i] << " \n"[i == sz(vv) - 1];
}

int main() {
    if (fopen("in.txt", "r")) {
        freopen("in.txt", "r", stdin);
    }
    ios_base::sync_with_stdio(0); cin.tie(0);
    solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed<<setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
const int MOD = (int) 1e9 + 7;
const int INF = (int) 1e9;
const ll LINF = (ll) 1e18;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}
template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}
inline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;}
inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;}
inline int sign(ld x, ld y) {return sign(x - y);}
#define db(x) cerr << #x << " = " << (x) << ", ";
#define endln cerr << "\n";
#define chkpt cerr << "-----\n";

template<class T> pair<T, T> euclid(T a, T b) {
	if (!b) return make_pair(1, 0);
	pair<T, T> r = euclid(b, a % b);
	return make_pair(r.second, r.first - a / b * r.second);
}

template<class T> invert(T a, T p) {
	pair<T, T> r = euclid(a, p);
	return (r.fi % p + p) % p;
}

const int maxn = 2e5 + 5;
int n, m;
int f[maxn];
vi g[maxn];
vi dvs[maxn];
int dp[maxn];
int nxt[maxn];

int func(int k) {
	int& res = dp[k];
	if (~res) return res;
	res = 0;
	FOR(i, 0, sz(dvs[k])) {
		int nk = dvs[k][i];
		if (chkmax(res, func(nk))) {
			nxt[k] = nk;
		}
	}
	res += f[k];
	return res;
}

int calc(int x, int y, int p) {
	int d = __gcd(x, y);
	x /= d, y /= d, p /= __gcd(p, d);
	return (long long) x * invert(y, p) % p;
}

void solve() {
	FOR(i, 1, maxn) {
		for (int j = i + i; j < maxn; j += i) {
			dvs[j].pb(i);
		}
	}
	cin >> n >> m;
	set<int> st;
	FOR(i, 1, m + 1) {
		st.insert(i);
	}
	FOR(i, 0, n) {
		int x; cin >> x;
		if (!x) x = m;
		if (present(st, x)) {
			st.erase(x);
		}
	}
	FORall(it, st) {
		int x = __gcd(*it, m);
		f[x]++, g[x].pb(*it);
	}
	ms(dp, -1), ms(nxt, -1);
	pi ans;
	FOR(i, 1, m + 1) if (f[i]) {
		chkmax(ans, mp(func(i), i));
	}
	cout << ans.fi << "\n";
	vi v;
	int ptr = ans.se;
	while (ptr != -1) {
		FOR(j, 0, sz(g[ptr])) {
			v.pb(g[ptr][j]);
		}
		ptr = nxt[ptr];
	}
	reverse(all(v));
	vi vv = v;
	FOR(i, 1, sz(v)) vv[i] = calc(v[i], v[i - 1], m);
	FOR(i, 0, sz(vv)) {
		cout << vv[i] % m << " \n"[i == sz(vv) - 1];
	}
}

int main() {
    if (fopen("in.txt", "r")) {
        freopen("in.txt", "r", stdin);
    }
    ios_base::sync_with_stdio(0); cin.tie(0);
    solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed<<setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
const int MOD = (int) 1e9 + 7;
const int INF = (int) 1e9;
const ll LINF = (ll) 1e18;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}
template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}
inline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;}
inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;}
inline int sign(ld x, ld y) {return sign(x - y);}
#define db(x) cerr << #x << " = " << (x) << ", ";
#define endln cerr << "\n";
#define chkpt cerr << "-----\n";

template<class T> pair<T, T> euclid(T a, T b) {
	if (!b) return make_pair(1, 0);
	pair<T, T> r = euclid(b, a % b);
	return make_pair(r.second, r.first - a / b * r.second);
}

template<class T> invert(T a, T p) {
	pair<T, T> r = euclid(a, p);
	return (r.fi % p + p) % p;
}

const int MAXN = 2e5 + 5;
int fl[MAXN];
int sp[MAXN];
void Sieve() {
	for (int i = 2; i < MAXN; i += 2) sp[i] = 2;
	for (int i = 3; i < MAXN; i += 2) if (!fl[i]) {
		sp[i] = i;
		for (int j = i; (long long) i * j < MAXN; j += 2) if (!fl[i * j]) {
			fl[i * j] = 1;
			sp[i * j] = i;
		}
	}
}

const int maxn = 2e5 + 5;
int n, m;
int f[maxn];
vi g[maxn];
vi dvs[maxn];
int dp[maxn];
int nxt[maxn];

int func(int k) {
	int& res = dp[k];
	if (~res) return res;
	res = 0;
	nxt[k] = -1;
	FOR(i, 0, sz(dvs[k])) {
		int nk = dvs[k][i];
		if (chkmax(res, func(nk))) {
			nxt[k] = nk;
		}
	}
	res += f[k];
	return res;
}

int calc(int x, int y, int p) {
	int d = __gcd(x, y);
	x /= d, y /= d, p /= d;
	return (long long) x * invert(y, p) % p;
}

void solve() {
	FOR(i, 1, maxn) {
		for (int j = i + i; j < maxn; j += i) {
			dvs[j].pb(i);
		}
	}
	Sieve();
	cin >> n >> m;
	set<int> st;
	FOR(i, 1, m + 1) {
		st.insert(i);
	}
	FOR(i, 0, n) {
		int x; cin >> x;
		if (present(st, x)) {
			st.erase(x);
		}
	}
	FORall(it, st) {
		int x = *it;
		int t = 1;
		while (x > 1) {
			int k = sp[x];
			if (m % k == 0) {
				t *= k;
			}
			x /= k;
		}
		t = __gcd(t, m);
		f[t]++, g[t].pb(*it);
	}
	ms(dp, -1);
	pi ans;
	FOR(i, 1, m + 1) if (f[i]) {
		chkmax(ans, mp(func(i), i));
	}
	cout << ans.fi << "\n";
	vi v;
	int ptr = ans.se;
	while (ptr != -1) {
		FOR(j, 0, sz(g[ptr])) {
			v.pb(g[ptr][j]);
		}
		ptr = nxt[ptr];
	}
	reverse(all(v));
	vi vv = v;
	FOR(i, 1, sz(v)) vv[i] = calc(v[i], v[i - 1], m);
	FOR(i, 0, sz(vv)) cout << vv[i] << " \n"[i == sz(vv) - 1];
}

int main() {
    if (fopen("in.txt", "r")) {
        freopen("in.txt", "r", stdin);
    }
    ios_base::sync_with_stdio(0); cin.tie(0);
    solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed<<setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
const int MOD = (int) 1e9 + 7;
const int INF = (int) 1e9;
const ll LINF = (ll) 1e18;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}
template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}
inline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;}
inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;}
inline int sign(ld x, ld y) {return sign(x - y);}
#define db(x) cerr << #x << " = " << (x) << ", ";
#define endln cerr << "\n";
#define chkpt cerr << "-----\n";

template<class T> pair<T, T> euclid(T a, T b) {
	if (!b) return make_pair(1, 0);
	pair<T, T> r = euclid(b, a % b);
	return make_pair(r.second, r.first - a / b * r.second);
}

template<class T> invert(T a, T p) {
	pair<T, T> r = euclid(a, p);
	return (r.fi % p + p) % p;
}

const int maxn = 2e5 + 5;
int n, m;
int f[maxn];
vi g[maxn];
vi dvs[maxn];
int dp[maxn];
int nxt[maxn];

int func(int k) {
	int& res = dp[k];
	if (~res) return res;
	res = 0;
	FOR(i, 0, sz(dvs[k])) {
		int nk = dvs[k][i];
		if (chkmax(res, func(nk))) {
			nxt[k] = nk;
		}
	}
	res += f[k];
	return res;
}

int calc(int x, int y, int p) {
	int d = __gcd(x, y);
	x /= d, y /= d, p /= d;
	return (long long) x * invert(y, p) % p;
}

void solve() {
	FOR(i, 1, maxn) {
		for (int j = i + i; j < maxn; j += i) {
			dvs[j].pb(i);
		}
	}
	cin >> n >> m;
	set<int> st;
	FOR(i, 1, m + 1) {
		st.insert(i);
	}
	FOR(i, 0, n) {
		int x; cin >> x;
		if (!x) x = m;
		if (present(st, x)) {
			st.erase(x);
		}
	}
	FORall(it, st) {
		int x = __gcd(*it, m);
		f[x]++, g[x].pb(*it);
	}
	ms(dp, -1), ms(nxt, -1);
	pi ans;
	FOR(i, 1, m + 1) if (f[i]) {
		chkmax(ans, mp(func(i), i));
	}
	cout << ans.fi << "\n";
	vi v;
	int ptr = ans.se;
	while (ptr != -1) {
		FOR(j, 0, sz(g[ptr])) {
			v.pb(g[ptr][j]);
		}
		ptr = nxt[ptr];
	}
	reverse(all(v));
	vi vv = v;
	FOR(i, 1, sz(v)) vv[i] = calc(v[i], v[i - 1], m);
	FOR(i, 0, sz(vv)) {
		cout << vv[i] % m << " \n"[i == sz(vv) - 1];
	}
}

int main() {
    if (fopen("in.txt", "r")) {
        freopen("in.txt", "r", stdin);
    }
    ios_base::sync_with_stdio(0); cin.tie(0);
    solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;
const int N = 2e5 + 5, SQRT = 1000;
int n, m, cnt, dv[SQRT], rdv[N], vis[SQRT], dp[SQRT][3];
vector<int> pr[SQRT];
set<int> marked;

int gcd (int a, int b) {
	return b ? gcd (b, a % b) : a;
}

int mod(int a, int b) {
	return ((a%b) + b) % b;
}

int extended_euclid(int a, int b, int &x, int &y) {
	int xx = y = 0;
	int yy = x = 1;
	while (b) {
		int q = a / b;
		int t = b; b = a%b; a = t;
		t = xx; xx = x - q*xx; x = t;
		t = yy; yy = y - q*yy; y = t;
	}
	return a;
}

int mod_inverse(int a, int n) {
	int x, y;
	int g = extended_euclid(a, n, x, y);
	if (g > 1) return -1;
	return mod(x, n);
}

void dfs (int u) {
	if (vis[u]) return;
	// cout << "DE: " << u << endl;
	vis[u] = 1;
	int d = dv[u]; int dd = m / d; int w = rdv[dd];
	dp[u][0] = 0; dp[u][1] = -1; dp[u][2] = 1;
	for (auto p: pr[w]) {
		int v = rdv[d * p];
		dfs(v);
		if (dp[v][0] > dp[u][0]) {
			dp[u][0] = dp[v][0];
			dp[u][1] = v;
			dp[u][2] = p;
		}
	}
	for (int i = 1; i <= dd; i++)
		if (gcd(i, dd) == 1 && !marked.count(i * d % m)) dp[u][0]++;
	// cout << "D: " << u << " " << d << ": " << dd << " " << dp[u][0] << " " << dp[u][2] << endl;
}
int main () {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= m; i++) if (m % i == 0) {
		dv[cnt] = i; rdv[i] = cnt;
		int x = i;
		for (int p = 2; p * 1ll * p <= x; p++) if (x % p == 0) {
			pr[cnt].pb(p);
			while (x % p == 0) x /= p;
		}
		if (x != 1) {
			pr[cnt].pb(x);
		}
		// cout << "PHI: " << cnt << " " << i << ": " << phi[cnt] << " " << pr[cnt].size() << endl;
		cnt++;
	}
	for (int i = 0; i < n; i++) {
		int a; cin >> a;
		marked.insert(a % m);
	}
	dfs(0);
	cout << dp[0][0] << "\n";
	int u = 0;
	int del = 1;
	while (u != -1) {
		int d = dv[u];
		int dd = m / d;
		int v = rdv[dd];
		// cout << u << "-> " << d <<  " " << dd << " " << del << endl;
		int prev = 1; bool started = 0;
		for (int i = 1; i <= dd; i++)
			if (gcd(i, dd) == 1 && !marked.count(i * d % m)) {
				int cur = i;
				if (!started) {
					cur = (cur * 1ll * del) % m; started = 1;
				}
				int inv = mod_inverse(prev, dd);
				cur = (cur * 1ll * inv) % m;
				// cout << i << ":" << cur << " ";
				cout << cur << " ";
				prev = i;
			}
		// cout << endl;
		if (started) del = 1;
		del = (del * 1ll * dp[u][2]) % m;
		del = (del * 1ll * mod_inverse(prev, dd)) % m;
		u = dp[u][1];
	}
	cout << "\n";
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;
const int N = 2e5 + 5, SQRT = 1000;
int n, m, cnt, dv[SQRT], rdv[N], phi[SQRT], vis[SQRT], dp[SQRT][3];
vector<int> pr[SQRT];
set<int> marked;

int gcd (int a, int b) {
	return b ? gcd (b, a % b) : a;
}

int mod(int a, int b) {
	return ((a%b) + b) % b;
}

int extended_euclid(int a, int b, int &x, int &y) {
	int xx = y = 0;
	int yy = x = 1;
	while (b) {
		int q = a / b;
		int t = b; b = a%b; a = t;
		t = xx; xx = x - q*xx; x = t;
		t = yy; yy = y - q*yy; y = t;
	}
	return a;
}

int mod_inverse(int a, int n) {
	int x, y;
	int g = extended_euclid(a, n, x, y);
	if (g > 1) return -1;
	return mod(x, n);
}

void dfs (int u) {
	if (vis[u]) return;
	// cout << "DE: " << u << endl;
	vis[u] = 1;
	int d = dv[u]; int dd = m / d; int w = rdv[dd];
	dp[u][0] = 0; dp[u][1] = -1;
	for (auto p: pr[w]) {
		int v = rdv[d * p];
		dfs(v);
		if (dp[v][0] > dp[u][0]) {
			dp[u][0] = dp[v][0];
			dp[u][1] = v;
			dp[u][2] = p;
		}
	}
	for (int i = 1; i <= dd; i++)
		if (gcd(i, dd) == 1 && !marked.count(i * d)) dp[u][0]++;
	// cout << "D: " << u << " " << d << ": " << dd << " " << dp[u][0] << " " << dp[u][2] << endl;
}
int main () {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= m; i++) if (m % i == 0) {
		dv[cnt] = i; rdv[i] = cnt;
		int x = i, y = i;
		for (int p = 2; p <= x/p; p++) if (x % p == 0) {
			pr[cnt].pb(p);
			y /= p; y *= p - 1;
			while (x % p == 0) x /= p;
		}
		if (x != 1) {
			pr[cnt].pb(x);
			y /= x; y *= x - 1;
		}
		phi[cnt] = y;
		// cout << "PHI: " << cnt << " " << i << ": " << phi[cnt] << " " << pr[cnt].size() << endl;
		cnt++;
	}
	for (int i = 0; i < n; i++) {
		int a; cin >> a;
		marked.insert(a % m);
	}
	dfs(0);
	cout << dp[0][0] << "\n";
	int u = 0;
	int del = 1;
	while (u != -1) {
		int d = dv[u];
		int dd = m / d;
		int v = rdv[dd];
		// cout << u << "-> " << d <<  " " << dd << " " << del << endl;
		int prev = 1; bool started = 0;
		for (int i = 1; i <= dd; i++)
			if (gcd(i, dd) == 1 && !marked.count(i * d)) {
				int cur = i;
				if (!started) {
					cur = (cur * 1ll * del) % m; started = 1;
				} else {
					int inv = mod_inverse(prev, dd);
					cur = (cur * 1ll * inv) % m;
				}
				// cout << i << ":" << cur << " ";
				cout << cur << " ";
				prev = i;
			}
		// cout << endl;
		del = (dp[u][2] * 1ll * mod_inverse(prev, dd)) % m;
		u = dp[u][1];
	}
	cout << "\n";
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <ctype.h>
#include <deque>
#include <queue>
#include <cstring>
#include <set>
#include <list>
#include <map>
#include <random>
#include <unordered_map>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef std::vector<int> vi;
typedef std::vector<bool> vb;
typedef std::vector<string> vs;
typedef std::vector<double> vd;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<int> > vvi;
typedef vector<vvi> vvvi;
typedef vector<vll> vvll;
typedef std::vector<std::pair<int, int> > vpi;
typedef vector<vpi> vvpi;
typedef std::pair<int, int> pi;
typedef std::pair<ll, ll> pll;
typedef std::vector<pll> vpll;

const long long mod = 1000000007;

#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define forn(i, a, b) for(int i = a; i < b; i++)

#define pb push_back
#define mp make_pair

long long gcd(long long a, long long b){
    if(a==0) return b;
    if(b==0) return a;
    if(a>b) return gcd(b, a%b);
    return gcd(a,b%a);
}

ll gcd (ll a, ll b, ll & x, ll & y) {
    if (a == 0) {
        x = 0; y = 1;
        return b;
    }
    ll x1, y1;
    ll d = gcd (b%a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return d;
}

ll invmod(ll a, ll modulo) {
    ll x, y;
    gcd(a, modulo, x, y);
    return (x%modulo+modulo)%modulo;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif
    int n,m;
    scanf("%d %d", &n, &m);
    if(m==1) {
        cout<<1<<endl<<0;
        return 0;
    }
    vi ban(m,0);
    forn(i,0,n) {
        int x;
        scanf("%d", &x);
        ban[x] = 1;
    }
    vi div;
    forn(i,1,m+1) {
        if(m%i == 0) div.pb(i);
    }
    vvi al(m+1);
    vi cnt(m+1,0);
    vi nx(m+1,m);
    if(!ban[0]) {
        al[m].pb(0);
        cnt[m]++;
    }
    for(int v = m-1; v > 0; v--) {
        if(m%v != 0) continue;
        int lo = m/v;
        forn(i,1,m/v+1) {
            if(gcd(i,lo) == 1 && !ban[v*i]) {
                al[v].pb(v*i);
                cnt[v]++;
            }
            if(m%(v*i) != 0) continue;
            if (i==1) continue;
            int cand = v*i;
            if(cnt[cand] > cnt[nx[v]]) {
                nx[v] = cand;
            }
        }
        cnt[v] += cnt[nx[v]];
    }
//    cout<<cnt[1];
    vi ans = al[1];
    int curd = 1;
    while(1) {
        curd = nx[curd];
        for(auto x : al[curd]) ans.pb(x);
        if(curd == m) break;
    }
    printf("%d\n", ans.size());
    for(int i = ans.size()-1; i>0; i--) {
        int v = ans[i];
        int u = ans[i-1];
        int d = gcd(v,u);
        ans[i] = (v/d * invmod(u/d, m/d)) % m;

    }
    for(auto x : ans) printf("%d ", x);

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <ctype.h>
#include <deque>
#include <queue>
#include <cstring>
#include <set>
#include <list>
#include <map>
#include <random>
#include <unordered_map>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef std::vector<int> vi;
typedef std::vector<bool> vb;
typedef std::vector<string> vs;
typedef std::vector<double> vd;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<int> > vvi;
typedef vector<vvi> vvvi;
typedef vector<vll> vvll;
typedef std::vector<std::pair<int, int> > vpi;
typedef vector<vpi> vvpi;
typedef std::pair<int, int> pi;
typedef std::pair<ll, ll> pll;
typedef std::vector<pll> vpll;

const long long mod = 1000000007;

#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define forn(i, a, b) for(int i = a; i < b; i++)

#define pb push_back
#define mp make_pair

long long gcd(long long a, long long b){
    if(a==0) return b;
    if(b==0) return a;
    if(a>b) return gcd(b, a%b);
    return gcd(a,b%a);
}

ll gcd (ll a, ll b, ll & x, ll & y) {
    if (a == 0) {
        x = 0; y = 1;
        return b;
    }
    ll x1, y1;
    ll d = gcd (b%a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return d;
}

ll invmod(ll a, ll modulo) {
    ll x, y;
    gcd(a, modulo, x, y);
    return (x%modulo+modulo)%modulo;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif
    int n,m;
    scanf("%d %d", &n, &m);
    vi ban(m,0);
    forn(i,0,n) {
        int x;
        scanf("%d", &x);
        ban[x] = 1;
    }
    vi div;
    forn(i,1,m+1) {
        if(m%i == 0) div.pb(i);
    }
    vvi al(m+1);
    vi cnt(m+1,0);
    vi nx(m+1,m);
    if(!ban[0]) {
        al[m].pb(0);
        cnt[m]++;
    }
    for(int v = m-1; v > 0; v--) {
        if(m%v != 0) continue;
        int lo = m/v;
        forn(i,1,m/v+1) {
            if(gcd(i,lo) == 1 && !ban[v*i]) {
                al[v].pb(v*i);
                cnt[v]++;
            }
            if(m%(v*i) != 0) continue;
            int cand = v*i;
            if(cnt[cand] > cnt[nx[v]]) {
                nx[v] = cand;
            }
        }
        cnt[v] += cnt[nx[v]];
    }
//    cout<<cnt[1];
    vi ans = al[1];
    int curd = 1;
    while(1) {
        curd = nx[curd];
        for(auto x : al[curd]) ans.pb(x);
        if(curd == m) break;
    }
    printf("%d\n", ans.size());
    for(int i = ans.size()-1; i>0; i--) {
        int v = ans[i];
        int u = ans[i-1];
        int d = gcd(v,u);
        ans[i] = (v/d * invmod(u/d, m)) % m;
    }
    for(auto x : ans) printf("%d ", x);

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <ctype.h>
#include <deque>
#include <queue>
#include <cstring>
#include <set>
#include <list>
#include <map>
#include <random>
#include <unordered_map>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef std::vector<int> vi;
typedef std::vector<bool> vb;
typedef std::vector<string> vs;
typedef std::vector<double> vd;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<int> > vvi;
typedef vector<vvi> vvvi;
typedef vector<vll> vvll;
typedef std::vector<std::pair<int, int> > vpi;
typedef vector<vpi> vvpi;
typedef std::pair<int, int> pi;
typedef std::pair<ll, ll> pll;
typedef std::vector<pll> vpll;

const long long mod = 1000000007;

#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define forn(i, a, b) for(int i = a; i < b; i++)

#define pb push_back
#define mp make_pair

long long gcd(long long a, long long b){
    if(a==0) return b;
    if(b==0) return a;
    if(a>b) return gcd(b, a%b);
    return gcd(a,b%a);
}

ll gcd (ll a, ll b, ll & x, ll & y) {
    if (a == 0) {
        x = 0; y = 1;
        return b;
    }
    ll x1, y1;
    ll d = gcd (b%a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return d;
}

ll invmod(ll a, ll modulo) {
    ll x, y;
    gcd(a, modulo, x, y);
    return (x%modulo+modulo)%modulo;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif
    int n,m;
    scanf("%d %d", &n, &m);
    if(m==1) {
        cout<<1<<endl<<0;
        return 0;
    }
    vi ban(m,0);
    forn(i,0,n) {
        int x;
        scanf("%d", &x);
        ban[x] = 1;
    }
    vi div;
    forn(i,1,m+1) {
        if(m%i == 0) div.pb(i);
    }
    vvi al(m+1);
    vi cnt(m+1,0);
    vi nx(m+1,m);
    if(!ban[0]) {
        al[m].pb(0);
        cnt[m]++;
    }
    for(int v = m-1; v > 0; v--) {
        if(m%v != 0) continue;
        int lo = m/v;
        forn(i,1,m/v+1) {
            if(gcd(i,lo) == 1 && !ban[v*i]) {
                al[v].pb(v*i);
                cnt[v]++;
            }
            if(m%(v*i) != 0) continue;
            if (i==1) continue;
            int cand = v*i;
            if(cnt[cand] > cnt[nx[v]]) {
                nx[v] = cand;
            }
        }
        cnt[v] += cnt[nx[v]];
    }
//    cout<<cnt[1];
    vi ans = al[1];
    int curd = 1;
    while(1) {
        curd = nx[curd];
        for(auto x : al[curd]) ans.pb(x);
        if(curd == m) break;
    }
    printf("%d\n", ans.size());
    for(int i = ans.size()-1; i>0; i--) {
        int v = ans[i];
        int u = ans[i-1];
        int d = gcd(v,u);
        d = gcd(d,m);
        ans[i] = (v/d * invmod(u/d, m/d)) % m;

    }
    for(auto x : ans) printf("%d ", x);

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <ctype.h>
#include <deque>
#include <queue>
#include <cstring>
#include <set>
#include <list>
#include <map>
#include <random>
#include <unordered_map>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef std::vector<int> vi;
typedef std::vector<bool> vb;
typedef std::vector<string> vs;
typedef std::vector<double> vd;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<int> > vvi;
typedef vector<vvi> vvvi;
typedef vector<vll> vvll;
typedef std::vector<std::pair<int, int> > vpi;
typedef vector<vpi> vvpi;
typedef std::pair<int, int> pi;
typedef std::pair<ll, ll> pll;
typedef std::vector<pll> vpll;

const long long mod = 1000000007;

#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define forn(i, a, b) for(int i = a; i < b; i++)

#define pb push_back
#define mp make_pair

long long gcd(long long a, long long b){
    if(a==0) return b;
    if(b==0) return a;
    if(a>b) return gcd(b, a%b);
    return gcd(a,b%a);
}

ll gcd (ll a, ll b, ll & x, ll & y) {
    if (a == 0) {
        x = 0; y = 1;
        return b;
    }
    ll x1, y1;
    ll d = gcd (b%a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return d;
}

ll invmod(ll a, ll modulo) {
    ll x, y;
    gcd(a, modulo, x, y);
    return (x%modulo+modulo)%modulo;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif
    int n,m;
    scanf("%d %d", &n, &m);
    if(m==1) {
        cout<<1<<endl<<0;
        return 0;
    }
    vi ban(m,0);
    forn(i,0,n) {
        int x;
        scanf("%d", &x);
        ban[x] = 1;
    }
    vi div;
    forn(i,1,m+1) {
        if(m%i == 0) div.pb(i);
    }
    vvi al(m+1);
    vi cnt(m+1,0);
    vi nx(m+1,m);
    if(!ban[0]) {
        al[m].pb(0);
        cnt[m]++;
    }
    for(int v = m-1; v > 0; v--) {
        if(m%v != 0) continue;
        int lo = m/v;
        forn(i,1,m/v+1) {
            if(gcd(i,lo) == 1 && !ban[v*i]) {
                al[v].pb(v*i);
                cnt[v]++;
            }
            if(m%(v*i) != 0) continue;
            if (i==1) continue;
            int cand = v*i;
            if(cnt[cand] > cnt[nx[v]]) {
                nx[v] = cand;
            }
        }
        cnt[v] += cnt[nx[v]];
    }
//    cout<<cnt[1];
    vi ans = al[1];
    int curd = 1;
    while(1) {
        curd = nx[curd];
        for(auto x : al[curd]) ans.pb(x);
        if(curd == m) break;
    }
    printf("%d\n", ans.size());
    for(int i = ans.size()-1; i>0; i--) {
        int v = ans[i];
        int u = ans[i-1];
        int d = gcd(v,u);
        ans[i] = (v/d * invmod(u/d, m)) % m;
    }
    for(auto x : ans) printf("%d ", x);

}


#include <string>
#include <vector>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<functional>
#include<list>
#include<deque>
#include<bitset>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<cstring>
#include<sstream>
#include<complex>
#include<iomanip>
#include<numeric>
#include<cassert>
#define X first
#define Y second
#define pb push_back
#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define reps(X,S,Y) for (int (X) = S;(X) < (Y);++(X))
#define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define rreps(X,S,Y) for (int (X) = (Y)-1;(X) >= (S);--(X))
#define repe(X,Y) for ((X) = 0;(X) < (Y);++(X))
#define peat(X,Y) for (;(X) < (Y);++(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
#define Endl endl

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<class T> using vv=vector<vector<T>>;
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"{"; rep(i,t.size()) {os<<t[i]<<",";} os<<"}"<<endl; return os;}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}
template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;}
#define out(args...){vector<string> a_r_g_s=s_p_l_i_t(#args, ','); e_r_r(a_r_g_s.begin(), args); }
vector<string> s_p_l_i_t(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while(getline(ss,x,c)) v.emplace_back(x); return move(v);}
void e_r_r(vector<string>::iterator it) {}
template<typename T, typename... Args> void e_r_r(vector<string>::iterator it, T a, Args... args){ if(*it==" 1"||*it=="1") cerr<<endl; else cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << ", "; e_r_r(++it, args...);}
const ll MOD=1e9+7;

vector<int> nums[212345];
int dp[212345];
int pre[212345];
ll extgcd(ll a,ll b,ll &x,ll &y){
  ll g=a; x=1,y=0;
  if(b){
    g=extgcd(b,a%b,y,x);
    y-=(a/b)*x;
  }
  return g;
}

int extgcd(ll a,ll b,ll c){
  ll g=__gcd(a,b);
  a/=g; b/=g; c/=g;
  ll x,y;
  extgcd(a,b,x,y);
  x%=b;
  if(x<0) x+=b;
  // out(a,b,c,x,y,1);
  // out(a*x+b*y,c,x*c%b,1);
  return x*c%b;
}

int main(){
  ios_base::sync_with_stdio(false);
  cout<<fixed<<setprecision(0);
  int n,m;
  cin>>n>>m;
  vector<int> a(n);
  rep(i,n) cin>>a[i];
  vector<int> facts;
  reps(i,1,m+1)if(m%i==0) facts.pb(i);
  //out(facts,1);
  unordered_set<int> ban(all(a));
  rep(i,m)if(ban.count(i)==0) nums[__gcd(m,i)].pb(i);
  //for(int x:facts) out(x,nums[x],1);
  for(int p:facts){
    for(int q:facts){
      if(q>=p) break;
      if(p%q==0 && MX(dp[p],dp[q])) pre[p]=q;
    }
    dp[p]+=nums[p].size();
  }
  vector<int> fs;
  for(int i=m;i>1;i=pre[i]) fs.pb(i);
  fs.pb(1);
  reverse(all(fs));
  //cout<<fs;
  vector<int> re;
  int prod=1;
  for(int p:fs){
    for(int y:nums[p]){
      int x=extgcd(prod,m,y);
      re.pb(x);
      (prod*=x)%=m;
    }
  }
  //cout<<re;
  cout<<re.size()<<endl;
  ll p=1;
  for(int x:re){
    x%=m;
    if(x<0) x+=m;
    (p*=x)%=m;
    // cout<<p<<",";
    cout<<x<<" ";
  }
  cout<<endl;
  return 0;
}


#include <string>
#include <vector>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<functional>
#include<list>
#include<deque>
#include<bitset>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<cstring>
#include<sstream>
#include<complex>
#include<iomanip>
#include<numeric>
#include<cassert>
#define X first
#define Y second
#define pb push_back
#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define reps(X,S,Y) for (int (X) = S;(X) < (Y);++(X))
#define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define rreps(X,S,Y) for (int (X) = (Y)-1;(X) >= (S);--(X))
#define repe(X,Y) for ((X) = 0;(X) < (Y);++(X))
#define peat(X,Y) for (;(X) < (Y);++(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
#define Endl endl

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<class T> using vv=vector<vector<T>>;
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"{"; rep(i,t.size()) {os<<t[i]<<",";} os<<"}"<<endl; return os;}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}
template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;}
#define out(args...){vector<string> a_r_g_s=s_p_l_i_t(#args, ','); e_r_r(a_r_g_s.begin(), args); }
vector<string> s_p_l_i_t(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while(getline(ss,x,c)) v.emplace_back(x); return move(v);}
void e_r_r(vector<string>::iterator it) {}
template<typename T, typename... Args> void e_r_r(vector<string>::iterator it, T a, Args... args){ if(*it==" 1"||*it=="1") cerr<<endl; else cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << ", "; e_r_r(++it, args...);}
const ll MOD=1e9+7;

vector<ll> nums[212345];
ll dp[212345];
ll pre[212345];
ll extgcd(ll a,ll b,ll &x,ll &y){
  ll g=a; x=1,y=0;
  if(b){
    g=extgcd(b,a%b,y,x);
    y-=(a/b)*x;
  }
  return g;
}

ll extgcd(ll a,ll b,ll c){
  ll g=__gcd(a,b);
  a/=g; b/=g; c/=g;
  ll x,y;
  extgcd(a,b,x,y);
  x%=b;
  if(x<0) x+=b;
  // out(a,b,c,x,y,1);
  // out(a*x+b*y,c,x*c%b,1);
  return x*c%b;
}

int main(){
  ios_base::sync_with_stdio(false);
  cout<<fixed<<setprecision(0);
  int n,m;
  cin>>n>>m;
  vector<ll> a(n);
  rep(i,n) cin>>a[i];
  vector<ll> facts;
  reps(i,1,m+1)if(m%i==0) facts.pb(i);
  //out(facts,1);
  unordered_set<ll> ban(all(a));
  rep(i,m)if(ban.count(i)==0) nums[__gcd(m,i)].pb(i);
  //for(int x:facts) out(x,nums[x],1);
  for(ll p:facts){
    for(ll q:facts){
      if(q>=p) break;
      if(p%q==0 && MX(dp[p],dp[q])) pre[p]=q;
    }
    dp[p]+=nums[p].size();
  }
  vector<ll> fs;
  for(ll i=m;i>1;i=pre[i]) fs.pb(i);
  fs.pb(1);
  reverse(all(fs));
  //cout<<fs;
  vector<ll> re;
  ll prod=1;
  for(ll p:fs){
    for(ll y:nums[p]){
      ll x=extgcd(prod,m,y);
      re.pb(x);
      (prod*=x)%=m;
    }
  }
  //cout<<re;
  cout<<re.size()<<endl;
  ll p=1;
  for(ll x:re){
    x%=m;
    if(x<0) x+=m;
    (p*=x)%=m;
    assert(ban.count(p)==0);
    ban.insert(p);
    // cout<<p<<",";
    cout<<x<<" ";
  }
  cout<<endl;
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef pair < ll, ll > pll;
typedef pair < int, ll > pil;
typedef pair < ll, int > pli;
typedef pair < int, int > pii;
typedef unsigned long long ull;

#define F first
#define S second
#define en end()
#define bg begin()
#define rev reverse
#define mp make_pair
#define pb push_back
#define y1 y1234567890
#define um unordered_map
#define all(x) x.bg, x.en
#define sqr(x) ((x) * (x))
#define sz(x) (int)x.size()

const ll inf = (ll)1e18;
const ll mod = (ll)1e9 + 7;
const double pi = acos(-1.0);
const double eps = (double)1e-9;

const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};

const int N = 200500;

bool u[N];
ll MOD, ans[N];
vector < int > v, ord, g[N];
int n, m, sz, a[N], b[N], dp[N], p[N], cnt[N];

inline int gcd(int a, int b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

ll bp(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1)
			res = res * a % MOD;
		a = a * a % MOD;
		b >>= 1;
	}
	return res;
}

int main() {
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);

	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);

	cin >> n >> m;

	MOD = m * 1ll;

	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		u[a[i]] = 1;
	}

	for (int i = 0; i < m; i++)
		if (!u[i])
			b[++sz] = i;

	for (int i = 1; i <= sz; i++) {
		cnt[gcd(m, b[i])]++;
		g[gcd(m, b[i])].pb(b[i]);
	}

	for (int i = 1; i <= m; i++)
		rev(all(g[i]));

	for (int i = 1; i <= m; i++)
		if (cnt[i])
			v.pb(i);

	for (int i = 0; i < sz(v); i++) {
		dp[i] = cnt[v[i]];
		p[i] = -1;
		for (int j = 0; j < i; j++) {
			if (v[i] % v[j] == 0 && dp[j] + cnt[v[i]] > dp[i]) {
				dp[i] = dp[j] + cnt[v[i]];
				p[i] = j;
			}
		}
	}

	int pos, mx = 0;

	for (int i = 0; i < sz(v); i++) {
		if (dp[i] > mx) {
			mx = dp[i];
			pos = i;
		}
	}

	while (pos != -1) {
		for (auto i : g[v[pos]])
			ord.pb(i);
		pos = p[pos];
	}

	rev(all(ord));

	ans[1] = ord[0];

	int phi = 0;

	for (int i = 1; i <= m; i++)
		if (gcd(i, m) == 1)
			phi++;

	for (int i = 1; i < sz(ord); i++) {
		int x = ord[i];
		int y = ord[i - 1];
		int GCD = gcd(x, y);
		x /= GCD; y /= GCD;
		ans[i + 1] = x * bp(y, phi - 1) % MOD;
	}

	cout << sz(ord) << endl;

	for (int i = 1; i <= sz(ord); i++)
		cout << ans[i] << " ";

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef pair < ll, ll > pll;
typedef pair < int, ll > pil;
typedef pair < ll, int > pli;
typedef pair < int, int > pii;
typedef unsigned long long ull;

#define F first
#define S second
#define en end()
#define bg begin()
#define rev reverse
#define mp make_pair
#define pb push_back
#define y1 y1234567890
#define um unordered_map
#define all(x) x.bg, x.en
#define sqr(x) ((x) * (x))
#define sz(x) (int)x.size()

const ll inf = (ll)1e18;
const ll mod = (ll)1e9 + 7;
const double pi = acos(-1.0);
const double eps = (double)1e-9;

const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};

const int N = 200500;

bool u[N];
ll MOD, ans[N];
vector < int > v, ord, g[N];
int n, m, sz, a[N], b[N], dp[N], p[N], cnt[N];

inline int gcd(int a, int b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

ll bp(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1)
			res = res * a % MOD;
		a = a * a % MOD;
		b >>= 1;
	}
	return res;
}

int main() {
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);

	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);

	cin >> n >> m;

	MOD = m * 1ll;

	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		u[a[i]] = 1;
	}

	for (int i = 0; i < m; i++)
		if (!u[i])
			b[++sz] = i;

	for (int i = 1; i <= sz; i++) {
		cnt[gcd(m, b[i])]++;
		g[gcd(m, b[i])].pb(b[i]);
	}

	for (int i = 1; i <= m; i++)
		rev(all(g[i]));

	for (int i = 1; i <= m; i++)
		if (cnt[i])
			v.pb(i);

	for (int i = 0; i < sz(v); i++) {
		dp[i] = cnt[v[i]];
		p[i] = -1;
		for (int j = 0; j < i; j++) {
			if (v[i] % v[j] == 0 && dp[j] + cnt[v[i]] > dp[i]) {
				dp[i] = dp[j] + cnt[v[i]];
				p[i] = j;
			}
		}
	}

	int pos, mx = 0;

	for (int i = 0; i < sz(v); i++) {
		if (dp[i] > mx) {
			mx = dp[i];
			pos = i;
		}
	}

	while (pos != -1) {
		for (auto i : g[v[pos]])
			ord.pb(i);
		pos = p[pos];
	}

	rev(all(ord));

	ans[1] = ord[0];

	int phi = 0;

	for (int i = 1; i <= m; i++)
		if (gcd(i, m) == 1)
			phi++;

	for (int i = 1; i < sz(ord); i++) {
		int x = ord[i];
		int y = ord[i - 1];
		int GCD = gcd(x, y);
		x /= GCD; y /= GCD;
		ans[i + 1] = x * bp(y, phi - 1) % MOD;
	}

	for (int i = 1; i <= sz(ord); i++)
		cout << ans[i] << " ";

	return 0;
}


#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <list>
#include <cassert>
#include <ctime>
#include <climits>
#include <random>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); }

const int MAXMOD=200000;

int n,MOD;
bool forbidden[MAXMOD];

vector<int> nrs[MAXMOD+1];
vector<int> opt;
vector<int> dp;
vector<int> prv;

// a*x+b*y=g
int egcd(int a,int b,int &x,int &y) {
	if(b==0) { x=1,y=0; return a; }
	int ret=egcd(b,a%b,x,y);
	swap(x,y); y-=(a/b)*x; return ret;
}

int solve(int a,int b) {
	int x,y; int g=egcd(a,MOD,x,y); // x*a=g (mod MOD)
	if(b%g!=0) { printf("err: %d*x=%d (MOD=%d,g=%d)\n",a,b,MOD,g); exit(1); }
	if(x<0) x+=MOD; return (ll)x*(b/g)%MOD;
}

void run() {
	scanf("%d%d",&n,&MOD);
	memset(forbidden,false,sizeof(forbidden)); REP(i,n) { int x; scanf("%d",&x); forbidden[x]=true; }
	REPE(i,MOD) nrs[i].clear(); REP(i,MOD) if(!forbidden[i]) nrs[gcd(i,MOD)].PB(i);
	opt.clear(); REPE(i,MOD) if(SZ(nrs[i])>0) opt.PB(i);
	dp=vector<int>(SZ(opt),0),prv=vector<int>(SZ(opt),-1);
	REPSZ(i,opt) {
		dp[i]+=SZ(nrs[opt[i]]);
		FORSZ(j,i+1,opt) if(opt[j]%opt[i]==0&&dp[i]>dp[j]) dp[j]=dp[i],prv[j]=i;
	}
	vector<int> pref;
	for(int at=SZ(opt)-1;at!=-1;at=prv[at]) REPSZ(i,nrs[opt[at]]) pref.PB(nrs[opt[at]][i]);
	reverse(pref.begin(),pref.end());
	//printf("pref:"); REPSZ(i,pref) printf(" %d",pref[i]); puts("");
	vector<int> ret(SZ(pref));
	ret[0]=pref[0];
	FORSZ(i,1,ret) ret[i]=solve(pref[i-1],pref[i]);
	printf("%d\n",SZ(ret));
	REPSZ(i,ret) { if(i!=0) printf(" "); printf("%d",ret[i]); } puts("");
}

int main() {
	run();
	return 0;
}


#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <list>
#include <cassert>
#include <ctime>
#include <climits>
#include <random>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); }

const int MAXMOD=200000;

int n,MOD;
bool forbidden[MAXMOD];

vector<int> nrs[MAXMOD+1];
vector<int> opt;
vector<int> dp;
vector<int> prv;

// a*x+b*y=g
int egcd(int a,int b,int &x,int &y) {
	if(b==0) { x=1,y=0; return a; }
	int ret=egcd(b,a%b,x,y);
	swap(x,y); y-=(a/b)*x; return ret;
}

int solve(int a,int b) {
	int x,y; int g=egcd(a,MOD,x,y); // x*a=g (mod MOD)
	if(b%g!=0) { printf("err: %d*x=%d (MOD=%d,g=%d)\n",a,b,MOD,g); exit(1); }
	if(x<0) x+=MOD; return (ll)x*(b/g)%MOD;
}

void run() {
	scanf("%d%d",&n,&MOD);
	memset(forbidden,false,sizeof(forbidden)); REP(i,n) { int x; scanf("%d",&x); forbidden[x]=true; }
	REPE(i,MOD) nrs[i].clear(); REP(i,MOD) if(!forbidden[i]) nrs[gcd(i,MOD)].PB(i);
	opt.clear(); REPE(i,MOD) if(SZ(nrs[i])>0) opt.PB(i);
	dp=vector<int>(SZ(opt),0),prv=vector<int>(SZ(opt),-1);
	REPSZ(i,opt) {
		dp[i]+=SZ(nrs[opt[i]]);
		FORSZ(j,i+1,opt) if(opt[j]%opt[i]==0&&dp[i]>dp[j]) dp[j]=dp[i],prv[j]=i;
	}
	vector<int> pref;
	int bst=0; FORSZ(i,1,opt) if(dp[i]>dp[bst]) bst=i;
	for(int at=bst;at!=-1;at=prv[at]) REPSZ(i,nrs[opt[at]]) pref.PB(nrs[opt[at]][i]);
	reverse(pref.begin(),pref.end());
	//printf("pref:"); REPSZ(i,pref) printf(" %d",pref[i]); puts("");
	vector<int> ret(SZ(pref));
	ret[0]=pref[0];
	FORSZ(i,1,ret) ret[i]=solve(pref[i-1],pref[i]);
	printf("%d\n",SZ(ret));
	REPSZ(i,ret) { if(i!=0) printf(" "); printf("%d",ret[i]); } puts("");
}

int main() {
	run();
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

template<class T> ostream& operator<<(ostream &os, vector<T> V) {
 os << "[ "; for(auto v : V) os << v << " "; return os << "]";}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
	return os << "(" << P.first << "," << P.second << ")";}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...) 1
#endif

#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define I insert
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector< pii >

// const int mod=1e9+7;
// inline int mul(int a,int b){return (a*1ll*b)%mod;}
// inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
// inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
// inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
// inline int inv(int a){return power(a,mod-2);}
// inline void modadd(int &a,int &b){a+=b;if(a>=mod)a-=mod;}
const int MAX = 2e5+5;

struct ex_gcd{ ll x,y,d;
	ex_gcd(ll x,ll y,ll d): x(x),y(y),d(d){}  };
ex_gcd gcd(ll a,ll b){if(b==0) return ex_gcd(1,0,a);
	ex_gcd c=gcd(b,a%b);
	return ex_gcd(c.y,c.x-(a/b)*c.y,c.d); }

bool exc[MAX];
int sz[MAX];
vi my[MAX];
int gc[MAX];
int inv[MAX];
int mx[MAX], nx[MAX];

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
	ll n, m;
	cin>>n>>m;
	int i,j,k;
	int a,b;
	for(i=0;i<n;i++){
		cin>>a; if(a==0) a=m;
		exc[a] = 1;
	}
	for(i=1;i<=m;i++){
		auto z = gcd(i, m);
		int x = z.d; gc[i] = x;
		inv[i] = ((z.x % m) + m ) % m;
		if(exc[i])continue;
		my[x].pb(i); sz[x]++;
	}
	for(i=m;i>=1;i--){
		mx[i] = sz[i]; nx[i]=-1;
		for(j=i+i;j<=m;j+=i){
			if(sz[i]+mx[j] > mx[i]){
				mx[i] = sz[i]+mx[j];
				nx[i] = j;
			}
		}
	}
	int cur = 1;
	cout<<mx[1]<<"\n";
	int prev = 1;
	int ext = 1;
	while(cur!=-1){
		// trace(cur);
		for(auto z:my[cur]){
			// trace(z, prev, inv[prev]);
			cout<< ( ((ext* (z/cur) )%m) * inv[prev])%m <<" ";
			prev = z;
			ext=1;
		}
		int cc = nx[cur];
		ext = cc/cur;
		cur = cc;
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

template<class T> ostream& operator<<(ostream &os, vector<T> V) {
 os << "[ "; for(auto v : V) os << v << " "; return os << "]";}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
	return os << "(" << P.first << "," << P.second << ")";}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...) 1
#endif

#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define I insert
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector< pii >

// const int mod=1e9+7;
// inline int mul(int a,int b){return (a*1ll*b)%mod;}
// inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
// inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
// inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
// inline int inv(int a){return power(a,mod-2);}
// inline void modadd(int &a,int &b){a+=b;if(a>=mod)a-=mod;}
const int MAX = 2e5+5;

struct ex_gcd{ ll x,y,d;
	ex_gcd(ll x,ll y,ll d): x(x),y(y),d(d){}  };
ex_gcd gcd(ll a,ll b){if(b==0) return ex_gcd(1,0,a);
	ex_gcd c=gcd(b,a%b);
	return ex_gcd(c.y,c.x-(a/b)*c.y,c.d); }

bool exc[MAX];
int sz[MAX];
vi my[MAX];
int gc[MAX];
int inv[MAX];
int mx[MAX], nx[MAX];

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
	ll n, m;
	cin>>n>>m;
	int i,j,k;
	int a,b;
	for(i=0;i<n;i++){
		cin>>a; if(a==0) a=m;
		exc[a] = 1;
	}
	for(i=1;i<=m;i++){
		auto z = gcd(i, m);
		int x = z.d; gc[i] = x;
		inv[i] = ((z.x % m) + m ) % m;
		if(exc[i])continue;
		my[x].pb(i); sz[x]++;
	}
	for(i=m;i>=1;i--){
		mx[i] = sz[i]; nx[i]=-1;
		for(j=i+i;j<=m;j+=i){
			if(sz[i]+mx[j] > mx[i]){
				mx[i] = sz[i]+mx[j];
				nx[i] = j;
			}
		}
	}
	int cur = 1;
	cout<<mx[1]<<"\n";
	int prev = 1;
	int ext = 1;
	while(cur!=-1){
		// trace(cur);
		for(auto z:my[cur]){
			// trace(z, prev, inv[prev]);
			cout<< ( ((ext* (z/cur) )%m) * inv[prev])%m <<" ";
			prev = z;
			ext=1;
		}
		int cc = nx[cur];
		ext *= cc/cur;
		cur = cc;
	}
}


//
//  main.cpp
//  C
//
//  Created by 黄宇凡 on 2017/4/17.
//  Copyright © 2017年 黄宇凡. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

#define F first
#define S second
using namespace std;

const int maxn = 2e5 + 5;

vector<pair<int,int> > dp;
vector<int> P;
vector<int> R;

vector<int> G[maxn];
int n,m;
bool vis[maxn];

typedef long long LL;

LL mod_pow(int a,int n){
    LL res = 1;
    LL b = a;
    while(n > 0){
        if(n & 1){
            res = res * b % m;
        }
        b = b * b % m;
        n >>= 1;
    }
    return res;
}

LL phi(int x){
    LL res = 1;
    for(int i = 2;i <= x;i++){
        if(x % i) continue;
        x /= i;
        res *= (i - 1);
        while(x % i == 0){
            x /= i;
            res *= i;
        }
    }
    return res;
}

int get(int a,int b,int c){
    int g = __gcd(a,b);
    a = a / g;
    b = b / g;
    return 1LL * b * mod_pow(a,c - 1) % m;
}

void print(){
    int power = (int)phi(m);
    reverse(R.begin(),R.end());
    printf("%d\n",(int)R.size() + (vis[0] == false));
    for(int i = 0;i < R.size();i++){
        if(i == 0){
            printf("%d",R[i]);
        }else{
            printf(" %d",get(R[i - 1],R[i],power));
        }
    }
    if(vis[0] == false) printf(" 0\n");
}

int main(int argc, const char * argv[]) {
    cin >> n >> m;
    for(int i = 1;i <= n;i++){
        int a;
        scanf("%d",&a);
        vis[a] = true;
    }
    for(int i = 1;i < m;i++){
        if(!vis[i]){
            G[__gcd(i,m)].push_back(i);
        }
    }
    for(int i = 1;i < m;i++){
        if(!G[i].empty()){
            P.push_back(i);
            dp.push_back(make_pair(G[i].size(),-1));
        }
    }int ret = 0;
    for(int i = 0;i < P.size();i++){
        for(int j = 0;j < i;j++){
            if(P[i] % P[j] == 0 && dp[i].F < dp[j].F + G[P[i]].size()){
                dp[i] = make_pair(dp[j].F + G[P[i]].size(),j);
            }
        }
        ret = max(ret,dp[i].F);
       // cout << dp[i].F << " " << dp[i].S << endl;
    }
  //  cout << "!" << endl;
    for(int i = 0;i < dp.size();i++){
        if(dp[i].F == ret){
            int tmp = i;
            while(tmp != -1){
                for(int j = 0;j < G[P[tmp]].size();j++){
                    R.push_back(G[P[tmp]][j]);
                }
                tmp = dp[tmp].S;
            }
            //cout << "!!" << endl;
            break;
        }
    }
//    for(int i = 0;i < R.size();i++){
//        printf("%d ",R[i]);
//    }
//    puts("");
    print();
    return 0;
}


//
//  main.cpp
//  C
//
//  Created by 黄宇凡 on 2017/4/17.
//  Copyright © 2017年 黄宇凡. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

#define F first
#define S second
using namespace std;

const int maxn = 2e5 + 5;

vector<pair<int,int> > dp;
vector<int> P;
vector<int> R;

vector<int> G[maxn];
int n,m;
bool vis[maxn];

typedef long long LL;

LL mod_pow(int a,int n){
    LL res = 1;
    LL b = a;
    while(n > 0){
        if(n & 1){
            res = res * b % m;
        }
        b = b * b % m;
        n >>= 1;
    }
    return res;
}

LL phi(int x){
    LL res = 1;
    for(int i = 2;i <= x;i++){
        if(x % i) continue;
        x /= i;
        res *= (i - 1);
        while(x % i == 0){
            x /= i;
            res *= i;
        }
    }
    return res;
}

int get(int a,int b){
    int g = __gcd(a,b);
    a = a / g;
    b = b / g;
    return 1LL * b * mod_pow(a,(int)phi(m) - 1) % m;
}

void print(){
    reverse(R.begin(),R.end());
    printf("%d\n",(int)R.size() + (vis[0] == false));
    for(int i = 0;i < R.size();i++){
        if(i == 0){
            printf("%d",R[i]);
        }else{
            printf(" %d",get(R[i - 1],R[i]));
        }
    }
    if(vis[0] == false) printf(" 0\n");
}

int main(int argc, const char * argv[]) {
    cin >> n >> m;
    for(int i = 1;i <= n;i++){
        int a;
        scanf("%d",&a);
        vis[a] = true;
    }
    for(int i = 1;i < m;i++){
        if(!vis[i]){
            G[__gcd(i,m)].push_back(i);
        }
    }
    for(int i = 1;i < m;i++){
        if(!G[i].empty()){
            P.push_back(i);
            dp.push_back(make_pair(G[i].size(),-1));
        }
    }int ret = 0;
    for(int i = 0;i < P.size();i++){
        for(int j = 0;j < i;j++){
            if(P[i] % P[j] == 0 && dp[i].F < dp[j].F + G[P[i]].size()){
                dp[i] = make_pair(dp[j].F + G[P[i]].size(),j);
            }
        }
        ret = max(ret,dp[i].F);
       // cout << dp[i].F << " " << dp[i].S << endl;
    }
  //  cout << "!" << endl;
    for(int i = 0;i < dp.size();i++){
        if(dp[i].F == ret){
            int tmp = i;
            while(tmp != -1){
                for(int j = 0;j < G[P[tmp]].size();j++){
                    R.push_back(G[P[tmp]][j]);
                }
                tmp = dp[tmp].S;
            }
            //cout << "!!" << endl;
            break;
        }
    }
//    for(int i = 0;i < R.size();i++){
//        printf("%d ",R[i]);
//    }
//    puts("");
    print();
    return 0;
}


#include <cstdio>
#include <cmath>
#include <utility>
#include <algorithm>

struct line_t
{
    double px,py,qx,qy;
};

struct point_t
{
    double first,second;
};

int n;
double m;
std::pair<int,int> points[1111];
double dis[1111][1111],ta[1111][1111],si[1111][1111];

#define x first
#define y second

inline double distancex(const std::pair<int,int> &a,const std::pair<int,int> &b)
{
    return sqrt(1ll * (b.second - a.second) * (b.second - a.second) + 1ll * (b.first - a.first) * (b.first - a.first));
}

inline double distancex(const struct line_t &line,const struct point_t &point)
{
    return std::abs(line.qx * (point.y - line.py) - line.qy * (point.x - line.px));
}

inline bool work(int a,int b,int c)
{
    struct point_t point;
    struct line_t line;
    double p,q;

    p = ta[a][b];
    q = si[a][b];

    point.x = points[c].x;
    point.y = points[c].y;
    line.px = (points[a].x + points[b].x) / 2.0;
    line.py = (points[a].y + points[b].y) / 2.0;
    line.qx = cos(p + q);
    line.qy = sin(p + q);
    if(distancex(line,point) <= m)
        return false;
    line.qx = cos(p - q);
    line.qy = sin(p - q);
    if(distancex(line,point) <= m)
        return false;

    line.qy = (points[b].y - points[a].y) / dis[a][b];
    line.qx = (points[b].x - points[a].x) / dis[a][b];
    line.px = points[a].x + m * line.qy;
    line.py = points[a].y - m * line.qx;
    if(distancex(line,point) <= m)
        return false;
    line.px = points[a].x - m * line.qy;
    line.py = points[a].y + m * line.qx;
    if(distancex(line,point) <= m)
        return false;

    return true;
}

bool judge(void)
{
    int i,j,k;

    for(i = 0;i < n;++i)
        for(j = i + 1;j < n;++j)
            si[i][j] = si[j][i] = asin(2 * m / dis[i][j]);

    for(i = 0;i < n;++i)
    	if((j = (i + 1) % n,k = (j + 1) % n,false) || !work(i,j,k) || !work(i,k,j) || !work(j,k,i))
    		return false;
    return true;
}

int main(void)
{
    int i,j;
    double l,r;

    //freopen("x.in","r",stdin);

    scanf("%d",&n);
    for(i = 0;i < n;++i)
        scanf("%d %d",&points[i].first,&points[i].second);

    l = 0;
    r = 1e100;
    for(i = 0;i < n;++i)
        for(j = i + 1;j < n;++j)
            r = std::min(r,dis[i][j] = dis[j][i] = distancex(points[i],points[j]));
    for(i = 0;i < n;++i)
        for(j = 0;j < n;++j)
            if(i != j)
                ta[i][j] = atan2(points[j].second - points[i].second,points[j].first - points[i].first);

    while(r - l >= 1e-7 * std::max(1.0,l))
    {
        m = (l + r) / 2;
        if(judge())
            l = m;
        else
            r = m;
    }

    printf("%.10lf\n",(r + l) / 2);

    return 0;
}


#include <cstdio>

int n,m;
int blocked[200020],gcd[200020];
int factor[22],data[200020],dp[200020],previous[200020];
int result[200020];

inline int gcdx(int a,int b)
{
	if(!a || !b)
		return a + b;
	return gcdx(b % a,a);
}

inline void exgcd(int a,int b,long long &x,long long &y)
{
	long long u,v;
	if(!a || !b)
		return (void)(x = y = 1);
	exgcd(b % a,a,u,v);
	x = v - (b / a) * u;
	y = u;
}

inline int inverse(int x,int p)
{
	long long u,v;
	exgcd(x,p,u,v);
	return (u % p + p) % p;
}

int main(void)
{
	int i,j,k;
	long long z;

	scanf("%d %d",&n,&m);
	for(i = 0;i < n;++i)
		scanf("%d",&j),blocked[j] = true;

	k = m;
	for(i = 2;i <= k;++i)
		if((k % i) == 0 && (factor[++factor[0]] = i))
			do k /= i; while((k % i) == 0);

	for(i = 0;i < m;++i)
		gcd[i] = gcdx(i,m);

	for(i = 1;i < m;++i)
		if((m % i) == 0)
			for(j = i;j < m;j += i)
				if(gcd[j] == i && !blocked[j])
					++data[i];
	dp[1] = data[1];
	for(i = 1;i < m;++i)
		if((m % i) == 0)
			for(j = 1;j <= factor[0];++j)
				if((m % (z = 1ll * factor[j] * i)) == 0 && dp[i] + data[z] > dp[z])
					dp[z] = dp[i] + data[z],previous[z] = i;

	factor[0] = 0;
	i = m;
	while((i = previous[i]))
		factor[++factor[0]] = i;

	for(i = factor[0];i >= 1;--i)
		for(j = factor[i];j < m;j += factor[i])
			if(gcd[j] == factor[i] && !blocked[j])
				result[++result[0]] = j;

	for(i = result[0];i >= 2;--i)
		result[i] = 1ll * result[i] / gcd[result[i - 1]] * inverse(result[i - 1] / gcd[result[i - 1]],m / gcd[result[i - 1]]) % m;

	if(!blocked[0])
		result[++result[0]] = 0;
	printf("%d\n",result[0]);
	for(i = 1;i <= result[0];++i)
		printf("%d%c",result[i]," \n"[i == result[0]]);

	return 0;
}


#include <cstdio>

int n,m;
int blocked[200020],gcd[200020];
int factor[22];
int result[200020];

inline int gcdx(int a,int b)
{
	if(!a || !b)
		return a + b;
	return gcdx(b % a,a);
}

inline void exgcd(int a,int b,long long &x,long long &y)
{
	long long u,v;
	if(!a || !b)
		return (void)(x = y = 1);
	exgcd(b % a,a,u,v);
	x = v - (b / a) * u;
	y = u;
}

inline int inverse(int x,int p)
{
	long long u,v;
	exgcd(x,p,u,v);
	return (u % p + p) % p;
}

int main(void)
{
	int i,j,k;

	scanf("%d %d",&n,&m);
	for(i = 0;i < n;++i)
		scanf("%d",&j),blocked[j] = true;

	factor[++factor[0]] = 1;
	for(i = 2;i <= m;++i)
		while((m % i) == 0)
			factor[++factor[0]] = i,m /= i;
	for(i = 2;i <= factor[0];++i)
		factor[i] *= factor[i - 1];
	m = factor[factor[0]];

	for(i = 0;i < m;++i)
		gcd[i] = gcdx(i,m);

	for(i = 1;i <= factor[0];++i)
		for(j = 1;1ll * factor[i] * j < m;++j)
			if(gcd[factor[i] * j] == factor[i] && !blocked[factor[i] * j])
				result[++result[0]] = factor[i] * j;

	for(i = result[0];i >= 2;--i)
		result[i] = 1ll * result[i] / gcd[result[i - 1]] * inverse(result[i - 1] / gcd[result[i - 1]],m / gcd[result[i - 1]]) % m;

	if(!blocked[0])
		result[++result[0]] = 0;
	printf("%d\n",result[0]);
	for(i = 1;i <= result[0];++i)
		printf("%d%c",result[i]," \n"[i == result[0]]);

	return 0;
}


#include<bits/stdc++.h>
#define sqr(x) ((x)*(x))
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define ld long double
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define debuge cerr<<"isok"<<endl
#define debug(x) cerr<<#x<<"="<<x<<endl
#define SS second
#define FF first
#define ls (k<<1)
#define rs (k<<1|1)
#define inf 0x3f3f3f3f
#define clr(a,x) memset(a,x,sizeof(a))
using namespace std;

const int N=200005,M=100005,mod=1e9+7;
template<class T> inline void gmin(T &x,const T &y){if(x>y) x=y;}
template<class T> inline void gmax(T &x,const T &y){if(x<y) x=y;}
inline void ch(int &x,int y){x+=y;if(x>=mod) x-=mod;}

inline int gcd(int x,int y){return y?gcd(y,x%y):x;}
int n,m,f[N],dp[N],from[N],now=1;
vi ans,g[N];
bool vis[N];

inline void gcd(int x,int y,int &a,int &b){
	if(!y){
		a=1;b=0;
		return ;
	}
	int ta,tb;gcd(y,x%y,ta,tb);
	a=tb;b=-tb*(x/y)+ta;
}
int inv(int x,int mod){
	int a,b;
	gcd(x,mod,a,b);
	return a=(a%mod+mod)%mod;
}
int get(int x,int y,int mod){
//	printf("%d %d %d\n",x,y,mod);
	return (ll)y*inv(x,mod)%mod;
}

void solve(int x){
	if(x!=1&&x!=from[x]) solve(from[x]);
	if(x==m&&g[x].size()>0){ans.pb(0);return;}
	for(int i:g[x]){
		int p=gcd(now,m),q=gcd(i,m),k=1;
		if(p!=q) k=q/p;
		k=(ll)k*get(now/p,i/q,m/p)%m;
		ans.pb(k);now=i;
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++) f[gcd(i,m)]++;
	for(int i=1;i<=n;i++){
		int x;scanf("%d",&x);
		vis[x]=1;f[gcd(x,m)]--;
	}
	for(int i=1;i<=m;i++)
		if(!vis[i]) g[gcd(i,m)].pb(i);
	for(int i=1;i<=m;i++) dp[i]=f[i],from[i]=i;
	for(int i=1;i<=m;i++){
		if(!f[i]) continue;
		for(int j=i+i;j<=m;j+=i)
			if(dp[i]+f[j]>dp[j]) dp[j]=f[j]+dp[i],from[j]=i;
	}
	printf("%d\n",dp[m]);
	solve(m);
	for(auto i:ans)printf("%d ",i);
	puts("");
//	int now=1;for(auto i:ans) now=(ll)now*i%m,printf("%d ",now);
	return 0;
}


#include<bits/stdc++.h>
#define sqr(x) ((x)*(x))
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define ld long double
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define debuge cerr<<"isok"<<endl
#define debug(x) cerr<<#x<<"="<<x<<endl
#define SS second
#define FF first
#define ls (k<<1)
#define rs (k<<1|1)
#define inf 0x3f3f3f3f
#define clr(a,x) memset(a,x,sizeof(a))
using namespace std;

const int N=200005,M=100005,mod=1e9+7;
template<class T> inline void gmin(T &x,const T &y){if(x>y) x=y;}
template<class T> inline void gmax(T &x,const T &y){if(x<y) x=y;}
inline void ch(int &x,int y){x+=y;if(x>=mod) x-=mod;}

inline int gcd(int x,int y){return y?gcd(y,x%y):x;}
int n,m,f[N],dp[N],from[N],now=1;
vi ans,g[N];
bool vis[N],go[N];

inline void gcd(int x,int y,int &a,int &b){
	if(!y){
		a=1;b=0;
		return ;
	}
	int ta,tb;gcd(y,x%y,ta,tb);
	a=tb;b=-tb*(x/y)+ta;
}
int inv(int x,int mod){
	int a,b;
	gcd(x,mod,a,b);
	return a=(a%mod+mod)%mod;
}
int get(int x,int y,int mod){
//	printf("%d %d %d\n",x,y,mod);
	return (ll)y*inv(x,mod)%mod;
}

void solve(int x){
	if(x!=1&&x!=from[x]) solve(from[x]);
	if(x==m&&g[x].size()>0){ans.pb(0);return;}
	for(int i:g[x]){
		int p=gcd(now,m),q=gcd(i,m),k=1;
		if(p!=q) k=q/p;
		k=(ll)k*get(now/p,i/q,m/p)%m;
		ans.pb(k);now=i;
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++) f[gcd(i,m)]++;
	for(int i=1;i<=n;i++){
		int x;scanf("%d",&x);
		vis[x]=1;f[gcd(x,m)]--;
		go[gcd(x,m)]=1;
	}
	for(int i=0;i<m;i++)
		if(!vis[i]) g[gcd(i,m)].pb(i);
	for(int i=1;i<=m;i++) dp[i]=f[i],from[i]=i;
	for(int i=1;i<=m;i++){
		if(!go[i]) continue;
		for(int j=i+i;j<=m;j+=i)
			if(dp[i]+f[j]>dp[j]) dp[j]=f[j]+dp[i],from[j]=i;
	}
	printf("%d\n",dp[m]);
	solve(m);
	for(auto i:ans)printf("%d ",i);
	puts("");
//	int now=1;for(auto i:ans) now=(ll)now*i%m,printf("%d ",now);
	return 0;
}


#include<bits/stdc++.h>
#define sqr(x) ((x)*(x))
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define ld long double
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define debuge cerr<<"isok"<<endl
#define debug(x) cerr<<#x<<"="<<x<<endl
#define SS second
#define FF first
#define ls (k<<1)
#define rs (k<<1|1)
#define inf 0x3f3f3f3f
#define clr(a,x) memset(a,x,sizeof(a))
using namespace std;

const int N=200005,M=100005,mod=1e9+7;
template<class T> inline void gmin(T &x,const T &y){if(x>y) x=y;}
template<class T> inline void gmax(T &x,const T &y){if(x<y) x=y;}
inline void ch(int &x,int y){x+=y;if(x>=mod) x-=mod;}

inline int gcd(int x,int y){return y?gcd(y,x%y):x;}
int n,m,f[N],dp[N],from[N],now=1;
vi ans,g[N];
bool vis[N],go[N];

inline void gcd(int x,int y,int &a,int &b){
	if(!y){
		a=1;b=0;
		return ;
	}
	int ta,tb;gcd(y,x%y,ta,tb);
	a=tb;b=-tb*(x/y)+ta;
}
int inv(int x,int mod){
	int a,b;
	gcd(x,mod,a,b);
	return a=(a%mod+mod)%mod;
}
int get(int x,int y,int mod){
//	printf("%d %d %d\n",x,y,mod);
	return (ll)y*inv(x,mod)%mod;
}

void solve(int x){
	if(x!=1&&x!=from[x]) solve(from[x]);
	if(x==m&&g[x].size()>0){ans.pb(0);return;}
	for(int i:g[x]){
		int p=gcd(now,m),q=gcd(i,m),k=1;
		if(p!=q) k=q/p;
		k=(ll)k*get(now/p,i/q,m/p)%m;
		ans.pb(k);now=i;
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++) f[gcd(i,m)]++,go[gcd(i,m)]=1;
	for(int i=1;i<=n;i++){
		int x;scanf("%d",&x);
		vis[x]=1;f[gcd(x,m)]--;
	}
	for(int i=0;i<m;i++)
		if(!vis[i]) g[gcd(i,m)].pb(i);
	for(int i=1;i<=m;i++) dp[i]=f[i],from[i]=i;
	for(int i=1;i<=m;i++){
		if(!go[i]) continue;
		for(int j=i+i;j<=m;j+=i)
			if(dp[i]+f[j]>dp[j]) dp[j]=f[j]+dp[i],from[j]=i;
	}
	printf("%d\n",dp[m]);
	solve(m);
	for(auto i:ans)printf("%d ",i);
	puts("");
//	int now=1;for(auto i:ans) now=(ll)now*i%m,printf("%d ",now);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#define ll long long
#define FORI(i,a,b) for (int i = a; i <= b; i++)
#define FORL(i,a,b) for (int i = a; i < b; i++)
using namespace std;
const int maxn = 5002;
string s;
int q, length;
int f[maxn][maxn];
int isPal[maxn][maxn];
int main() {
    cin >> s;
    s = " " + s;
    length = s.size() - 1;
    s += " ";
    FORI(i,1,length)
        f[i][i] = isPal[i][i] = 1;
    FORL(i,1,length)
    {
        f[i][i+1] = 2 + (s[i] == s[i+1] ? 1 : 0);
        isPal[i][i+1] = (s[i] == s[i+1]) ? 1 : 0;
    }
    //ll check;
    FORL(len, 2, length)
    {
        FORI(i,1,length - len + 1)
        {
            f[i][i + len] = f[i][i+len-1] + f[i+1][i+len] - f[i+1][i+len-1];
            isPal[i][i+len] = ((s[i] == s[i+len]) ? isPal[i+1][i+len-1] : 0);
            f[i][i+len] += isPal[i][i+len];
        }
    }
    cin >> q;
    while (q--)
    {
        int l,r;
        cin >> l >> r;
        printf("%lld\n", f[l][r]);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#define ll long long
#define FORI(i,a,b) for (int i = a; i <= b; i++)
#define FORL(i,a,b) for (int i = a; i < b; i++)
using namespace std;
const int maxn = 1002;
string s;
int q, length;
ll f[maxn][maxn];
ll isPal[maxn][maxn];
int main() {
    cin >> s;
    s = " " + s;
    length = s.size() - 1;
    s += " ";
    FORI(i,1,length)
        f[i][i] = isPal[i][i] = 1;
    FORL(i,1,length)
    {
        f[i][i+1] = 2 + (s[i] == s[i+1] ? 1 : 0);
        isPal[i][i+1] = (s[i] == s[i+1]) ? 1 : 0;
    }
    //ll check;
    FORL(len, 2, length)
    {
        FORI(i,1,length - len + 1)
        {
            f[i][i + len] = f[i][i+len-1] + f[i+1][i+len] - f[i+1][i+len-1];
            isPal[i][i+len] = ((s[i] == s[i+len]) ? isPal[i+1][i+len-1] : 0);
            f[i][i+len] += isPal[i][i+len];
        }
    }
    cin >> q;
    while (q--)
    {
        int l,r;
        cin >> l >> r;
        printf("%lld\n", f[l][r]);
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long int ll;
typedef long double dl;
#define PI  3.14159265358979323846
#define M   1000000007
ll fast(ll n,ll a,ll m)
{
    if(n==0)
        return 1;
    else if(n==1)
        return a;
    else
    {
        ll r=fast(n/2,a,m);
        if(n%2==0)
        {
            return (((r%m)*(r%m))%m);
        }
        else
        {
            return (((((r%m)*a)%m)*r)%m);
        }

    }
}
int main()
{
    string s;
    cin>>s;
    int n=s.length();
    bool dp[n+1][n+1];
    int p[n+1][n+1];
    int i,j;
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=n;j++)
        {
            dp[i][j]=false;
            if(i==j)
            {
                dp[i][j]=true;
            }
            p[i][j]=0;
        }
    }

    for(i=1;i<=n;i++)
    {
        for(j=i-1;j>=1;j--)
        {
            if(i==j+1)
            {
                if(s[i-1]==s[j-1])
                dp[j][i]=true;
            }
            else
            {
                if(s[i-1]==s[j-1])
                dp[j][i]|=dp[j+1][i-1];
            }
        }
    }
    for(j=1;j<=n;j++)
    {
        for(i=j;i>=1;i--)
        {
            if(i==j)
                p[i][j]=1;
            else
            {
                p[i][j]=p[i+1][j]+p[i][j-1]-p[i+1][j-1]+(dp[i][j]==true?1:0);
            }
        }
    }

    int Q;
    cin>>Q;
    int L,R;
    while(Q--)
    {
        scanf("%d%d",&L,&R);
        ll ans=p[L][R];
        printf("%lld\n",ans);
    }

}


#include <iostream>
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long int ll;
typedef long double dl;
#define PI  3.14159265358979323846
#define M   1000000007
ll fast(ll n,ll a,ll m)
{
    if(n==0)
        return 1;
    else if(n==1)
        return a;
    else
    {
        ll r=fast(n/2,a,m);
        if(n%2==0)
        {
            return (((r%m)*(r%m))%m);
        }
        else
        {
            return (((((r%m)*a)%m)*r)%m);
        }

    }
}
int main()
{
    string s;
    cin>>s;
    int n=s.length();
    bool dp[n+1][n+1];
    int p[n+1][n+1];
    int i,j;
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=n;j++)
        {
            dp[i][j]=false;
            if(i==j)
            {
                dp[i][j]=true;
            }
            p[i][j]=0;
        }
    }

    for(i=1;i<=n;i++)
    {
        for(j=i-1;j>=1;j--)
        {
            if(i==j+1)
            {
                if(s[i-1]==s[j-1])
                dp[j][i]=true;
            }
            else
            {
                if(s[i-1]==s[j-1])
                dp[j][i]|=dp[j+1][i-1];
            }
        }
    }
    for(j=1;j<=n;j++)
    {
        for(i=j;i>=1;i--)
        {
            if(i==j)
                p[i][j]=1;
            else
            {
                p[i][j]=p[i+1][j]+p[i][j-1]-p[i+1][j-1]+(dp[i][j]==true?1:0);
            }
        }
    }

    int Q;
    cin>>Q;
    while(Q--)
    {
        int L,R;
        cin>>L>>R;
        ll ans=p[L][R];
        cout<<ans<<"\n";
    }

}


#include <iostream>
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long int ll;
typedef long double dl;
#define PI  3.14159265358979323846
#define M   1000000007
ll fast(ll n,ll a,ll m)
{
    if(n==0)
        return 1;
    else if(n==1)
        return a;
    else
    {
        ll r=fast(n/2,a,m);
        if(n%2==0)
        {
            return (((r%m)*(r%m))%m);
        }
        else
        {
            return (((((r%m)*a)%m)*r)%m);
        }

    }
}
int main()
{
    string s;
    cin>>s;
    int n=s.length();
    bool dp[n+1][n+1];
    memset(dp,false,sizeof(dp));
    int i,j;
    for(i=1;i<=n;i++)
    {
        dp[i][i]=true;
    }
    for(i=1;i<=n;i++)
    {
        for(j=i-1;j>=1;j--)
        {
            if(i==j+1)
            {
                if(s[i-1]==s[j-1])
                dp[j][i]=true;
            }
            else
            {
                if(s[i-1]==s[j-1])
                dp[j][i]|=dp[j+1][i-1];
            }
        }
    }
    int p[n+1][n+1],cnt[n+1][n+1];
    memset(p,0,sizeof(p));
    memset(cnt,0,sizeof(cnt));
    for(j=1;j<=n;j++)
    {
        for(i=j;i>=1;i--)
        {
            if(i==j)
            {
                p[i][j]=1;
            }
            else
            {
                p[i][j]=p[i+1][j];
                if(dp[i][j]==true)
                    p[i][j]++;
            }
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=i;j<=n;j++)
        {
            if(i==j)
                cnt[i][j]=1;
            else
            {
                cnt[i][j]=cnt[i][j-1]+(dp[i][j]==true?1:0);
            }
        }
    }
    for(j=1;j<=n;j++)
    {
        for(i=j;i>=1;i--)
        {
            if(i==j)
                p[i][j]=1;
            else
            {
                p[i][j]=p[i+1][j]+cnt[i][j];
            }
        }
    }
    int Q;
    cin>>Q;
    while(Q--)
    {
        int L,R;
        cin>>L>>R;
        ll ans=p[L][R];
        cout<<ans<<"\n";
    }

}


#include <iostream>
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long int ll;
typedef long double dl;
#define PI  3.14159265358979323846
#define M   1000000007
ll fast(ll n,ll a,ll m)
{
    if(n==0)
        return 1;
    else if(n==1)
        return a;
    else
    {
        ll r=fast(n/2,a,m);
        if(n%2==0)
        {
            return (((r%m)*(r%m))%m);
        }
        else
        {
            return (((((r%m)*a)%m)*r)%m);
        }

    }
}
int main()
{string s;
    cin>>s;
    int n=s.length();
    bool dp[n+1][n+1];
    int p[n+1][n+1],cnt[n+1][n+1];
    int i,j;
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=n;j++)
        {
            dp[i][j]=false;
            if(i==j)
            {
                dp[i][j]=true;
            }
            p[i][j]=0;
            cnt[i][j]=0;
        }
    }

    for(i=1;i<=n;i++)
    {
        for(j=i-1;j>=1;j--)
        {
            if(i==j+1)
            {
                if(s[i-1]==s[j-1])
                dp[j][i]=true;
            }
            else
            {
                if(s[i-1]==s[j-1])
                dp[j][i]|=dp[j+1][i-1];
            }
        }
    }

    for(j=1;j<=n;j++)
    {
        for(i=j;i>=1;i--)
        {
            if(i==j)
            {
                p[i][j]=1;
            }
            else
            {
                p[i][j]=p[i+1][j];
                if(dp[i][j]==true)
                    p[i][j]++;
            }
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=i;j<=n;j++)
        {
            if(i==j)
                cnt[i][j]=1;
            else
            {
                cnt[i][j]=cnt[i][j-1]+(dp[i][j]==true?1:0);
            }
        }
    }
    for(j=1;j<=n;j++)
    {
        for(i=j;i>=1;i--)
        {
            if(i==j)
                p[i][j]=1;
            else
            {
                p[i][j]=p[i+1][j]+cnt[i][j];
            }
        }
    }
    int Q;
    cin>>Q;
    int L,R;
    while(Q--)
    {
        scanf("%d%d",&L,&R);
        ll ans=p[L][R];
        printf("%lld\n",ans);
    }

}


#include <iostream>
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long int ll;
typedef long double dl;
#define PI  3.14159265358979323846
#define M   1000000007
ll fast(ll n,ll a,ll m)
{
    if(n==0)
        return 1;
    else if(n==1)
        return a;
    else
    {
        ll r=fast(n/2,a,m);
        if(n%2==0)
        {
            return (((r%m)*(r%m))%m);
        }
        else
        {
            return (((((r%m)*a)%m)*r)%m);
        }

    }
}
int main()
{
    string s;
    cin>>s;
    int n=s.length();
    bool dp[n+1][n+1];
    memset(dp,false,sizeof(dp));
    int i,j;
    for(i=1;i<=n;i++)
    {
        dp[i][i]=true;
    }
    for(i=1;i<=n;i++)
    {
        for(j=i-1;j>=1;j--)
        {
            if(i==j+1)
            {
                if(s[i-1]==s[j-1])
                dp[j][i]=true;
            }
            else
            {
                if(s[i-1]==s[j-1])
                dp[j][i]|=dp[j+1][i-1];
            }
        }
    }
    int p[n+1][n+1];
    memset(p,0,sizeof(p));
    for(j=1;j<=n;j++)
    {
        for(i=j;i>=1;i--)
        {
            if(i==j)
            {
                p[i][j]=1;
            }
            else
            {
                p[i][j]=p[i+1][j];
                if(dp[i][j]==true)
                    p[i][j]++;
            }
        }
    }
    int Q;
    cin>>Q;
    while(Q--)
    {
        int L,R;
        cin>>L>>R;
        ll ans=0;
        for(j=R;j>=L;j--)
        {
            ans+=p[L][j];
        }
        cout<<ans<<"\n";
    }

}


/*
      H   H
       \ /
        C
       / \
      H   H
*/

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll, ll>
#define vi          vector<ll>
#define vvi			vector<vi>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        998244353
#define rep(i,a,b)	for(ll i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define tr(c,i)		for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x)	((c).find(x) != (c).end())
#define cpresent(c,x)	(find(all(c),x) != (c).end())
using namespace std;

#define N  5005
ll p = 31, m = 1e9+9, n;
int hash1[N], hash2[N], p_val[N], inv[N];
int dp[25000001];

ll power(ll x, ll y)
{
	if(y==0)
		return 1;
	ll temp = power(x, y/2);

	if(y%2!=0)
		return (((x*temp)%m)*temp)%m;
	else
		return (temp*temp)%m;
}

void preprocess(string s)
{
	ll prev = 1;
	rep(i, 0, n)
	{
		p_val[i] = (int)prev;
		prev = (prev*p)%m;
	}

	rep(i, 0, n)
	inv[i] = (int)power(p_val[i], m-2);
}

void compute_hash(string s)
{
	ll val = 0, val1 = 0;

	rep(i, 0, n)
	{
		val = (val + (ll)((s[i]-'a'+1)*p_val[i]))%m;
		hash1[i] = (int)val;
		val1 = (val1 + (ll)((s[n-i-1]-'a'+1)*p_val[i]))%m;
		hash2[i] = (int)val1;
	}
}

int gethash(int i, int j, int f)
{
	ll p_inv = inv[i];
	if(!f)
		return (p_inv*((ll)((hash1[j] - (i>0?hash1[i-1]:0) +m)%m)))%m;
	else
		return (p_inv*((ll)((hash2[j] - (i>0?hash2[i-1]:0) +m)%m)))%m;
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
//	cin>>TESTS;
	while(TESTS--)
	{
		string s;
		cin >> s;
		n = sz(s);

		preprocess(s);
		compute_hash(s);

		set< pii > s1;

		rep(i, 0, n)
		{
			rep(j, i, n)
			{
				if(gethash(i, j, 0)==gethash(n-j-1, n-i-1, 1))
				{
					s1.insert({i, j});
				}
			}
		}

		memset(dp, 0, sizeof(dp));

		rep(i, 0, n)
		{
			if(s1.find({i, i})!=s1.end())
			dp[i*n+i] = 1;
		}

		rep(i, 0, n-1)
		{
			int j = i+1;
			dp[i*n+j] = dp[i*n+i] + dp[j*n+j];
			if(s1.find({i, j})!=s1.end())
				dp[i*n+j]++;
		}

		rep(l, 3, n+1)
		{
			rep(i, 0, n-l+1)
			{
				int j = i+l-1;
				dp[i*n+j] = dp[i*n+j-1] + dp[(i+1)*n+j] - dp[(i+1)*n+j-1];
				if(s1.find({i, j})!=s1.end())
					dp[i*n+j]++;
			}
		}

		int q;
		cin >> q;

		while(q--)
		{
			int l, r;
			cin >> l >> r;
			l--;
			r--;
			cout << dp[l*n+r] << endl;
		}
	}
	return 0;
}