stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define ll long long

using namespace std;

const int mod=1e9+7;

ll m,n,yy[3005],money[3005],dp[10005],vis[20]; 



int main()

{

    std::ios::sync_with_stdio(0); std::cin.tie(0);

    cin>>m>>n;

    for(int i=1;i<=5000;++i) dp[i]=1e15;

    ll a=1<<m,b,c;

    for(int i=0;i<n;++i)

    {

    	cin>>money[i]>>yy[i];

    	ll s=0;

    	for(int j=1;j<=yy[i];++j) 

    	{

    		cin>>b;vis[b]=1;

    		s+=1<<(b-1);

		}

		yy[i]=s;

	}

	for(int i=1;i<=m;++i) if(!vis[i]) {cout<<-1; return 0;} 

	for(int i=0;i<a-1;++i)

	{

		for(int j=0;j<n;++j)

		{

			dp[i|yy[j]]=min(dp[i|yy[j]],dp[i]+money[j]);

		}

	}

	cout<<dp[a-1];

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstring>

#include<algorithm>

#include<cstdio>

#define N 10005

#define INF 0x3f3f3f3f

using namespace std;

long long int d[N];

long long int a[1005];

int v[15];

int s[10005];

int b[1005];

int c[15];

int n;

int m;

int tar;

long long int ans=INF;

int main(){

scanf("%d%d",&n,&m);

//for(int i=1;i<=n;i++){

//l[i][0]=0;

//}

memset(d,0x3f,sizeof(d));

memset(v,0,sizeof(v));

memset(s,0,sizeof(s));



for(int i=1;i<=m;i++){

int x,y;

scanf("%lld%d",&a[i],&y);

int st=0;

for(int j=1;j<=y;j++){

scanf("%d",&c[j]);

v[c[j]]++;

st+=(1<<(c[j]-1));

}

b[i]=st;

}

tar=0;

for(int i=1;i<=n;i++){

tar+=(1<<(i-1));

if(v[i]==0){

printf("-1");

return 0;

}

}

d[0]=0;

for(int i=0;i<=tar;i++){

for(int j=1;j<=m;j++){

if(d[i|b[j]]>d[i]+a[j]){

d[i|b[j]]=d[i]+a[j];

}

}



}

printf("%lld",d[tar]);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<iostream>

#include<cstring>

#include<algorithm>

#include<cmath>

#include<map>

#include<set>

#include<vector>

using namespace std;

#define ll long long

#define db double

ll dp[1<<12+5];

ll a[1005];

ll key[1005];

ll pow2[15];



int main()

{

    ll n,m;



    pow2[0]=1;

    for(ll i=1;i<15;i++)pow2[i]=pow2[i-1]<<1;

    memset(dp,1e9+5,sizeof(dp));



    scanf("%lld %lld",&n,&m);



    dp[0]=0;



    for(ll i=1;i<=m;i++)

    {

        ll b,t=1;

        scanf("%lld %lld",&a[i],&b);

        for(ll j=1;j<=b;j++)

        {

            ll tmp;

            scanf("%lld",&tmp);

            key[i]=key[i]+pow2[tmp-1];

        }

    }



    for(ll j=0;j<pow2[n];j++)

    {

        for(ll i=1;i<=m;i++)

        {

            dp[j|key[i]]=min(dp[j]+a[i],dp[j|key[i]]);

        }

    }





    if(dp[pow2[n]-1]>=1e9+5)

    {

        printf("-1");

        return 0;

    }

    printf("%lld",dp[pow2[n]-1]);

    return 0;

}






// Optimized solution to the same problem (faster):
#pragma GCC optimize (3)

#include<vector>

#include<stack>

#include<bitset>

#include<cstdlib>

#include<cmath>

#include<set>

#include<list>

#include<deque>

#include<map>

#include<queue>

#include<iostream>

#include<cstdio>

#include<cstring>

#include<iomanip>

#include<algorithm>

#include<unordered_map>

/*#include<ext/pb_ds/assoc_container.hpp>

#include<ext/pb_ds/tree_policy.hpp>

#include<ext/pb_ds/hash_policy.hpp>

#include<ext/pb_ds/trie_policy.hpp>

#include<ext/pb_ds/priority_queue.hpp>*/

//#include<ext/rope>

#define ll long long

#define ull unsigned long long

#define INFI 2147483647

#define INFL 9223372036854775807

#define INFU 18446744073709551615

#define maxn 1005

using namespace std;

//using namespace __gnu_pbds;

//using namespace __gnu_cxx;

const double PI=acos(-1.0);

const double eps=1e-6;

int a[maxn],sta[maxn],dp[maxn][5000];

inline int read()

{

    int x=0,f=1;

    char ch=getchar();

    while(ch<'0' || ch>'9'){

        if(ch=='-') f=-1;

        ch=getchar();

    }

    while(ch>='0' && ch<='9'){

        x=x*10+ch-'0';

        ch=getchar();

    }

    return x*f;

}

template<typename T> void debug(T x)

{

    cout<<"value="<<x<<endl;

    return;

}

signed main()

{

	//ios_base::sync_with_stdio(false);

    //cin.tie(0);

    //cout.tie(0);

    int n=read(),m=read();

    for(int i=1;i<=m;i++){

        a[i]=read();

        int b=read();

        for(int j=1;j<=b;j++){

            int c=read();

            c=1<<(c-1);

            sta[i]|=c;

        }

    }

    for(int i=0;i<=m;i++) for(int j=1;j<(1<<n);j++) dp[i][j]=100000001;

    for(int i=1;i<=m;i++){

        for(int j=0;j<(1<<n);j++){

            dp[i][j|sta[i]]=min(dp[i][j|sta[i]],dp[i-1][j]+a[i]);

            dp[i][j]=min(dp[i][j],dp[i-1][j]);

        }

    }

    if(dp[m][(1<<n)-1]==100000001) printf("-1\n");

    else printf("%d\n",dp[m][(1<<n)-1]);

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
//お勉強中

//これは高速ゼータ変換の応用なの？



#include<bits/stdc++.h>

#include<cstdio>

#include<algorithm>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 16;

char cn[cm], * ci = cn;

inline int getint() {

	char C = *ci++;

	int A = C - '0';

	while ((C = *ci++) >= '0') A = A * 10 + C - '0';

	return A;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);



	fread(cn, 1, cm, stdin);



	int N = getint(), M = getint();

	int n = 1 << N;

	int dp[1 << 12];

	rep(i, n) dp[i] = 1e9;



	rep(i, M) {

		int A = getint();

		int B = getint();

		int C = 0;

		rep(j, B) {

			int c = getint();

			C += 1 << (c - 1);

		}

		dp[C] = min(dp[C], A);

	}



	rep(j, N) {

		rep (i, n) {

			if (!(i >> j & 1)) dp[i] = min(dp[i], dp[i | 1 << j]);

		}

	}



	rep1(i, n - 1) {

		for (int j = i - 1 & i; j > 0; j = j - 1 & i) {

			dp[i] = min(dp[i], dp[j] + dp[i ^ j]);

		}

	}



	if (dp[n - 1] >= 1e9) printf("-1");

	else printf("%d", dp[n - 1]);





	Would you please return 0;

}

// Optimized solution to the same problem (faster):
//お勉強中

//これは高速ゼータ変換の応用なの？



//#include<bits/stdc++.h>

#include<cstdio>

#include<algorithm>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 16;

char cn[cm], * ci = cn;

inline int getint() {

	char C = *ci++;

	int A = C - '0';

	while ((C = *ci++) >= '0') A = A * 10 + C - '0';

	return A;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);



	fread(cn, 1, cm, stdin);



	int N = getint(), M = getint();

	int n = 1 << N;

	int dp[1 << 12];

	rep(i, n) dp[i] = 1e9;



	rep(i, M) {

		int A = getint();

		int B = getint();

		int C = 0;

		rep(j, B) {

			int c = getint();

			C += 1 << (c - 1);

		}

		dp[C] = min(dp[C], A);

	}



	rep(j, N) {

		rep (i, n) {

			if (!(i >> j & 1)) dp[i] = min(dp[i], dp[i | 1 << j]);

		}

	}



	rep1(i, n - 1) {

		int k = 1 << 31 - __builtin_clz(i);

		for (int j = i - 1 & i; j >= k; j = j - 1 & i) {

			dp[i] = min(dp[i], dp[j] + dp[i ^ j]);

		}

	}



	if (dp[n - 1] >= 1e9) printf("-1");

	else printf("%d", dp[n - 1]);





	Would you please return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define INF 1LL<<62

#define inf 1000000007



int main() {

	ll n,m,a[1005],b[1005],p[13];

	ll cnt=1;

	for(ll i=0;i<13;i++){

		p[i]=cnt;

		cnt*=2;

	}

	cin>>n>>m;

	for(ll i=0;i<m;i++){

		ll x,y;

		cin>>x>>y;

		a[i]=x;

		ll now=0;

		for(ll j=0;j<y;j++){

			ll next;

			cin>>next;

			next--;

			now=now|p[next];

		}

		b[i]=now;

//		cout <<now<<endl;

	}

	ll dp[100000];

	for(ll i=0;i<100000;i++){

		dp[i]=INF;

	}

	dp[0]=0;

	for(ll i=0;i<100000;i++){

		for(ll j=0;j<m;j++){

			ll cost=a[j];

			ll op=b[j];

			ll next=i|op;

			if(next<100000){

			dp[next]=min(dp[next],dp[i]+cost);

			}

		}

	}

//	cout << ll(pow(2,n))-1;

	if(dp[ll(pow(2,n))-1]==INF){

		cout <<-1;

		return 0;

	}

	cout <<dp[ll(pow(2,n))-1];

	// your code goes here

	return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define INF 1LL<<62

#define inf 1000000007



ll dp[4200];

int main() {

	for(ll i=0;i<4200;i++){

		dp[i]=INF;

	}

	dp[0]=0;

	ll n,m;

	cin>>n>>m;

	for(ll i=0;i<m;i++){

		ll x,y;

		cin>>x>>y;

		ll op=0;

		for(ll j=0;j<y;j++){

			ll now;

			cin>>now;

			now--;

			now=pow(2,now);

			op=op|now;

		}

		for(ll j=0;j<4200;j++){

			if(ll(j|op)<4200){

			dp[j|op]=min(dp[j]+x,dp[j|op]);

			}

		}

	}

	ll ans=0;

	for(ll i=0;i<n;i++){

	  ans=ans|ll(pow(2,i));

	}

//	cout <<ans<<endl;

if(dp[ans]==INF){

	cout << -1;

	return 0;

}

	cout <<dp[ans];

	// your code goes here

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<algorithm>

#include<math.h>

#include<cstdio>

#include<string>

#include<string.h>

#include<list>

#include<queue>

#include<sstream>

#include<vector>

#include<set>

#include<map>

#include<deque>

#include<stack>

using namespace std;

#define debug(x) cout<<"###"<<x<<"###"<<endl;

const int INF=0x3f3f3f3f,MAXN=1e3+5;

typedef long long ll;

int dp[1<<17];

int cost[MAXN],p[MAXN];

int main(){

	int n,m;

	cin>>n>>m;

	memset(p,0,sizeof(p));

	for(int i=1;i<=m;i++){

		int b;

		scanf("%d%d",&cost[i],&b);

		for(int j=0;j<b;j++){

			int t;

			scanf("%d",&t);

			p[i]|=(1<<(t-1));

		}

	}



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

//	int N=(1<<n);

//	for(int i=0;i<(1<<n);i++)dp[i][0]=0;

	dp[0]=0;

	for(int i=1;i<=m;i++){

		for(int s=0;s<(1<<n);s++){

			dp[s|p[i]]=min(dp[s]+cost[i],dp[s|p[i]]);

		}

	}



	if(dp[(1<<n)-1]==INF){

		cout<<"-1"<<endl;

	}

	else{

		cout<<dp[(1<<n)-1]<<endl;

	}

	return 0;

}

/*

2 1

30 2

1 2

*/

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define endl '\n'

#define fast_in ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define mp  make_pair

#define pb  push_back

#define  sz(x)   (int)x.size()

#define  all(x)  x.begin(), x.end()

#define	 allr(x)  x.rbegin(), x.rend()

#define  bit(x)  __builtin_popcountll(x)

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef long double ld;

typedef pair <int,int> pii;

const int inf = (int) 1e9+99999, mod = (int)1e9+7;

const ll linf = (ll) 2e18+99999;

int mul (int x, int y) { return ((ll)x * y) % mod; } 

int sub (int x, int y) { x -= y; if (x < 0) return x + mod; return x; } 

int add (int x, int y) { x += y; if (x >= mod) return x - mod; return x; }

 

const int maxn = (int) 3e5;





#define int ll



int cost[maxn], k[maxn], c[1001][50];

int d[5001][2];



void solve () {

	int n, m; cin >> n >> m;

	for (int i = 1; i <= m; i ++) {

		cin >> cost[i] >> k[i];

		for (int j = 1; j <= k[i]; j ++) {

			cin >> c[i][j];	

			c[i][j] --;

		}

	}





	for (int mask = 0; mask < (1 << n); mask ++)

		for (int j = 0; j <= n; j ++)

		d[mask][j] = d[mask][j] = linf; 

	d[0][0] = 0;



	for (int i = 1; i <= m; i ++) {

		for (int mask = 0; mask < (1 << n); mask ++)

			d[mask][i] = d[mask][i-1];

		

		for (int mask = 0; mask < (1 << n); mask ++) {

			int mask2 = mask; for (int j = 1; j <= k[i]; j ++) mask2 |= (1 << c[i][j]);

			d[mask2][i] = min (d[mask2][i], d[mask][i-1] + cost[i]);		

		}

	}





	int res = d[(1 << n)-1][m];



	if (res == linf) res = -1;

	cout << res;

}



main () { fast_in;

    int t = 1; //cin >> t;

    while (t --) {

    	solve ();    

    }

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define fs first

#define se second

#define pb push_back

using namespace std;



typedef long long ll;

typedef pair<int,int> pii;

typedef double db;



const int maxn=5e5+5;

const ll inf=0x3f3f3f3f;

const ll mod=1e9+7;



int dp[1<<17];



pii p[maxn];



int main(){

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

	int n,m;

	scanf("%d%d",&n,&m);

	for(int i=1;i<=m;i++){

		int x,y;

		scanf("%d%d",&x,&y);

		p[i].fs=x;

		for(int j=1;j<=y;j++){

			int k;

			scanf("%d",&k);

			p[i].se|=(1<<k-1);

		}

	}

	dp[0]=0;

	int N=1<<n;

	for(int i=1;i<=m;i++){

		for(int j=0;j<N;j++){

			dp[j|p[i].se]=min(dp[j|p[i].se],dp[j]+p[i].fs);

		}

	}

	if(dp[N-1]==inf) printf("-1");

	else printf("%d\n",dp[N-1]);

	return 0;

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <cstring>

using namespace std;

const int inf = 1e9;

struct key{

    int cost;

    int box;

} k[1005];

int n;

int cs[1 << 12];



int main() {

    int m;

    cin >> n >> m;

    for (int i = 0; i < m; i++) {

        k[i].box = 0;

    }

    int mx = (1 << n) - 1;

    for (int i = 1; i <= mx; i++) {

        cs[i] = inf;

    }

    for (int i = 0; i < m; i++) {

        int a, b;

        cin >> a >> b;

        k[i].cost = a;

        int tmp;

        for (int j = 0; j < b; j++) {

            scanf("%d", &tmp);

            k[i].box += 1 << (tmp - 1);

        }

        cs[k[i].box] = min(cs[k[i].box], a);

    }

    for (int i = 0; i < m; i++) {

        for (int j = 1; j <= mx; j++) {

            int uni = k[i].box | j;

            if (cs[j] != inf && uni != j) {

                cs[uni] = min(cs[uni], cs[j] + k[i].cost);

            }

        }

    }

    if (cs[mx] != inf) cout << cs[mx];

    else cout << -1;

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <algorithm>

#include <stdio.h>

#include <vector>



using namespace std;



#define LL long long

#define FOR(i,l,r) for(int i = l; i <= r; ++i)

#define REP(i,l,r) for(int i = l; i < r; ++i)

#define pi pair<int,int>

#define mp make_pair



const int maxN = 1e5 + 11;

const LL oo = 1e17 + 11;

const int msk = 1 << 12;



int N,M;

int A[maxN];

vector <int> e[maxN];

LL F[1001][msk];



int main(){

    ios_base::sync_with_stdio(false);

    cin.tie();

    cout.tie();

    //

    cin >> N >> M;

    FOR(i,1,M){

        int d,x;

        cin >> A[i] >> d;

        FOR(j,1,d){

            cin >> x;

            e[i].push_back(x);

        }

    }



    int woosh = 1 << N;

    FOR(i,0,M) REP(j,0,woosh) F[i][j] = oo;

    F[0][0] = 0;

    //

    REP(i,0,M) REP(j,0,woosh) if(F[i][j] < oo){

        F[i + 1][j] = min(F[i + 1][j],F[i][j]); // không chọn i + 1

        int newMask = j;

        REP(k,0,e[i + 1].size()){

            int z = e[i + 1][k];

            newMask |= (1 << (z - 1));

        }

        F[i + 1][newMask] = min(F[i + 1][newMask],F[i][j] + A[i + 1]);

    }

    

    if(F[M][woosh - 1] >= oo) cout << -1;

    else cout << F[M][woosh - 1];



    return 0;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

#define sci(a) scanf("%d", &a)

#define fto(i, a, b) for (int i = a; i <= b; ++i)

#define fdto(i, a, b) for (int i = a; i >= b; --i)

#define scii(a, b) scanf("%d %d", &a, &b)

#define ll long long

#define ff first

#define ss second

#define vi vector<int>

#define pi pair<int, int>

#define maxN 100005

#define oo 1000000007

#define mp make_pair

#define pb push_back

#define bug(a) cout << #a << "= " << a << endl;

#define bugarr(a, x, y) cout << #a << "[" << x << ", " << y << "]= "; for (int i = x; i <= y; ++i) cout << a[i] << ", "; cout << endl;

#define sz(a) (int)a.size()

#define vii vector<pi>

#define db double

#define forit(var, it) for(__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)



using namespace std;



int n, m, a[1005], b[1005], c[1005], f[maxN];



int main() {

    //#ifndef ONLINE_JUDGE

     //   freopen("GetEverything.inp", "r", stdin);

      //  freopen("GetEverything.out", "w", stdout);

   // #endif // ONLINE_JUDGE



    scii(n, m);

    fto(i, 1, m) {

        scii(a[i], b[i]);

        fto(j, 1, b[i]) {

            int x; sci(x); x-=1;

            c[i] += (1 << x);

        }

        //cout << c[i] << "\n";

    }

    int doangioi = (1 << (n));

    fto(i, 0, doangioi) f[i] = -1;

   // cout << doangioi << "\n";

    f[0] = 0;

    fto(i, 1, m) {

        fto(j, 0, doangioi) {

            if (f[j] != -1) {

                int y = j | c[i];

                if (y != j) {

                    if (f[y] == -1) f[y] = f[j] + a[i];

                    else f[y] = min(f[y], f[j] + a[i]);

                }

            }

        }

    }

    printf("%d\n", f[doangioi-1]);



    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cstdio>

#include <cstring>

using namespace std;



long long int gcd(long long int a, long long int b){

	if(b == 0)return a;

	return gcd(b,a % b);

}



long long int lcm(long long int a, long long int b){

	return a * b / gcd(a,b);

}



int main(){

	long long int a[3], m[3], p[3] = {1,1,1}, c[3] = {0};

	while(1){

		memset(c,0,sizeof(c));

		cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2];

		if(!a[0])break;

		for(int i=0;i<3;i++){

			do{

			p[i] = (a[i] * p[i]) % m[i];

			//cout << "Turn " << c[i] << ": " << p[i] << endl;

			c[i] += 1;

			}while(p[i] != 1);

		}

		//cout << c[0] << c[1] << c[2] << endl;

		//int gcd = rec(c[0],rec(c[1],c[2])),

		//lcm = rec(c[0],gcd) * rec(c[1],gcd) * rec(c[2],gcd) * gcd;

			//cout << gcd << " " << lcm << endl;

		cout << lcm(lcm(c[0],c[1]),c[2]) << endl;

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <cstring>



long long gcd(long long a, long long b){

	return b?gcd(b,a % b):a;

}



long long lcm(long long a, long long b){

	return a * b / gcd(a,b);

}



int main(){

	int a[3], m[3], p[3] = {1,1,1}, c[3] = {0};

	while(scanf("%d%d%d%d%d%d",a,m,a+1,m+1,a+2,m+2),a[0]){

		memset(c,0,sizeof(c));

		for(int i=0;i<3;i++){

			do{

			p[i] = a[i] * p[i] % m[i];

			c[i]++;

			}while(p[i] != 1);

		}

		printf("%lld\n",lcm(lcm(c[0],c[1]),c[2]));

	}

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cstdio>

#include <cstring>

using namespace std;



long long int gcd(long long int a, long long int b){

	if(b == 0)return a;

	return gcd(b,a % b);

}



long long int lcm(long long int a, long long int b){

	return a * b / gcd(a,b);

}



int main(){

	int a[3], m[3], p[3] = {1,1,1}, c[3] = {0};

	while(1){

		memset(c,0,sizeof(c));

		cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2];

		if(!a[0])break;

		for(int i=0;i<3;i++){

			do{

			p[i] = a[i] * p[i] % m[i];

			c[i] += 1;

			}while(p[i] != 1);

		}

		cout << lcm(lcm(c[0],c[1]),c[2]) << endl;

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <cstring>



long long gcd(long long a, long long b){

	return b?gcd(b,a % b):a;

}



long long lcm(long long a, long long b){

	return a * b / gcd(a,b);

}



int main(){

	int a[3], m[3], p[3] = {1,1,1}, c[3] = {0};

	while(scanf("%d%d%d%d%d%d",a,m,a+1,m+1,a+2,m+2),a[0]){

		memset(c,0,sizeof(c));

		for(int i=0;i<3;i++){

			do{

			p[i] = a[i] * p[i] % m[i];

			c[i]++;

			}while(p[i] != 1);

		}

		printf("%lld\n",lcm(lcm(c[0],c[1]),c[2]));

	}

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

 

using namespace std;

 

typedef long long ll;

 

#define sz(x) (int)(x).size()

#define mp make_pair

#define pb push_back

#define f first

#define s second

#define lb lower_bound

#define ub upper_bound

#define all(x) x.begin(), x.end()

 

const int MOD = 1000000007;

const ll INF = 1e18;

const int MX = 100001;

 

int main() {

    ios_base::sync_with_stdio(0); cin.tie(0);

 

    int n;

    cin >> n;

    vector <ll> a(n);

    for (int i=0;i<n;i++)

    cin >> a[i];

 

    ll ans[n];

    ans[0] = 0;

    for (int i=0;i<n;i++)

    {

        ans[0] += a[i];

        if (i % 2 == 1)

        ans[0] -= 2*a[i];

    }

 

    ans[0] = ans[0] / 2;

    for (int i=1;i<n;i++) {

        ans[i] = a[i-1] - ans[i-1];

    }

 

    for (int i=0;i<n;i++) {

        cout << ans[i] * 2 << " ";

    }

 



}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

#include <cstring>

#define ll long long

using namespace std;

int a[100005],ans[100005];

int main(){

	int n,i;

	int tmp;

	scanf("%d",&n);

	for(i=0;i<n;i++){

        scanf("%d",&a[i]);

	}

	for(i=0,tmp=a[0];i<n;i++){

        tmp=a[i]-tmp;

	}

	tmp=tmp+a[0];

	tmp=tmp/2;

	for(i=0;i<n;i++){

	    printf("%d ",tmp*2);

        tmp=a[i]-tmp;



	}

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <vector>





using namespace std;



int main() {

	int n;

	cin >> n;

	vector <int> a;

	for (int i = 0; i < n; ++i) {

		int new_element;

		cin >> new_element;

		a.push_back(new_element);

	}

	int b0 = 0;

	for (int i = 0; i < n; ++i) {

		if (i % 2 == 0){

			b0 += a[i];

		}

		else {

			b0 -= a[i];

		}

	}

	cout << b0 << ' ';

	int bi = b0;

	for (int i = 1; i < n; ++i) {

		bi = (a[i - 1] - bi / 2) * 2;

		cout << bi << ' ';

	}

}

	

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

#include <cstring>

#define ll long long

using namespace std;

int a[100005],ans[100005];

int main(){

	int n,i;

	int tmp;

	scanf("%d",&n);

	for(i=0;i<n;i++){

        scanf("%d",&a[i]);

	}

	for(i=0,tmp=a[0];i<n;i++){

        tmp=a[i]-tmp;

	}

	tmp=tmp+a[0];

	tmp=tmp/2;

	for(i=0;i<n;i++){

	    printf("%d ",tmp*2);

        tmp=a[i]-tmp;



	}

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



#define lp(i,n) for(int i = 0; i <n;i++)

#define all(x) (x).begin(),(x).end()

#define pb push_back

#define MP make_pair

#define SZ(v) ((int)((v).size()))

typedef long long ll;



const ll MOD = 1e9 + 7;

const int OO = (int)1e6;

const int N = (int)1e7;



void init(){

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

}

int a[N];

int main() {

	#   ifdef LOCAL_PROJECT

        freopen("in.txt", "r", stdin);

    #   endif

    

    int n;

    scanf("%d",&n);

    int cnt =0;

    

    for(int i=1;i<=n;i++){

        scanf("%d",&a[i]);

        if(i&1)cnt+=a[i];

        else cnt-=a[i];

    }

    

    for(int i=1;i<=n;i++){

        printf("%d ",cnt);

        cnt=(a[i]-cnt/2)*2;

        

    }

    

    return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

#include <cstring>

#define ll long long

using namespace std;

int a[100005],ans[100005];

int main(){

	int n,i;

	int tmp;

	scanf("%d",&n);

	for(i=0;i<n;i++){

        scanf("%d",&a[i]);

	}

	for(i=0,tmp=a[0];i<n;i++){

        tmp=a[i]-tmp;

	}

	tmp=tmp+a[0];

	tmp=tmp/2;

	for(i=0;i<n;i++){

	    printf("%d ",tmp*2);

        tmp=a[i]-tmp;



	}

    return 0;

}


// Fastest known solution for this problem: