stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 3.4865605672638873x):
#include<bits/stdc++.h>



using namespace std;



int dp[1<<13];



pair<int,int> a[1010];



int main(){

	int n,m;

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

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

		int cost,k;

		scanf("%d%d",&cost,&k);

		int tmp=0;

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

			int x;

			scanf("%d",&x);

			tmp|=(1<<(x-1));

		}

		a[i].first=cost;a[i].second=tmp;

	}

	

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

	dp[0]=0;

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

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

			dp[a[i].second|j]=min(dp[a[i].second|j],dp[j]+a[i].first);

		}

	}

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

	printf("%d",dp[num]==0x3f3f3f3f?-1:dp[num]);

	return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 3.601158077095202x):
#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;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 3.5515355062837743x):
#include <bits/stdc++.h>



using namespace std;

const long long INF = 1e12;

long long dp[2][(1<<12)+5],v[105];



int main()

{

    int n,m;

    cin >> n >> m;

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

        dp[0][i] = dp[1][i] = INF;

    bool t = 1;

    for (int i = 1; i<=m; i++, t^=1)

    {

        long long c, b = 0;

        int nr;

        cin >> c >> nr;

        for (int j = 1; j<=nr; j++)

        {

            int x;

            cin >> x;

            b|=(1<<(x-1));

        }

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

        {

            dp[t][bit] = dp[!t][bit];

            if ((bit&b))

                dp[t][bit] = min(dp[t][bit],dp[!t][(bit^(bit&b))]+c);

        }

    }

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

        cout << -1;

    else

        cout << dp[!t][(1<<n)-1];

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 3.478295685164982x):
#include <iostream>

#include <vector>

#include <algorithm>

#include <numeric>

#include <functional>

#include <queue>

#include <stack>

#include <bitset>

#include <set>

#include <chrono>

#include <string>

#include <assert.h>



using namespace std;



#define endl '\n'



int main()

{

    ios_base::sync_with_stdio(0);

    cin.tie(0);



    int n, m;

    cin >> n >> m;



    vector<int> cost(1 << n, 1000000000);

    cost[0] = 0;



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

        int mask = 0;



        int a, b; cin >> a >> b;



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

            int u; cin >> u; u--;

            mask |= 1 << u;

        }



        for (int ms = (1 << n) - 1; ms >= 0; --ms)

            cost[ms | mask] = min(cost[ms | mask], cost[ms] + a);

    }



    if (cost.back() == 1000000000)

        cost.back() = -1;



    cout << cost.back() << endl;



    return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 3.445373249572299x):
#include <iostream>

#include <bits/stdc++.h>



using namespace std;

int dp[10000];

int a[1005],b[1005],n,m;

int ans;



void dfs(int pos,int d,int cost)

{

    if(dp[d]!=-1&&cost>dp[d]) return;

    dp[d] = cost;

    if(pos>=m||d>=ans) return;

    dfs(pos+1,d|b[pos],cost+a[pos]);

    dfs(pos+1,d,cost);

}



int main()

{

    memset(dp,-1,sizeof(dp));

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

    int ta,tb;

    ans = 1<<n;--ans;



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

        scanf("%d%d",a+i,&tb);

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

            scanf("%d",&ta);

            b[i]|=(1<<(ta-1));

        }

    }

    dfs(0,0,0);

    printf("%d\n",dp[ans]);

    return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 3.2316312067336943x):
#include <bits/stdc++.h>

using namespace std;

const int maxn = 3e5 + 123;

#define int long long 

int n, m;

pair <int, int> a[maxn];

string s[60], t[60];

int dp[2][5005];

main () { 

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);



    cin >> n >> m;



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

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

    	dp[i][j] = 1e9;

    dp[0][0] = 0;

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

    	int cost, b;

    	int msk = 0;

    	cin >> cost >> b;

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

    		int x;

    		cin >> x;

    		x--;

    		msk |= 1 << x;

    	}

    	int cur = i % 2;

    	int prev = cur ^ 1;



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

    		dp[cur][j] = min(dp[cur][j], dp[prev][j]);

    		dp[cur][j | msk] = min(dp[cur][j | msk], dp[prev][j] + cost);

    	}

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

    		dp[prev][j] = 1e9;

    }

    if(dp[m % 2][(1 << n) - 1] >= 1e9) cout << -1;

    else cout << dp[m % 2][(1 << n) - 1];

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 3.516890961742458x):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll Maxn=1e5+10;

const ll INF=1e13;

ll dp[(1<<14)],cost[Maxn],item[Maxn];

ll N,M;

bool vis[20];

int main() {

    ios::sync_with_stdio(false);

    cin>>N>>M;

    ll limits=(1<<N)-1;

    for(ll i=1;i<=limits;i++)  dp[i]=INF;

    dp[0]=0;

    for(ll i=1;i<=M;i++){

        ll a,b;

        cin>>a>>b;

        memset(vis,0,sizeof(vis));

        for(ll i=1;i<=b;i++){

            ll c;

            cin>>c;

            vis[c]=1;

        }

        ll temp=0,now=N;

        while(now>0){

            if(vis[now]){

                temp|=1;

            }

            now--;

            temp<<=1;

        }

        temp>>=1;

        cost[i]=a;

        item[i]=temp;

    }

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

        for(ll j=1;j<=M;j++){

            ll state=item[j];

            dp[i|state]=min(dp[i|state],cost[j]+dp[i]);

        }

    }

    if(dp[limits]==INF) cout<<"-1";

    else{

        cout<<dp[limits];

    }

    return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 3.2990165750640084x):
#include<bits/stdc++.h>

#define ll long long

#define pr pair<ll,ll>

using namespace std;

const int mod = 313, INF = 1000000000;

ll cost[10005];

bool vis[10005];

vector<int> key;

int main() {

	int n, m,able=0;

	cin >> n >> m;

	for (int i = 0; i <= 10000; i++)cost[i] = INF;

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

		int b, p = 0;

		ll a;

		cin >> a >> b;

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

			int k;

			cin >> k;

			p |= (1 << (k - 1));

		}

		able |= p;

		key.push_back(p);

		vis[p] = 1;

		cost[p] = min(a,cost[p]);

	}

	if (able != (1 << n) - 1) {

		cout << -1;

		return 0;

	}

	for (int i = 0; i < key.size();i++) {

		for (int j = 0; j < key.size();j++) {

			int z= key[i] | key[j];

			if (!vis[z]) {

				key.push_back(z);

				vis[z] = 1;

			}

			cost[z] =min( cost[key[i]] + cost[key[j]], cost[z]);

		}

	}

	cout << cost[(1 << n) - 1];

	return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 3.5026409893686266x):
#include <cstdio>

#include <queue>

#include <iostream>

#include <algorithm>

#include<cstring>

using namespace std;

int dp[1<<12];

int main()

{

    int n,m;

    cin >>n >>m;

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

    dp[0] = 0;

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

    {

        int a,b;

        cin >>a >> b;

        int u=0;

        for(int i=1;i<=b;i++)

        {

            int c;

            cin >> c;

            u = u|(1<<(c-1));

        }

        for(int i=0;i<(1<<12);i++)

        {

            if(dp[i]!=0x3f3f3f3f)

            {

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

            }

        }

    }

    int sum=1;

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

    {

        sum = sum|(1<<i);

    }

    if(dp[sum]==0x3f3f3f3f)

    {

        cout << -1;

    }

    else

    {

        cout << dp[sum];

    }

    return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 4.543300775944312x):
#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;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 4.543745871584489x):
#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;

}