#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll d[100010];
vector <ll> g[100010];
vector <ll> v[100010];
vector <pair<ll,ll> > res;
int main() {
    ll n,k,cnt=0,s,prev=1,edges=0,val=-1;
    cin>>n>>k;
    for(ll i=0;i<n;i++)
    {
        cin>>d[i];
        //cout<<d[i]<<endl;
        val = max(val,d[i]);
        if(d[i]==0)
        {
            s=i+1;
            cnt++;
        }
        v[d[i]].push_back(i+1);
    }
    if(cnt==0||cnt>1)
    {
        //cout<<cnt<<endl;
        cout<<"-1"<<endl;
        return 0;
    }
    for(ll i=1;i<=val;i++)
    {
        // cout<<v[i].size()<<endl;
        if(v[i].size()==0)
        {
            cout<<"-1"<<endl;
            break;
        }
        if((ll)prev*k>=v[i].size())
        {
            if(i==1)
            {
                for(ll j=0;j<v[i].size();j++)
                {
                    //cout<<"ppp"<<endl;
                    //g[s].push_back(v[i][j]);
                    //g[v[i][j]].push_back(s);
                    res.push_back(make_pair(s,v[i][j]));
                    edges+=2;
                }
            }
            else
            {
                ll l = 0;
                for(ll j=0;j<v[i-1].size();j++)
                {
                    ll temp = k;
                    while(temp--&&l<(ll)v[i].size())
                    {
                        res.push_back(make_pair(v[i][l],v[i-1][j]));
                        //g[v[i][l]].push_back(v[i-1][j]);
                        //g[v[i-1][j]].push_back(v[i][l]);
                        l++;
                        edges+=2;
                    }
                    if(l>=(ll)v[i].size())
                    break;
                }
            }
        }
        else
        {
            cout<<"-1"<<endl;
            return 0;
        }
        prev = (ll)v[i].size();
    }
    cout<<res.size()<<endl;
    for(ll i=0;i<res.size();i++)
        cout<<res[i].first<<" "<<res[i].second<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int N=2e5+20;
//题意:给出n个点的最短距离数组d[i],每个顶点的度数<=K,构造出满足条件的图,输出边
struct node{
	int id,x;
}d[N];
bool cmp(node a,node b)
{
	return a.x<b.x;
}
int n,K;
vector<ii> ans;
vector<ii> pre,now;// 距离为k-1的点,使用次数
int main()
{
	while(cin>>n>>K)
	{

		int u=-1;
		bool flag=true;
		for(int i=1;i<=n;i++)
			scanf("%d",&d[i].x),d[i].id=i;
		sort(d+1,d+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			if(d[i].x==0)
			{
				if(u==-1)
					u=i;
				else
					flag=false;
			}
		}
		if(u==-1||flag==false)
		{
			puts("-1");
			continue;
		}

		ans.clear(),pre.clear(),now.clear();
		int i=2,j=0;
		pre.push_back(ii(u,K));
		for(int k=1;k<n;k++)//最短距离为k时
		{
			if(pre.empty()&&i<=n)
			{
				flag=false;
				break;
			}
			while(i<=n&&d[i].x==k)
			{
				if(pre[j].second==0)
					j++;
				else
					pre[j].second--;
				if(j>=pre.size())
				{
					flag=false;
					break;
				}
				ans.push_back(ii(pre[j].first,i));
				now.push_back(ii(i,K-1));
				i++;
			}
			j=0;
			pre.clear();
			pre=now;
			now.clear();
		}
		if(flag==false)
		{
			puts("-1");
			continue;
		}
		cout<<ans.size()<<endl;
		for(int i=0;i<ans.size();i++)
			cout<<d[ans[i].first].id<<' '<<d[ans[i].second].id<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int N=2e5+20;
//题意:给出n个点的最短距离数组d[i],每个顶点的度数<=K,构造出满足条件的图,输出边
//先将距离d[i] 排序 距离从小到大构造边,
struct node{
	int id,x;
}d[N];
bool cmp(node a,node b)
{
	if(a.x==b.x)
		return a.id<b.id;
	return a.x<b.x;
}
int n,K;
vector<ii> ans;//u-v
vector<ii> pre,now;// 距离为k-1的点,使用次数
int main()
{
	while(cin>>n>>K)
	{
		int u=-1;
		bool flag=true;
		for(int i=1;i<=n;i++)
			scanf("%d",&d[i].x),d[i].id=i;
		sort(d+1,d+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			if(d[i].x==0)
			{
				if(u==-1)
					u=i;
				else
					flag=false;
			}
		}
		if(u==-1||flag==false)
		{
			puts("-1");
			continue;
		}

		ans.clear(),pre.clear(),now.clear();
		int i=2,j;
		pre.push_back(ii(u,K));
		for(int k=1;k<n;k++)//最短距离为k时
		{
			if(pre.empty()&&i<=n)
			{
				flag=false;
				break;
			}
			j=0;//
			while(i<=n&&d[i].x==k)
			{
				if(pre[j].second==0)
					j++;
				else
					pre[j].second--;
				if(j>=pre.size())
				{
					flag=false;
					break;
				}
				ans.push_back(ii(pre[j].first,i));
				now.push_back(ii(i,K-1));
				i++;
			}
			pre.clear();
			pre=now;
			now.clear();
		}
		if(flag==false)
		{
			puts("-1");
			continue;
		}
		cout<<ans.size()<<endl;
		for(int i=0;i<ans.size();i++)
			cout<<d[ans[i].first].id<<' '<<d[ans[i].second].id<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int N=2e5+20;
//题意:给出n个点的最短距离数组d[i],每个顶点的度数<=K,构造出满足条件的图,输出边
struct node{
	int id,x;
}d[N];
bool cmp(node a,node b)
{
	return a.x<b.x;
}
int n,K;
vector<ii> ans;
vector<ii> pre,now;// 距离为k-1的点,使用次数
int main()
{
	while(cin>>n>>K)
	{

		int u=-1;
		bool flag=true;
		for(int i=1;i<=n;i++)
			scanf("%d",&d[i].x),d[i].id=i;
		sort(d+1,d+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			if(d[i].x==0)
			{
				if(u==-1)
					u=i;
				else
					flag=false;
			}
		}
		if(flag==false)
		{
			puts("-1");
			continue;
		}

		ans.clear(),pre.clear(),now.clear();
		int i=2,j=0;
		pre.push_back(ii(1,K));
		for(int k=1;k<=n;k++)//最短距离为k时
		{
			while(d[i].x==k&&i<=n)
			{
				if(pre[j].second==0)
					j++;
				else
					pre[j].second--;
				if(j>=pre.size())
				{
					flag=false;
					break;
				}
				ans.push_back(ii(pre[j].first,i));
				now.push_back(ii(i,K));
				i++;
			}
			pre.clear();
			pre=now;
			now.clear();
		}
		if(flag==false)
		{
			puts("-1");
			continue;
		}
		cout<<ans.size()<<endl;
		for(int i=0;i<ans.size();i++)
			cout<<d[ans[i].first].id<<' '<<d[ans[i].second].id<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int N=2e5+20;
//ÌâÒâ:¸ø³ön¸öµãµÄ×î¶Ì¾àÀëÊý×éd[i],¹¹Ôì³öÂú×ãÌõ¼þµÄÍ¼,Êä³ö±ß
struct node{
	int id,x;
}d[N];
bool cmp(node a,node b)
{
	return a.x<b.x;
}
int n,K;
vector<ii> ans;
int main()
{
	while(cin>>n>>K)
	{

		int u=-1;
		bool flag=true;
		for(int i=1;i<=n;i++)
			scanf("%d",&d[i].x),d[i].id=i;
		sort(d+1,d+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			if(d[i].x==0)
			{
				if(u==-1)
					u=i;
				else
					flag=false;
			}
		}
		if(flag==false)
		{
			puts("-1");
			continue;
		}

		ans.clear();
		int i=2;
		for(int k=1;k<=n;k++)//×î¶Ì¾àÀëÎªkÊ±
		{
			while(d[i].x==k&&i<=n)
			{
				//cout<<u<<' '<<i<<endl;
				ans.push_back(ii(u,i));
				i++;
			}
			u=i-1;
		}
		cout<<ans.size()<<endl;
		for(int i=0;i<ans.size();i++)
			cout<<d[ans[i].first].id<<' '<<d[ans[i].second].id<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int N=2e5+20;
//题意:给出n个点的最短距离数组d[i],每个顶点的度数<=K,构造出满足条件的图,输出边
struct node{
	int id,x;
}d[N];
bool cmp(node a,node b)
{
	return a.x<b.x;
}
int n,K;
vector<ii> ans;
vector<ii> pre,now;// 距离为k-1的点,使用次数
int main()
{
	while(cin>>n>>K)
	{

		int u=-1;
		bool flag=true;
		for(int i=1;i<=n;i++)
			scanf("%d",&d[i].x),d[i].id=i;
		sort(d+1,d+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			if(d[i].x==0)
			{
				if(u==-1)
					u=i;
				else
					flag=false;
			}
		}
		if(u==-1||flag==false)
		{
			puts("-1");
			continue;
		}

		ans.clear(),pre.clear(),now.clear();
		int i=2,j=0;
		pre.push_back(ii(u,K));
		for(int k=1;k<=n;k++)//最短距离为k时
		{
			while(d[i].x==k&&i<=n)
			{
				if(pre[j].second==0)
					j++;
				else
					pre[j].second--;
				if(j>=pre.size())
				{
					flag=false;
					break;
				}
				ans.push_back(ii(pre[j].first,i));
				now.push_back(ii(i,K-1));
				i++;
			}
			pre.clear();
			pre=now;
			now.clear();
		}
		if(flag==false)
		{
			puts("-1");
			continue;
		}
		cout<<ans.size()<<endl;
		for(int i=0;i<ans.size();i++)
			cout<<d[ans[i].first].id<<' '<<d[ans[i].second].id<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int N=2e5+20;
//题意:给出n个点的最短距离数组d[i],每个顶点的度数<=K,构造出满足条件的图,输出边
//先将距离d[i] 排序 距离从小到大构造边,
struct node{
	int id,x;
}d[N];
bool cmp(node a,node b)
{
	if(a.x==b.x)
		return a.id<b.id;
	return a.x<b.x;
}
int n,K;
vector<ii> ans;//u-v
vector<ii> pre,now;// 距离为k-1的点,使用次数
int main()
{
	while(cin>>n>>K)
	{
		int u=-1;
		bool flag=true;
		for(int i=1;i<=n;i++)
			scanf("%d",&d[i].x),d[i].id=i;
		sort(d+1,d+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			if(d[i].x==0)
			{
				if(u==-1)
					u=i;
				else
					flag=false;
			}
		}
		if(u==-1||flag==false)
		{
			puts("-1");
			continue;
		}

		ans.clear(),pre.clear(),now.clear();
		int i=2,j;
		pre.push_back(ii(u,K));
		for(int k=1;k<n;k++)//最短距离为k时
		{
			if(pre.empty()&&i<=n)
			{
				flag=false;
				break;
			}
			j=0;//
			while(i<=n&&d[i].x==k)
			{
				if(pre[j].second==0)
					j++;
				if(j==pre.size())
				{
					flag=false;
					break;
				}
				pre[j].second--;

				ans.push_back(ii(pre[j].first,i));
				now.push_back(ii(i,K-1));
				i++;
			}
			pre.clear();
			pre=now;
			now.clear();
		}
		if(flag==false)
		{
			puts("-1");
			continue;
		}
		cout<<ans.size()<<endl;
		for(int i=0;i<ans.size();i++)
			cout<<d[ans[i].first].id<<' '<<d[ans[i].second].id<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int N=2e5+20;
//题意:给出n个点的最短距离数组d[i],每个顶点的度数<=K,构造出满足条件的图,输出边
struct node{
	int id,x;
}d[N];
bool cmp(node a,node b)
{
	return a.x<b.x;
}
int n,K;
vector<ii> ans;
vector<ii> pre,now;// 距离为k-1的点,使用次数
int main()
{
	while(cin>>n>>K)
	{

		int u=-1;
		bool flag=true;
		for(int i=1;i<=n;i++)
			scanf("%d",&d[i].x),d[i].id=i;
		sort(d+1,d+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			if(d[i].x==0)
			{
				if(u==-1)
					u=i;
				else
					flag=false;
			}
		}
		if(flag==false)
		{
			puts("-1");
			continue;
		}

		ans.clear(),pre.clear(),now.clear();
		int i=2,j=0;
		pre.push_back(ii(1,K));
		for(int k=1;k<=n;k++)//最短距离为k时
		{
			while(d[i].x==k&&i<=n)
			{
				if(pre[j].second==0)
					j++;
				else
					pre[j].second--;
				if(j>=pre.size())
				{
					flag=false;
					break;
				}
				ans.push_back(ii(pre[j].first,i));
				now.push_back(ii(i,K-1));
				i++;
			}
			pre.clear();
			pre=now;
			now.clear();
		}
		if(flag==false)
		{
			puts("-1");
			continue;
		}
		cout<<ans.size()<<endl;
		for(int i=0;i<ans.size();i++)
			cout<<d[ans[i].first].id<<' '<<d[ans[i].second].id<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int N=2e5+20;
struct node{
	int id,x;
}d[N];
bool cmp(node a,node b)
{
	return a.x<b.x;
}
int n,K;
vector<ii> ans;//u-v
vector<ii> pre,now;// 距离为k-1的点,使用次数
int main()
{
	while(cin>>n>>K)
	{
		int u=-1;
		bool flag=true;
		for(int i=1;i<=n;i++)
			scanf("%d",&d[i].x),d[i].id=i;
		sort(d+1,d+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			if(d[i].x==0)
			{
				if(u==-1)
					u=i;
				else
					flag=false;
			}
		}
		if(u==-1||flag==false)
		{
			puts("-1");
			continue;
		}

		ans.clear(),pre.clear(),now.clear();
		int i=2,j;
		pre.push_back(ii(u,K));
		for(int k=1;k<n;k++)//最短距离为k时
		{
			if(pre.empty()&&i<=n)
			{
				flag=false;
				break;
			}
			j=0;//
			while(i<=n&&d[i].x==k)
			{
				if(pre[j].second==0)
					j++;
				if(j==pre.size())
				{
					flag=false;
					break;
				}
				pre[j].second--;

				ans.push_back(ii(pre[j].first,i));
				now.push_back(ii(i,K-1));
				i++;
			}
			pre.clear();
			pre=now;
			now.clear();
		}
		if(flag==false)
		{
			puts("-1");
			continue;
		}
		cout<<ans.size()<<endl;
		for(int i=0;i<ans.size();i++)
			cout<<d[ans[i].first].id<<' '<<d[ans[i].second].id<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int N=2e5+20;
//题意:给出n个点的最短距离数组d[i],每个顶点的度数<=K,构造出满足条件的图,输出边
struct node{
	int id,x;
}d[N];
bool cmp(node a,node b)
{
	return a.x<b.x;
}
int n,K;
vector<ii> ans;
vector<ii> pre,now;// 距离为k-1的点,使用次数
int main()
{
	while(cin>>n>>K)
	{

		int u=-1;
		bool flag=true;
		for(int i=1;i<=n;i++)
			scanf("%d",&d[i].x),d[i].id=i;
		sort(d+1,d+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			if(d[i].x==0)
			{
				if(u==-1)
					u=i;
				else
					flag=false;
			}
		}
		if(u==-1||flag==false)
		{
			puts("-1");
			continue;
		}

		ans.clear(),pre.clear(),now.clear();
		int i=2,j=0;
		pre.push_back(ii(1,K-1));
		for(int k=1;k<=n;k++)//最短距离为k时
		{
			while(d[i].x==k&&i<=n)
			{
				if(pre[j].second==0)
					j++;
				else
					pre[j].second--;
				if(j>=pre.size())
				{
					flag=false;
					break;
				}
				ans.push_back(ii(pre[j].first,i));
				now.push_back(ii(i,K-1));
				i++;
			}
			pre.clear();
			pre=now;
			now.clear();
		}
		if(flag==false)
		{
			puts("-1");
			continue;
		}
		cout<<ans.size()<<endl;
		for(int i=0;i<ans.size();i++)
			cout<<d[ans[i].first].id<<' '<<d[ans[i].second].id<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int N=2e5+20;
//题意:给出n个点的最短距离数组d[i],每个顶点的度数<=K,构造出满足条件的图,输出边
struct node{
	int id,x;
}d[N];
bool cmp(node a,node b)
{
	return a.x<b.x;
}
int n,K;
vector<ii> ans;
vector<ii> pre,now;// 距离为k-1的点,使用次数
int main()
{
	while(cin>>n>>K)
	{

		int u=-1;
		bool flag=true;
		for(int i=1;i<=n;i++)
			scanf("%d",&d[i].x),d[i].id=i;
		sort(d+1,d+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			if(d[i].x==0)
			{
				if(u==-1)
					u=i;
				else
					flag=false;
			}
		}
		if(u==-1||flag==false)
		{
			puts("-1");
			continue;
		}

		ans.clear(),pre.clear(),now.clear();
		int i=2,j=0;
		pre.push_back(ii(u,K));
		for(int k=1;k<n;k++)//最短距离为k时
		{
			if(pre.empty()&&i<=n)
			{
				flag=false;
				break;
			}
			while(d[i].x==k&&i<=n)
			{
				if(pre[j].second==0)
					j++;
				else
					pre[j].second--;
				if(j>=pre.size())
				{
					flag=false;
					break;
				}
				ans.push_back(ii(pre[j].first,i));
				now.push_back(ii(i,K-1));
				i++;
			}
			pre.clear();
			pre=now;
			now.clear();
		}
		if(flag==false)
		{
			puts("-1");
			continue;
		}
		cout<<ans.size()<<endl;
		for(int i=0;i<ans.size();i++)
			cout<<d[ans[i].first].id<<' '<<d[ans[i].second].id<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int N=2e5+20;
//题意:给出n个点的最短距离数组d[i],每个顶点的度数<=K,构造出满足条件的图,输出边
struct node{
	int id,x;
}d[N];
bool cmp(node a,node b)
{
	return a.x<b.x;
}
int n,K;
vector<ii> ans;
vector<ii> pre,now;// 距离为k-1的点,使用次数
int main()
{
	while(cin>>n>>K)
	{

		int u=-1;
		bool flag=true;
		for(int i=1;i<=n;i++)
			scanf("%d",&d[i].x),d[i].id=i;
		sort(d+1,d+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			if(d[i].x==0)
			{
				if(u==-1)
					u=i;
				else
					flag=false;
			}
		}
		if(u==-1||flag==false)
		{
			puts("-1");
			continue;
		}

		ans.clear(),pre.clear(),now.clear();
		int i=2,j=0;
		pre.push_back(ii(1,K));
		for(int k=1;k<=n;k++)//最短距离为k时
		{
			while(d[i].x==k&&i<=n)
			{
				if(pre[j].second==0)
					j++;
				else
					pre[j].second--;
				if(j>=pre.size())
				{
					flag=false;
					break;
				}
				ans.push_back(ii(pre[j].first,i));
				now.push_back(ii(i,K-1));
				i++;
			}
			pre.clear();
			pre=now;
			now.clear();
		}
		if(flag==false)
		{
			puts("-1");
			continue;
		}
		cout<<ans.size()<<endl;
		for(int i=0;i<ans.size();i++)
			cout<<d[ans[i].first].id<<' '<<d[ans[i].second].id<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
int main(){
int n,k;
cin>>n>>k;
vector<pair<int,int>>d(n);
vector<int>distance(n+1);
for(int i=0;i<n;i++){
    int x;
    cin>>x;
    distance[i+1]=x;
    d[i]=mp(x,i+1);
}
sort(d.begin(),d.end());
int i=1;
queue<int>bfs;
bfs.push(d[0].second);
vector<pair<int,int>>edges;
while(!bfs.empty()){
    if(i==n)break;
    int s=bfs.front();
    bfs.pop();
  for(int j=0;j<k;j++){
    if(distance[d[i].second]>distance[s]+1){
        break;

    }
    if(distance[d[i].second]==distance[s]){
        cout<<-1;
        return 0;
    }
    bfs.push(d[i].second);
    edges.pb(mp(s,d[i].second));
  i++;
  if(i==n)break;
  if(s==d[0].second)k--;
  }
  }
cout<<edges.size()<<'\n';
for(auto i:edges){
    cout<<i.first<<' '<<i.second<<'\n';
}
return 0;}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
int main(){
int n,k;
cin>>n>>k;
vector<pair<int,int>>d(n);
vector<int>distance(n+1);
for(int i=0;i<n;i++){
    int x;
    cin>>x;
    distance[i+1]=x;
    d[i]=mp(x,i+1);
}
sort(d.begin(),d.end());
int i=1;
queue<int>bfs;
bfs.push(d[0].second);
vector<pair<int,int>>edges;
while(!bfs.empty()){
    if(i==n)break;
    int s=bfs.front();
    bfs.pop();
  for(int j=0;j<k;j++){
    if(distance[d[i].second]>distance[s]+1){
        break;

    }
    if(distance[d[i].second]==distance[s]){
        cout<<-1;
        return 0;
    }
    bfs.push(d[i].second);
    edges.pb(mp(s,d[i].second));
  i++;
  if(i==n)break;

  }

if(s==d[0].second)k--;
if(k==0){
 cout<<-1;
return 0;}
  }
cout<<edges.size()<<'\n';
for(auto i:edges){
    cout<<i.first<<' '<<i.second<<'\n';
}
return 0;}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
int main(){
int n,k;
cin>>n>>k;
vector<pair<int,int>>d(n);
vector<int>distance(n+1);
for(int i=0;i<n;i++){
    int x;
    cin>>x;
    distance[i+1]=x;
    d[i]=mp(x,i+1);
}
sort(d.begin(),d.end());
int i=1;
queue<int>bfs;
bfs.push(d[0].second);
vector<pair<int,int>>edges;
while(!bfs.empty()){
    if(i==n)break;
if(k==0){
 cout<<-1;
return 0;}

    int s=bfs.front();
    bfs.pop();
  for(int j=0;j<k;j++){
    if(distance[d[i].second]>distance[s]+1){
        break;

    }
    if(distance[d[i].second]<=distance[s]){
        cout<<-1;
        return 0;
    }
    bfs.push(d[i].second);
    edges.pb(mp(s,d[i].second));
  i++;
  if(i==n)break;

  }

if(s==d[0].second)k--;

  }
if(edges.size()==0||d[0].first!=0){
cout<<-1;
return 0;}

cout<<edges.size()<<'\n';
for(auto i:edges){
    cout<<i.first<<' '<<i.second<<'\n';
}
return 0;}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
int main(){
int n,k;
cin>>n>>k;
vector<pair<int,int>>d(n);
vector<int>distance(n+1);
for(int i=0;i<n;i++){
    int x;
    cin>>x;
    distance[i+1]=x;
    d[i]=mp(x,i+1);
}
sort(d.begin(),d.end());
int i=1;
queue<int>bfs;
bfs.push(d[0].second);
vector<pair<int,int>>edges;
while(!bfs.empty()){
    if(i==n)break;
if(k==0){
 cout<<-1;
return 0;}

    int s=bfs.front();
    bfs.pop();
  for(int j=0;j<k;j++){
    if(distance[d[i].second]>distance[s]+1){
        break;

    }
    if(distance[d[i].second]==distance[s]){
        cout<<-1;
        return 0;
    }
    bfs.push(d[i].second);
    edges.pb(mp(s,d[i].second));
  i++;
  if(i==n)break;

  }

if(s==d[0].second)k--;

  }
cout<<edges.size()<<'\n';
for(auto i:edges){
    cout<<i.first<<' '<<i.second<<'\n';
}
return 0;}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
int main(){
int n,k;
cin>>n>>k;
vector<pair<int,int>>d(n);
vector<int>distance(n+1);
for(int i=0;i<n;i++){
    int x;
    cin>>x;
    distance[i+1]=x;
    d[i]=mp(x,i+1);
}
sort(d.begin(),d.end());
int i=1;
queue<int>bfs;
bfs.push(d[0].second);
vector<pair<int,int>>edges;
while(!bfs.empty()){
    if(i==n)break;
    int s=bfs.front();
    bfs.pop();
  for(int j=0;j<k;j++){
    if(distance[d[i].second]==distance[s]){
        cout<<-1;
        return 0;
    }
    bfs.push(d[i].second);
    edges.pb(mp(s,d[i].second));
  i++;
  if(i==n)break;
  }
  }
cout<<edges.size()<<'\n';
for(auto i:edges){
    cout<<i.first<<' '<<i.second<<'\n';
}
return 0;}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
int main(){
int n,k;
cin>>n>>k;
vector<pair<int,int>>d(n);
vector<int>distance(n+1);
for(int i=0;i<n;i++){
    int x;
    cin>>x;
    distance[i+1]=x;
    d[i]=mp(x,i+1);
}
sort(d.begin(),d.end());
int i=1;
queue<int>bfs;
bfs.push(d[0].second);
vector<pair<int,int>>edges;
while(!bfs.empty()){
    if(i==n)break;
if(k==0){
 cout<<-1;
return 0;}

    int s=bfs.front();
    bfs.pop();
  for(int j=0;j<k;j++){
    if(distance[d[i].second]>distance[s]+1){
        break;

    }
    if(distance[d[i].second]==distance[s]){
        cout<<-1;
        return 0;
    }
    bfs.push(d[i].second);
    edges.pb(mp(s,d[i].second));
  i++;
  if(i==n)break;

  }

if(s==d[0].second)k--;

  }
if(edges.size()==0){
cout<<-1;
return 0;}

cout<<edges.size()<<'\n';
for(auto i:edges){
    cout<<i.first<<' '<<i.second<<'\n';
}
return 0;}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
int main(){
int n,k;
cin>>n>>k;
vector<pair<int,int>>d(n);
vector<int>distance(n+1);
for(int i=0;i<n;i++){
    int x;
    cin>>x;
    distance[i+1]=x;
    d[i]=mp(x,i+1);
}
sort(d.begin(),d.end());
int i=1;
queue<int>bfs;
bfs.push(d[0].second);
vector<pair<int,int>>edges;
while(!bfs.empty()){
    if(i==n)break;
    int s=bfs.front();
    bfs.pop();
  for(int j=0;j<k;j++){
    if(distance[d[i].second]>distance[s]+1){
        break;

    }
    if(distance[d[i].second]==distance[s]){
        cout<<-1;
        return 0;
    }
    bfs.push(d[i].second);
    edges.pb(mp(s,d[i].second));
  i++;
  if(i==n)break;

  }

if(s==d[0].second)k--;
  }
cout<<edges.size()<<'\n';
for(auto i:edges){
    cout<<i.first<<' '<<i.second<<'\n';
}
return 0;}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
int main(){
int n,k;
cin>>n>>k;
vector<pair<int,int>>d(n);
vector<int>distance(n+1);
for(int i=0;i<n;i++){
    int x;
    cin>>x;
    distance[i+1]=x;
    d[i]=mp(x,i+1);
}
sort(d.begin(),d.end());
int i=1;
queue<int>bfs;
bfs.push(d[0].second);
vector<pair<int,int>>edges;
while(!bfs.empty()){
    if(i==n)break;
if(k==0){
 cout<<-1;
return 0;}

    int s=bfs.front();
    bfs.pop();
  for(int j=0;j<k;j++){
    if(distance[d[i].second]>distance[s]+1){
        break;

    }
    if(distance[d[i].second]==distance[s]){
        cout<<-1;
        return 0;
    }
    bfs.push(d[i].second);
    edges.pb(mp(s,d[i].second));
  i++;
  if(i==n)break;

  }

if(s==d[0].second)k--;

  }
if(edges.size()==0||d[0].first!=0){
cout<<-1;
return 0;}

cout<<edges.size()<<'\n';
for(auto i:edges){
    cout<<i.first<<' '<<i.second<<'\n';
}
return 0;}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
int main(){
int n,k;
cin>>n>>k;
vector<pair<int,int>>d(n);
vector<int>distance(n+1);
for(int i=0;i<n;i++){
    int x;
    cin>>x;
    distance[i+1]=x;
    d[i]=mp(x,i+1);
}
sort(d.begin(),d.end());
int i=1;
queue<int>bfs;
bfs.push(d[0].second);
vector<pair<int,int>>edges;
while(!bfs.empty()){
    if(i==n)break;
if(k==0){
 cout<<-1;
return 0;}

    int s=bfs.front();
    bfs.pop();
  for(int j=0;j<k;j++){
    if(distance[d[i].second]>distance[s]+1){
        break;

    }
    if(distance[d[i].second]==distance[s]){
        cout<<-1;
        return 0;
    }
    bfs.push(d[i].second);
    edges.pb(mp(s,d[i].second));
  i++;
  if(i==n)break;

  }

if(s==d[0].second)k--;

  }
if(edges.size()==0||d[0].second!=0){
cout<<-1;
return 0;}

cout<<edges.size()<<'\n';
for(auto i:edges){
    cout<<i.first<<' '<<i.second<<'\n';
}
return 0;}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
int main(){
int n,k;
cin>>n>>k;
vector<pair<int,int>>d(n);
vector<int>distance(n+1);
for(int i=0;i<n;i++){
    int x;
    cin>>x;
    distance[i+1]=x;
    d[i]=mp(x,i+1);
}
sort(d.begin(),d.end());
int i=1;
queue<int>bfs;
bfs.push(d[0].second);
vector<pair<int,int>>edges;
while(!bfs.empty()){
    if(i==n)break;
    int s=bfs.front();
    bfs.pop();
  for(int j=0;j<k;j++){
    if(distance[d[i].second]>distance[s]+1){
        break;

    }
    if(distance[d[i].second]==distance[s]){
        cout<<-1;
        return 0;
    }
    bfs.push(d[i].second);
    edges.pb(mp(s,d[i].second));
  i++;
  if(i==n)break;
  }
  }
cout<<edges.size()<<'\n';
for(auto i:edges){
    cout<<i.first<<' '<<i.second<<'\n';
}
return 0;}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
int main(){
int n,k;
cin>>n>>k;
vector<pair<int,int>>d(n);
vector<int>distance(n+1);
for(int i=0;i<n;i++){
    int x;
    cin>>x;
    distance[i+1]=x;
    d[i]=mp(x,i+1);
}
sort(d.begin(),d.end());
int i=1;
queue<int>bfs;
bfs.push(d[0].second);
vector<pair<int,int>>edges;
while(!bfs.empty()){
    if(i==n)break;
if(k==0){
 cout<<-1;
return 0;}

    int s=bfs.front();
    bfs.pop();
  for(int j=0;j<k;j++){
    if(distance[d[i].second]>distance[s]+1){
        break;

    }
    if(distance[d[i].second]<=distance[s]){
        cout<<-1;
        return 0;
    }
    bfs.push(d[i].second);
    edges.pb(mp(s,d[i].second));
  i++;
  if(i==n)break;

  }

if(s==d[0].second)k--;

  }
if(edges.size()==0||d[0].first!=0||i<n){
cout<<-1;
return 0;}

cout<<edges.size()<<'\n';
for(auto i:edges){
    cout<<i.first<<' '<<i.second<<'\n';
}
return 0;}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
int main(){
int n,k;
cin>>n>>k;
vector<pair<int,int>>d(n);
vector<int>distance(n+1);
for(int i=0;i<n;i++){
    int x;
    cin>>x;
    distance[i+1]=x;
    d[i]=mp(x,i+1);
}
sort(d.begin(),d.end());
int i=1;
queue<int>bfs;
bfs.push(d[0].second);
vector<pair<int,int>>edges;
while(!bfs.empty()){
    if(i==n)break;
if(k==0){
 cout<<-1;
return 0;}
  }
    int s=bfs.front();
    bfs.pop();
  for(int j=0;j<k;j++){
    if(distance[d[i].second]>distance[s]+1){
        break;

    }
    if(distance[d[i].second]==distance[s]){
        cout<<-1;
        return 0;
    }
    bfs.push(d[i].second);
    edges.pb(mp(s,d[i].second));
  i++;
  if(i==n)break;

  }

if(s==d[0].second)k--;

cout<<edges.size()<<'\n';
for(auto i:edges){
    cout<<i.first<<' '<<i.second<<'\n';
}
return 0;}


/*
"IF U BELIEVE WHY NOT!!"
                       --Cristiano Ronaldo
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
lli M=1e9+7;
#define INF 1000000007
typedef pair<lli,lli> ll;
#define mem(a,x) memset(a,x,sizeof(a))
lli n,k;
multimap<lli,lli> m;
lli z,q,u,a1,l,r,ax,cx,ay,by,cy,ql,qr,d,x,y;
lli w[1000007];
lli b[4007][4007];
//unordered_map<pair<ll,lli>,lli> mp;
vector<lli> v[400000];
vector<lli> vx[400000];
lli dp[1009][1009];
lli dp1[400000];
map<lli,lli> c2;
map<lli,lli> c1;
lli to;
lli s1[400000];
lli s[400000];
lli f;
multimap<lli,lli> px;
lli solve(lli i,lli pr)
{
 lli cnt=0;
 for(int j=vx[pr+1].size()-1;j>=0;j--)
 {
     if(v[i].size()<k&&v[i].size()>0)
     {
         px.insert(make_pair(i,vx[pr+1][j]));
         v[i].push_back(vx[pr+1][j]);
         v[vx[pr+1][j]].push_back(i);
         cnt++;
         solve(vx[pr+1][j],pr+1);
         vx[pr+1].pop_back();
     }
 }
 return 0;
}
lli tot=0;
lli me(lli a,lli b,lli M)
{
    if(b==0)
    return 1;
    else if(b%2==0)
    return me((a*a)%M,b/2,M);
    else
    return (a%M*me((a*a)%M,(b-1)/2,M)%M)%M;
}
lli mI(lli a,lli m)
{
    return me(a,m-2,m);
}
int main()
{
cin>>n>>k;
map<lli,lli> m1;
lli d=0;
for(int i=1;i<=n;i++)
{
    cin>>x;
    if(x==0)
    d=i;
    m1[x]++;
    vx[x].push_back(i);
}
solve(d,0LL);
map<lli,lli> kj;
for(auto it=px.begin();it!=px.end();it++)
{
    kj[it->first]++;
    kj[it->second]++;
    //cout<<it->first<<" "<<it->second<<endl;
}
for(int i=1;i<=n;i++)
{
    if(kj[i]==0)
    {

        return cout<<-1,0;
    }
}
cout<<px.size()<<endl;
for(auto it=px.begin();it!=px.end();it++)
cout<<it->first<<" "<<it->second<<endl;
}


/*
"IF U BELIEVE WHY NOT!!"
                       --Cristiano Ronaldo
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
lli M=1e9+7;
#define INF 1000000007
typedef pair<lli,lli> ll;
#define mem(a,x) memset(a,x,sizeof(a))
lli n,k;
multimap<lli,lli> m;
lli z,q,u,a1,l,r,ax,cx,ay,by,cy,ql,qr,d,x,y;
lli w[1000007];
lli b[4007][4007];
//unordered_map<pair<ll,lli>,lli> mp;
vector<lli> v[400000];
vector<lli> vx[400000];
lli dp[1009][1009];
lli dp1[400000];
map<lli,lli> c2;
map<lli,lli> c1;
lli to;
lli s1[400000];
lli s[400000];
lli f;
multimap<lli,lli> px;
lli solve(lli i,lli pr)
{
 lli cnt=0;
 for(int j=vx[pr+1].size()-1;j>=0;j--)
 {
     if(v[i].size()<k&&vx[i].size()>0)
     {
         px.insert(make_pair(i,vx[pr+1][j]));
         v[i].push_back(vx[pr+1][j]);
         v[vx[pr+1][j]].push_back(i);
         cnt++;
         solve(vx[pr+1][j],pr+1);
         vx[pr+1].pop_back();
     }
 }
 return 0;
}
lli tot=0;
lli me(lli a,lli b,lli M)
{
    if(b==0)
    return 1;
    else if(b%2==0)
    return me((a*a)%M,b/2,M);
    else
    return (a%M*me((a*a)%M,(b-1)/2,M)%M)%M;
}
lli mI(lli a,lli m)
{
    return me(a,m-2,m);
}
int main()
{
cin>>n>>k;
map<lli,lli> m1;
lli d=0;
for(int i=1;i<=n;i++)
{
    cin>>x;
    if(x==0)
    d=i;
    m1[x]++;
    vx[x].push_back(i);
}
solve(d,0LL);
map<lli,lli> kj;
for(auto it=px.begin();it!=px.end();it++)
{
    kj[it->first]++;
    kj[it->second]++;
    //cout<<it->first<<" "<<it->second<<endl;
}
for(int i=1;i<=n;i++)
{
    if(kj[i]==0)
    {

        return cout<<-1,0;
    }
}
cout<<px.size()<<endl;
for(auto it=px.begin();it!=px.end();it++)
cout<<it->first<<" "<<it->second<<endl;
}


/*
"IF U BELIEVE WHY NOT!!"
                       --Cristiano Ronaldo
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
lli M=1e9+7;
#define INF 1000000007
typedef pair<lli,lli> ll;
#define mem(a,x) memset(a,x,sizeof(a))
lli n,k;
multimap<lli,lli> m;
lli z,q,u,a1,l,r,ax,cx,ay,by,cy,ql,qr,d,x,y;
lli w[1000007];
lli b[4007][4007];
//unordered_map<pair<ll,lli>,lli> mp;
vector<lli> v[400000];
vector<lli> vx[400000];
lli dp[1009][1009];
lli dp1[400000];
map<lli,lli> c2;
map<lli,lli> c1;
lli to;
lli s1[400000];
lli s[400000];
lli f;
multimap<lli,lli> px;
lli solve(lli i,lli pr)
{
 lli cnt=0;
 for(int j=vx[pr+1].size()-1;j>=0;j--)
 {
     if(v[i].size()<k&&vx[pr+1][j]<=n)
     {
         px.insert(make_pair(i,vx[pr+1][j]));
         v[i].push_back(vx[pr+1][j]);
         v[vx[pr+1][j]].push_back(i);
         cnt++;
         solve(vx[pr+1][j],pr+1);
         vx[pr+1].pop_back();
     }
 }
 return 0;
}
lli tot=0;
lli me(lli a,lli b,lli M)
{
    if(b==0)
    return 1;
    else if(b%2==0)
    return me((a*a)%M,b/2,M);
    else
    return (a%M*me((a*a)%M,(b-1)/2,M)%M)%M;
}
lli mI(lli a,lli m)
{
    return me(a,m-2,m);
}
int main()
{
cin>>n>>k;
map<lli,lli> m1;
lli d;
for(int i=1;i<=n;i++)
{
    cin>>x;
    if(x==0)
    d=i;
    m1[x]++;
    vx[x].push_back(i);
}
solve(d,0LL);
map<lli,lli> kj;
for(auto it=px.begin();it!=px.end();it++)
{
    kj[it->first]++;
    kj[it->second]++;
    //cout<<it->first<<" "<<it->second<<endl;
}
for(int i=1;i<=n;i++)
{
    if(kj[i]==0)
    {

        return cout<<-1,0;
    }
}
cout<<px.size()<<endl;
for(auto it=px.begin();it!=px.end();it++)
cout<<it->first<<" "<<it->second<<endl;
}


/*
"IF U BELIEVE WHY NOT!!"
                       --Cristiano Ronaldo
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
lli M=1e9+7;
#define INF 1000000007
typedef pair<lli,lli> ll;
#define mem(a,x) memset(a,x,sizeof(a))
lli n,k;
multimap<lli,lli> m;
lli z,q,u,a1,l,r,ax,cx,ay,by,cy,ql,qr,d,x,y;
lli w[1000007];
lli b[4007][4007];
//unordered_map<pair<ll,lli>,lli> mp;
vector<lli> v[400000];
lli dp[1009][1009];
lli dp1[400000];
map<lli,lli> c2;
map<lli,lli> c1;
lli to;
lli s1[400000];
lli s[400000];
lli f;
multimap<lli,lli> px;
lli solve(lli i,lli pr)
{
 lli cnt=0;
 for(auto it=m.begin();it!=m.end();it++)
 {
     if(it->first==pr+1&&v[i].size()<k&&it->second>0)
     {
         //cout<<i<<" "<<it->second<<endl;
         px.insert(make_pair(i,it->second));
         v[i].push_back(it->second);
         cnt++;
         solve(it->second,pr+1);
         it->second=-1;
     }
 }
 return 0;
}
lli tot=0;
lli me(lli a,lli b,lli M)
{
    if(b==0)
    return 1;
    else if(b%2==0)
    return me((a*a)%M,b/2,M);
    else
    return (a%M*me((a*a)%M,(b-1)/2,M)%M)%M;
}
lli mI(lli a,lli m)
{
    return me(a,m-2,m);
}
int main()
{
cin>>n>>k;
map<lli,lli> m1;
for(int i=1;i<=n;i++)
{
    cin>>x;
    m1[x]++;
    m.insert(make_pair(x,i));
}
for(int i=0;i<=n;i++)
{
    if(i==0)
    {
        if(m1[i]>1)
        return cout<<-1,0;
    }
    else
    {
        if(m1[i]>k*m1[i-1])
        return cout<<-1,0;
    }
}
lli d=m.begin()->second;
m.erase(m.begin());
solve(d,0LL);
cout<<px.size()<<endl;
for(auto it=px.begin();it!=px.end();it++)
cout<<it->first<<" "<<it->second<<endl;
}


/*
"IF U BELIEVE WHY NOT!!"
                       --Cristiano Ronaldo
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
lli M=1e9+7;
#define INF 1000000007
typedef pair<lli,lli> ll;
#define mem(a,x) memset(a,x,sizeof(a))
lli n,k;
multimap<lli,lli> m;
lli z,q,u,a1,l,r,ax,cx,ay,by,cy,ql,qr,d,x,y;
lli w[1000007];
lli b[4007][4007];
//unordered_map<pair<ll,lli>,lli> mp;
vector<lli> v[400000];
lli dp[1009][1009];
lli dp1[400000];
map<lli,lli> c2;
map<lli,lli> c1;
lli to;
lli s1[400000];
lli s[400000];
lli f;
multimap<lli,lli> px;
lli solve(lli i,lli pr)
{
 lli cnt=0;
 for(auto it=m.begin();it!=m.end();it++)
 {
     if(it->first==pr+1&&cnt<=k&&it->second>0)
     {
         //cout<<i<<" "<<it->second<<endl;
         px.insert(make_pair(i,it->second));
         v[i].push_back(it->second);
         cnt++;
         solve(it->second,pr+1);
         it->second=-1;
     }
 }
 return 0;
}
lli tot=0;
lli me(lli a,lli b,lli M)
{
    if(b==0)
    return 1;
    else if(b%2==0)
    return me((a*a)%M,b/2,M);
    else
    return (a%M*me((a*a)%M,(b-1)/2,M)%M)%M;
}
lli mI(lli a,lli m)
{
    return me(a,m-2,m);
}
int main()
{
cin>>n>>k;
map<lli,lli> m1;
for(int i=1;i<=n;i++)
{
    cin>>x;
    m1[x]++;
    m.insert(make_pair(x,i));
}
for(int i=0;i<=n;i++)
{
    if(i==0)
    {
        if(m1[i]>1)
        return cout<<-1,0;
    }
    else
    {
        if(m1[i]>k*m1[i-1])
        return cout<<-1,0;
    }
}
lli d=m.begin()->second;
m.erase(m.begin());
solve(d,0LL);
cout<<px.size()<<endl;
for(auto it=px.begin();it!=px.end();it++)
cout<<it->first<<" "<<it->second<<endl;
}


/*
"IF U BELIEVE WHY NOT!!"
                       --Cristiano Ronaldo
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
lli M=1e9+7;
#define INF 1000000007
typedef pair<lli,lli> ll;
#define mem(a,x) memset(a,x,sizeof(a))
lli n,k;
multimap<lli,lli> m;
lli z,q,u,a1,l,r,ax,cx,ay,by,cy,ql,qr,d,x,y;
lli w[1000007];
lli b[4007][4007];
//unordered_map<pair<ll,lli>,lli> mp;
vector<lli> v[400000];
vector<lli> vx[400000];
lli dp[1009][1009];
lli dp1[400000];
map<lli,lli> c2;
map<lli,lli> c1;
lli to;
lli s1[400000];
lli s[400000];
lli f;
multimap<lli,lli> px;
lli solve(lli i,lli pr)
{
 lli cnt=0;
 for(int j=vx[pr+1].size()-1;j>=0;j--)
 {
     if(v[i].size()<k)
     {
         //cout<<i<<" "<<it->second<<endl;
         px.insert(make_pair(i,vx[pr+1][j]));
         v[i].push_back(vx[pr+1][j]);
         v[vx[pr+1][j]].push_back(i);
         cnt++;
         solve(vx[pr+1][j],pr+1);
         vx[pr+1].pop_back();
     }
 }
 return 0;
}
lli tot=0;
lli me(lli a,lli b,lli M)
{
    if(b==0)
    return 1;
    else if(b%2==0)
    return me((a*a)%M,b/2,M);
    else
    return (a%M*me((a*a)%M,(b-1)/2,M)%M)%M;
}
lli mI(lli a,lli m)
{
    return me(a,m-2,m);
}
int main()
{
cin>>n>>k;
map<lli,lli> m1;
lli d;
for(int i=1;i<=n;i++)
{
    cin>>x;
    if(x==0)
    d=i;
    m1[x]++;
    vx[x].push_back(i);
}
solve(d,0LL);
map<lli,lli> kj;
for(auto it=px.begin();it!=px.end();it++)
{
    kj[it->first]++;
    kj[it->second]++;
}
for(int i=1;i<=n;i++)
{
    if(kj[i]==0)
    return cout<<-1,0;
}
cout<<px.size()<<endl;
for(auto it=px.begin();it!=px.end();it++)
cout<<it->first<<" "<<it->second<<endl;
}


/*
"IF U BELIEVE WHY NOT!!"
                       --Cristiano Ronaldo
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
lli M=1e9+7;
#define INF 1000000007
typedef pair<lli,lli> ll;
#define mem(a,x) memset(a,x,sizeof(a))
lli n,k;
multimap<lli,lli> m;
lli z,q,u,a1,l,r,ax,cx,ay,by,cy,ql,qr,d,x,y;
lli w[1000007];
lli b[4007][4007];
//unordered_map<pair<ll,lli>,lli> mp;
vector<lli> v[400000];
vector<lli> vx[400000];
lli dp[1009][1009];
lli dp1[400000];
map<lli,lli> c2;
map<lli,lli> c1;
lli to;
lli s1[400000];
lli s[400000];
lli f;
multimap<lli,lli> px;
lli solve(lli i,lli pr)
{
 lli cnt=0;
 for(int j=vx[pr+1].size()-1;j>=0;j--)
 {
     if(v[i].size()<k)
     {
         px.insert(make_pair(i,vx[pr+1][j]));
         v[i].push_back(vx[pr+1][j]);
         v[vx[pr+1][j]].push_back(i);
         cnt++;
         solve(vx[pr+1][j],pr+1);
         vx[pr+1].pop_back();
     }
 }
 return 0;
}
lli tot=0;
lli me(lli a,lli b,lli M)
{
    if(b==0)
    return 1;
    else if(b%2==0)
    return me((a*a)%M,b/2,M);
    else
    return (a%M*me((a*a)%M,(b-1)/2,M)%M)%M;
}
lli mI(lli a,lli m)
{
    return me(a,m-2,m);
}
int main()
{
cin>>n>>k;
map<lli,lli> m1;
lli d=0;
for(int i=1;i<=n;i++)
{
    cin>>x;
    if(x==0)
    d=i;
    m1[x]++;
    vx[x].push_back(i);
}
solve(d,0LL);
map<lli,lli> kj;
for(auto it=px.begin();it!=px.end();it++)
{
    kj[it->first]++;
    kj[it->second]++;
    //cout<<it->first<<" "<<it->second<<endl;
}
for(int i=1;i<=n;i++)
{
    if(kj[i]==0||d==0)
    {

        return cout<<-1,0;
    }
}
cout<<px.size()<<endl;
for(auto it=px.begin();it!=px.end();it++)
cout<<it->first<<" "<<it->second<<endl;
}


#include<cstdio>

int m, h1, a1, x1, y1, h2, a2, x2, y2;
int delta1, cycle1, delta2, cycle2, v;

int main() {
	scanf("%d%d%d%d%d%d%d%d%d", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);
	int h = h1;
	for(delta1=0;delta1<=m && h!=a1;delta1++)
		h = (1LL*x1*h+y1)%m;
	if(delta1>m) {
		printf("-1\n");
		return 0;
	}
	for(cycle1=1;cycle1<=m;cycle1++) {
		h = (1LL*x1*h+y1)%m;
		if (h==a1) break;
	}

	h = h2;
	for(delta2=0;delta2<=m && h!=a2;delta2++)
		h = (1LL*x2*h+y2)%m;
	if(delta2>m) {
		printf("-1\n");
		return 0;
	}
	for(cycle2=1;cycle2<=m;cycle2++) {
		h = (1LL*x2*h+y2)%m;
		if (h==a2) break;
	}

	//printf("%d %d %d %d\n", delta1, cycle1, delta2, cycle2);
	v = delta1;
	for(int i=0;i<=cycle2;i++) {
		if(v>=delta2 && (v-delta2)%cycle2==0) {
			printf("%d\n", v);
			return 0;
		}
		v+=cycle1;
	}
	printf("-1\n");

	return 0;
}


#include<cstdio>

int m, h1, a1, x1, y1, h2, a2, x2, y2;
int delta1, cycle1, delta2, cycle2;
long long v;

int main() {
	scanf("%d%d%d%d%d%d%d%d%d", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);
	int h = h1;
	for(delta1=0;delta1<=m && h!=a1;delta1++)
		h = (1LL*x1*h+y1)%m;
	if(delta1>m) {
		printf("-1\n");
		return 0;
	}
	for(cycle1=1;cycle1<=m;cycle1++) {
		h = (1LL*x1*h+y1)%m;
		if (h==a1) break;
	}

	h = h2;
	for(delta2=0;delta2<=m && h!=a2;delta2++)
		h = (1LL*x2*h+y2)%m;
	if(delta2>m) {
		printf("-1\n");
		return 0;
	}
	for(cycle2=1;cycle2<=m;cycle2++) {
		h = (1LL*x2*h+y2)%m;
		if (h==a2) break;
	}

	//printf("%d %d %d %d\n", delta1, cycle1, delta2, cycle2);
	v = delta1;
	for(int i=0;i<=m;i++) {
		if(v>=delta2 && (v-delta2)%cycle2==0) {
			printf("%I64d\n", v);
			return 0;
		}
		v+=cycle1;
	}
	printf("-1\n");

	return 0;
}


#include<cstdio>

int m, h1, a1, x1, y1, h2, a2, x2, y2;
int delta1, cycle1, delta2, cycle2, ans;
long long v;

int main() {
	scanf("%d%d%d%d%d%d%d%d%d", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);
	int h = h1;
	for(delta1=0;delta1<=m && h!=a1;delta1++)
		h = (1LL*x1*h+y1)%m;
	if(delta1>m) {
		printf("-1\n");
		return 0;
	}
	for(cycle1=1;cycle1<=m;cycle1++) {
		h = (1LL*x1*h+y1)%m;
		if (h==a1) break;
	}
	if(h!=a1) cycle1=-1;

	h = h2;
	for(delta2=0;delta2<=m && h!=a2;delta2++)
		h = (1LL*x2*h+y2)%m;
	if(delta2>m) {
		printf("-1\n");
		return 0;
	}
	for(cycle2=1;cycle2<=m;cycle2++) {
		h = (1LL*x2*h+y2)%m;
		if (h==a2) break;
	}
	if(h!=a2) cycle2=-1;

	//printf("%d %d %d %d\n", delta1, cycle1, delta2, cycle2);

	if(cycle1==-1 || cycle2==-1) {
		if(cycle1==-1 && cycle2==-1) ans = (delta1==delta2) ? delta1 : -1;
		else if(cycle1==-1) ans = (delta1>=delta2 && (delta1-delta2)%cycle2==0) ? delta1 : -1;
		else if(cycle2==-1) ans = (delta2>=delta1 && (delta2-delta1)%cycle1==0) ? delta2 : -1;
		else ans = -1;
		printf("%d\n", ans);
		return 0;
	}
	v = delta1;
	for(int i=0;i<=m;i++) {
		if(v>=delta2 && (v-delta2)%cycle2==0) {
			printf("%I64d\n", v);
			return 0;
		}
		v+=cycle1;
	}
	printf("-1\n");

	return 0;
}


#include<cstdio>

int m, h1, a1, x1, y1, h2, a2, x2, y2;
int delta1, cycle1, delta2, cycle2;
long long v;

int main() {
	scanf("%d%d%d%d%d%d%d%d%d", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);
	int h = h1;
	for(delta1=0;delta1<=m && h!=a1;delta1++)
		h = (1LL*x1*h+y1)%m;
	if(delta1>m) {
		printf("-1\n");
		return 0;
	}
	for(cycle1=1;cycle1<=m;cycle1++) {
		h = (1LL*x1*h+y1)%m;
		if (h==a1) break;
	}
	if(h!=a1) cycle1=-1;

	h = h2;
	for(delta2=0;delta2<=m && h!=a2;delta2++)
		h = (1LL*x2*h+y2)%m;
	if(delta2>m) {
		printf("-1\n");
		return 0;
	}
	for(cycle2=1;cycle2<=m;cycle2++) {
		h = (1LL*x2*h+y2)%m;
		if (h==a2) break;
	}
	if(h!=a2) cycle2=-1;
	if (delta1==delta2) {
		printf("%d\n", delta1);
		return 0;
	}
	if(cycle1==-1 || cycle2==-1) {
		printf("-1\n");
		return 0;
	}

	//printf("%d %d %d %d\n", delta1, cycle1, delta2, cycle2);
	v = delta1;
	for(int i=0;i<=m;i++) {
		if(v>=delta2 && (v-delta2)%cycle2==0) {
			printf("%I64d\n", v);
			return 0;
		}
		v+=cycle1;
	}
	printf("-1\n");

	return 0;
}


#include<cstdio>

int m, h1, a1, x1, y1, h2, a2, x2, y2;
int delta1, cycle1, delta2, cycle2;
long long v;

int main() {
	scanf("%d%d%d%d%d%d%d%d%d", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);
	int h = h1;
	for(delta1=0;delta1<=m && h!=a1;delta1++)
		h = (1LL*x1*h+y1)%m;
	if(delta1>m) {
		printf("-1\n");
		return 0;
	}
	for(cycle1=1;cycle1<=m;cycle1++) {
		h = (1LL*x1*h+y1)%m;
		if (h==a1) break;
	}
	if(h!=a1) cycle1=-1;

	h = h2;
	for(delta2=0;delta2<=m && h!=a2;delta2++)
		h = (1LL*x2*h+y2)%m;
	if(delta2>m) {
		printf("-1\n");
		return 0;
	}
	for(cycle2=1;cycle2<=m;cycle2++) {
		h = (1LL*x2*h+y2)%m;
		if (h==a2) break;
	}
	if(h!=a2) cycle2=-1;
	if (delta1==delta2) {
		printf("%d\n", delta1);
	}
	if(cycle1==-1 || cycle2==-1) {
		printf("-1\n");
		return 0;
	}

	//printf("%d %d %d %d\n", delta1, cycle1, delta2, cycle2);
	v = delta1;
	for(int i=0;i<=m;i++) {
		if(v>=delta2 && (v-delta2)%cycle2==0) {
			printf("%I64d\n", v);
			return 0;
		}
		v+=cycle1;
	}
	printf("-1\n");

	return 0;
}


#include<cstdio>

int m, h1, a1, x1, y1, h2, a2, x2, y2;
int delta1, cycle1, delta2, cycle2;
long long v;

int main() {
	scanf("%d%d%d%d%d%d%d%d%d", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);
	int h = h1;
	for(delta1=0;delta1<=m && h!=a1;delta1++)
		h = (1LL*x1*h+y1)%m;
	if(delta1>m) {
		printf("-1\n");
		return 0;
	}
	for(cycle1=1;cycle1<=m;cycle1++) {
		h = (1LL*x1*h+y1)%m;
		if (h==a1) break;
	}

	h = h2;
	for(delta2=0;delta2<=m && h!=a2;delta2++)
		h = (1LL*x2*h+y2)%m;
	if(delta2>m) {
		printf("-1\n");
		return 0;
	}
	for(cycle2=1;cycle2<=m;cycle2++) {
		h = (1LL*x2*h+y2)%m;
		if (h==a2) break;
	}

	printf("%d %d %d %d\n", delta1, cycle1, delta2, cycle2);
	v = delta1;
	for(int i=0;i<=cycle2;i++) {
		if(v>=delta2 && (v-delta2)%cycle2==0) {
			printf("%I64d\n", v);
			return 0;
		}
		v+=cycle1;
	}
	printf("-1\n");

	return 0;
}


#include<cstdio>

int m, h1, a1, x1, y1, h2, a2, x2, y2;
int delta1, cycle1, delta2, cycle2;
long long v;

int main() {
	scanf("%d%d%d%d%d%d%d%d%d", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);
	int h = h1;
	for(delta1=0;delta1<=m && h!=a1;delta1++)
		h = (1LL*x1*h+y1)%m;
	if(delta1>m) {
		printf("-1\n");
		return 0;
	}
	for(cycle1=1;cycle1<=m;cycle1++) {
		h = (1LL*x1*h+y1)%m;
		if (h==a1) break;
	}

	h = h2;
	for(delta2=0;delta2<=m && h!=a2;delta2++)
		h = (1LL*x2*h+y2)%m;
	if(delta2>m) {
		printf("-1\n");
		return 0;
	}
	for(cycle2=1;cycle2<=m;cycle2++) {
		h = (1LL*x2*h+y2)%m;
		if (h==a2) break;
	}

	//printf("%d %d %d %d\n", delta1, cycle1, delta2, cycle2);
	v = delta1;
	for(int i=0;i<=cycle2;i++) {
		if(v>=delta2 && (v-delta2)%cycle2==0) {
			printf("%I64d\n", v);
			return 0;
		}
		v+=cycle1;
	}
	printf("-1\n");

	return 0;
}


#include<cstdio>

int m, h1, a1, x1, y1, h2, a2, x2, y2;
int delta1, cycle1, delta2, cycle2, v;

int main() {
	scanf("%d%d%d%d%d%d%d%d%d", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);
	int h = h1;
	for(delta1=0;delta1<=m && h!=a1;delta1++)
		h = (1LL*x1*h+y1)%m;
	if(delta1>m) {
		printf("-1\n");
		return 0;
	}
	for(cycle1=1;cycle1<=m;cycle1++) {
		h = (1LL*x1*h+y1)%m;
		if (h==a1) break;
	}

	h = h2;
	for(delta2=0;delta2<=m && h!=a2;delta2++)
		h = (1LL*x2*h+y2)%m;
	if(delta2>m) {
		printf("-1\n");
		return 0;
	}
	for(cycle2=1;cycle2<=m;cycle2++) {
		h = (1LL*x2*h+y2)%m;
		if (h==a2) break;
	}

	//printf("%d %d %d %d\n", delta1, cycle1, delta2, cycle2);
	v = delta1;
	for(int i=0;i<cycle2;i++) {
		if(v>=delta2 && (v-delta2)%cycle2==0) {
			printf("%d\n", v);
			return 0;
		}
		v+=cycle1;
	}
	printf("-1\n");

	return 0;
}


#include<stdio.h>
long long ok1[1000001],ok2[1000001];
long long gcd(long long x,long long y)
{
    return y==0?x:gcd(y,x%y);
}
long long abs(long long x)
{
    return x>0?x:-x;
}
int main()
{
    long long m,h1,a1,x1,y1,h2,a2,x2,y2;
    scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&m,&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
    long long nowh1=h1,nowt1=0,nowh2=h2,nowt2=0;
    long long t1dif,t2dif;
    while(1)
    {
        nowh1=(nowh1*x1+y1)%m;
        //printf("%I64d ",nowh1);
        nowt1++;
        if(ok1[nowh1])
        {
            t1dif=nowt1-ok1[nowh1];
            break;
        }
        ok1[nowh1]=nowt1;
    }
    //printf("\n");
    while(1)
    {
        nowh2=(nowh2*x2+y2)%m;
        //printf("%I64d ",nowh2);
        nowt2++;
        if(ok2[nowh2])
        {
            t2dif=nowt2-ok2[nowh2];
            break;
        }
        ok2[nowh2]=nowt2;
    }
    //printf("\n");
    if(ok1[a1]==0||ok2[a2]==0)printf("-1\n");
    else
    {
        long long flagh1=nowh1,flagh2=nowh2;
        int in1=0,in2=0;
        while(1)
        {
            nowh1=(nowh1*x1+y1)%m;
            if(nowh1==a1)
            {
                in1=1;
                break;
            }
            if(nowh1==flagh1)break;
        }
        while(1)
        {
            nowh2=(nowh2*x2+y2)%m;
            if(nowh2==a2)
            {
                in2=1;
                break;
            }
            if(nowh2==flagh2)break;
        }
        long long t1=ok1[a1],t2=ok2[a2];
        if(in1==0&&in2==0)printf("-1\n");
        else
        {
        if(in1==0)
        {
            if(t2>t1||(t1-t2)%t2dif)printf("-1\n");
            else printf("%I64d\n",t1);
        }
        else if(in2==0)
        {
            if(t1>t2||(t2-t1)%t1dif)printf("-1\n");
            else printf("%I64d\n",t2);
        }
        else
        {
            long long div=gcd(t1dif,t2dif);
            if((abs(t2-t1))%div)printf("-1\n");
        else
        {
            while(t1!=t2)
            {
                if(t1<t2)t1=t1+(t2-t1)/t1dif*t1dif;
                if(t1<t2)t1=t1+t1dif;
                if(t1>t2)t2=t2+(t1-t2)/t2dif*t2dif;
                if(t1>t2)t2=t2+t2dif;
            }
            printf("%I64d\n",t1);
        }
        }
        }
    }
    return 0;
}


#include<stdio.h>
int ok1[1000001],ok2[1000001];
long long gcd(long long x,long long y)
{
    return y==0?x:gcd(y,x%y);
}
long long abs(long long x)
{
    return x>0?x:-x;
}
int main()
{
    long long m,h1,a1,x1,y1,h2,a2,x2,y2;
    scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&m,&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
    long long nowh1=h1,nowt1=0,nowh2=h2,nowt2=0;
    long long t1dif,t2dif;
    ok1[h1]=1;
    while(1)
    {
        nowh1=(nowh1*x1+y1)%m;
        //printf("%I64d ",nowh1);
        nowt1++;
        if(ok1[nowh1])
        {
            t1dif=nowt1-ok1[nowh1];
            break;
        }
        ok1[nowh1]=nowt1;
    }
    //printf("\n");
    ok2[h2]=1;
    while(1)
    {
        nowh2=(nowh2*x2+y2)%m;
        //printf("%I64d ",nowh2);
        nowt2++;
        if(ok2[nowh2])
        {
            t2dif=nowt2-ok2[nowh2];
            break;
        }
        ok2[nowh2]=nowt2;
    }
    //printf("\n");
    if(ok1[a1]==0||ok2[a2]==0)printf("-1\n");
    else
    {
        long long t1=ok1[a1],t2=ok2[a2];
        long long div=gcd(t1dif,t2dif);
        if(abs(t2-t1)%div)printf("-1\n");
        else
        {
            while(t1!=t2)
            {
                if(t1<t2)t1=t1+(t2-t1)/t1dif*t1dif;
                if(t1!=t2)t1=t1+t1dif;
                if(t1>t2)t2=t2+(t1-t2)/t2dif*t2dif;
                if(t1!=t2)t2=t2+t2dif;
            }
        printf("%I64d\n",t1);
        }
    }
    return 0;
}


#include<stdio.h>
long long ok1[1000001],ok2[1000001];
long long gcd(long long x,long long y)
{
    return y==0?x:gcd(y,x%y);
}
long long abs(long long x)
{
    return x>0?x:-x;
}
int main()
{
    long long m,h1,a1,x1,y1,h2,a2,x2,y2;
    scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&m,&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
    long long nowh1=h1,nowt1=0,nowh2=h2,nowt2=0;
    long long t1dif,t2dif;
    while(1)
    {
        nowh1=(nowh1*x1+y1)%m;
        //printf("%I64d ",nowh1);
        nowt1++;
        if(ok1[nowh1])
        {
            t1dif=nowt1-ok1[nowh1];
            break;
        }
        ok1[nowh1]=nowt1;
    }
    //printf("\n");
    while(1)
    {
        nowh2=(nowh2*x2+y2)%m;
        //printf("%I64d ",nowh2);
        nowt2++;
        if(ok2[nowh2])
        {
            t2dif=nowt2-ok2[nowh2];
            break;
        }
        ok2[nowh2]=nowt2;
    }
    //printf("\n");
    if(ok1[a1]==0||ok2[a2]==0)printf("-1\n");
    else
    {
        long long flagh1=nowh1,flagh2=nowh2;
        int in1=0,in2=0;
        while(1)
        {
            nowh1=(nowh1*x1+y1)%m;
            if(nowh1==a1)
            {
                in1=1;
                break;
            }
            if(nowh1==flagh1)break;
        }
        while(1)
        {
            nowh2=(nowh2*x2+y2)%m;
            if(nowh2==a2)
            {
                in2=1;
                break;
            }
            if(nowh2==flagh2)break;
        }
        long long t1=ok1[a1],t2=ok2[a2];
        long long div=gcd(t1dif,t2dif);
        if((abs(t2-t1))%div||((in1==0||in2==0)&&t1!=t2))printf("-1\n");
        else
        {
            while(t1!=t2)
            {
                if(t1<t2)t1=t1+(t2-t1)/t1dif*t1dif;
                if(t1<t2)t1=t1+t1dif;
                if(t1>t2)t2=t2+(t1-t2)/t2dif*t2dif;
                if(t1>t2)t2=t2+t2dif;
            }
            printf("%I64d\n",t1);
        }
    }
    return 0;
}


#include<stdio.h>
long long ok1[1000001],ok2[1000001];
long long gcd(long long x,long long y)
{
    return y==0?x:gcd(y,x%y);
}
long long abs(long long x)
{
    return x>0?x:-x;
}
int main()
{
    long long m,h1,a1,x1,y1,h2,a2,x2,y2;
    scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&m,&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
    long long nowh1=h1,nowt1=0,nowh2=h2,nowt2=0;
    long long t1dif,t2dif;
    while(1)
    {
        nowh1=(nowh1*x1+y1)%m;
        //printf("%I64d ",nowh1);
        nowt1++;
        if(ok1[nowh1])
        {
            t1dif=nowt1-ok1[nowh1];
            break;
        }
        ok1[nowh1]=nowt1;
    }
    //printf("\n");
    while(1)
    {
        nowh2=(nowh2*x2+y2)%m;
        //printf("%I64d ",nowh2);
        nowt2++;
        if(ok2[nowh2])
        {
            t2dif=nowt2-ok2[nowh2];
            break;
        }
        ok2[nowh2]=nowt2;
    }
    //printf("\n");
    if(ok1[a1]==0||ok2[a2]==0)printf("-1\n");
    else
    {
        long long t1=ok1[a1],t2=ok2[a2];
        long long div=gcd(t1dif,t2dif);
        if((abs(t2-t1))%div)printf("-1\n");
        else
        {
            while(t1!=t2)
            {
                if(t1<t2)t1=t1+(t2-t1)/t1dif*t1dif;
                if(t1<t2)t1=t1+t1dif;
                if(t1>t2)t2=t2+(t1-t2)/t2dif*t2dif;
                if(t1>t2)t2=t2+t2dif;
            }
            printf("%I64d\n",t1);
        }
    }
    return 0;
}


#include<stdio.h>
int ok1[1000001],ok2[1000001];
long long gcd(long long x,long long y)
{
    return y==0?x:gcd(y,x%y);
}
int main()
{
    long long m,h1,a1,x1,y1,h2,a2,x2,y2;
    scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&m,&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
    long long nowh1=h1,nowt1=0,nowh2=h2,nowt2=0;
    long long t1dif,t2dif;
    ok1[h1]=1;
    while(1)
    {
        nowh1=(nowh1*x1+y1)%m;
        //printf("%I64d ",nowh1);
        nowt1++;
        if(ok1[nowh1])
        {
            t1dif=nowt1-ok1[nowh1];
            break;
        }
        ok1[nowh1]=nowt1;
    }
    //printf("\n");
    ok2[h2]=1;
    while(1)
    {
        nowh2=(nowh2*x2+y2)%m;
        //printf("%I64d ",nowh2);
        nowt2++;
        if(ok2[nowh2])
        {
            t2dif=nowt2-ok2[nowh2];
            break;
        }
        ok2[nowh2]=nowt2;
    }
    //printf("\n");
    if(ok1[a1]==0||ok2[a2]==0)printf("-1\n");
    else
    {
        long long t1=ok1[a1],t2=ok2[a2];
        while(t1!=t2)
        {
            if(t1<t2)t1=t1+(t2-t1)/t1dif*t1dif;
            if(t1!=t2)t1=t1+t1dif;
            if(t1>t2)t2=t2+(t1-t2)/t2dif*t2dif;
            if(t1!=t2)t2=t2+t2dif;
        }

        printf("%I64d\n",t1);
    }
    return 0;
}


#include<stdio.h>
int ok1[1000001],ok2[1000001];
long long gcd(long long x,long long y)
{
    return y==0?x:gcd(y,x%y);
}
int main()
{
    long long m,h1,a1,x1,y1,h2,a2,x2,y2;
    scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&m,&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
    long long nowh1=h1,nowt1=0,nowh2=h2,nowt2=0;
    ok1[h1]=1;
    int ok=1;
    while(1)
    {
        nowh1=(nowh1*x1+y1)%m;
        //printf("%I64d ",nowh1);
        nowt1++;
        if(ok1[nowh1])
        {
            ok=0;
            break;
        }
        if(nowh1==a1)break;
        ok1[nowh1]=1;
    }
    //printf("\n");
    ok2[h2]=1;
    while(1)
    {
        nowh2=(nowh2*x2+y2)%m;
        //printf("%I64d ",nowh2);
        nowt2++;
        if(ok2[nowh2])
        {
            ok=0;
            break;
        }
        if(nowh2==a2)break;
        ok2[nowh2]=1;
    }
    //printf("\n");
    if(ok==0)printf("-1\n");
    else
    {
        long long div=gcd(nowt1,nowt2);
        printf("%I64d\n",nowt1*nowt2/div);
    }
    return 0;
}


#include<stdio.h>
long long ok1[1000001],ok2[1000001];
long long gcd(long long x,long long y)
{
    return y==0?x:gcd(y,x%y);
}
long long abs(long long x)
{
    return x>0?x:-x;
}
int main()
{
    long long m,h1,a1,x1,y1,h2,a2,x2,y2;
    scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&m,&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
    long long nowh1=h1,nowt1=0,nowh2=h2,nowt2=0;
    long long t1dif,t2dif;
    while(1)
    {
        nowh1=(nowh1*x1+y1)%m;
        //printf("%I64d ",nowh1);
        nowt1++;
        if(ok1[nowh1])
        {
            t1dif=nowt1-ok1[nowh1];
            break;
        }
        ok1[nowh1]=nowt1;
    }
    //printf("\n");
    while(1)
    {
        nowh2=(nowh2*x2+y2)%m;
        //printf("%I64d ",nowh2);
        nowt2++;
        if(ok2[nowh2])
        {
            t2dif=nowt2-ok2[nowh2];
            break;
        }
        ok2[nowh2]=nowt2;
    }
    //printf("\n");
    if(ok1[a1]==0||ok2[a2]==0)printf("-1\n");
    else
    {
        long long t1=ok1[a1],t2=ok2[a2];
        long long div=gcd(t1dif,t2dif);
        if(abs(t2-t1)%div)printf("-1\n");
        else
        {
            while(t1!=t2)
            {
                if(t1<t2)t1=t1+(t2-t1)/t1dif*t1dif;
                if(t1!=t2)t1=t1+t1dif;
                if(t1>t2)t2=t2+(t1-t2)/t2dif*t2dif;
                if(t1!=t2)t2=t2+t2dif;
            }
            printf("%I64d\n",t1);
        }
    }
    return 0;
}


#include<stdio.h>
long long ok1[1000001],ok2[1000001];
long long gcd(long long x,long long y)
{
    return y==0?x:gcd(y,x%y);
}
long long abs(long long x)
{
    return x>0?x:-x;
}
int main()
{
    long long m,h1,a1,x1,y1,h2,a2,x2,y2;
    scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&m,&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
    long long nowh1=h1,nowt1=0,nowh2=h2,nowt2=0;
    long long t1dif,t2dif;
    while(1)
    {
        nowh1=(nowh1*x1+y1)%m;
        //printf("%I64d ",nowh1);
        nowt1++;
        if(ok1[nowh1])
        {
            t1dif=nowt1-ok1[nowh1];
            break;
        }
        ok1[nowh1]=nowt1;
    }
    //printf("\n");
    while(1)
    {
        nowh2=(nowh2*x2+y2)%m;
        //printf("%I64d ",nowh2);
        nowt2++;
        if(ok2[nowh2])
        {
            t2dif=nowt2-ok2[nowh2];
            break;
        }
        ok2[nowh2]=nowt2;
    }
    //printf("\n");
    if(ok1[a1]==0||ok2[a2]==0)printf("-1\n");
    else
    {
        long long flagh1=nowh1,flagh2=nowh2;
        int in1=0,in2=0;
        while(1)
        {
            nowh1=(nowh1*x1+y1)%m;
            if(nowh1==a1)
            {
                in1=1;
                break;
            }
            if(nowh1==flagh1)break;
        }
        while(1)
        {
            nowh2=(nowh2*x2+y2)%m;
            if(nowh2==a2)
            {
                in2=1;
                break;
            }
            if(nowh2==flagh2)break;
        }
        long long t1=ok1[a1],t2=ok2[a2];
        if(in1==0||in2==0)
        {
            if(t1!=t2)printf("-1\n");
            else printf("%I64d\n",t1);
        }
        else
        {long long div=gcd(t1dif,t2dif);
        if((abs(t2-t1))%div)printf("-1\n");
        else
        {
            while(t1!=t2)
            {
                if(t1<t2)t1=t1+(t2-t1)/t1dif*t1dif;
                if(t1<t2)t1=t1+t1dif;
                if(t1>t2)t2=t2+(t1-t2)/t2dif*t2dif;
                if(t1>t2)t2=t2+t2dif;
            }
            printf("%I64d\n",t1);
        }}
    }
    return 0;
}


#include<stdio.h>
long long ok1[1000001],ok2[1000001];
long long gcd(long long x,long long y)
{
    return y==0?x:gcd(y,x%y);
}
long long abs(long long x)
{
    return x>0?x:-x;
}
int main()
{
    long long m,h1,a1,x1,y1,h2,a2,x2,y2;
    scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&m,&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
    long long nowh1=h1,nowt1=0,nowh2=h2,nowt2=0;
    long long t1dif,t2dif;
    while(1)
    {
        nowh1=(nowh1*x1+y1)%m;
        //printf("%I64d ",nowh1);
        nowt1++;
        if(ok1[nowh1])
        {
            t1dif=nowt1-ok1[nowh1];
            break;
        }
        ok1[nowh1]=nowt1;
    }
    //printf("\n");
    while(1)
    {
        nowh2=(nowh2*x2+y2)%m;
        //printf("%I64d ",nowh2);
        nowt2++;
        if(ok2[nowh2])
        {
            t2dif=nowt2-ok2[nowh2];
            break;
        }
        ok2[nowh2]=nowt2;
    }
    //printf("\n");
    if(ok1[a1]==0||ok2[a2]==0)printf("-1\n");
    else
    {
        long long flagh1=nowh1,flagh2=nowh2;
        int in1=0,in2=0;
        while(1)
        {
            nowh1=(nowh1*x1+y1)%m;
            if(nowh1==a1)
            {
                in1=1;
                break;
            }
            if(nowh1==flagh1)break;
        }
        while(1)
        {
            nowh2=(nowh2*x1+y1)%m;
            if(nowh2==a2)
            {
                in2=1;
                break;
            }
            if(nowh2==flagh2)break;
        }
        long long t1=ok1[a1],t2=ok2[a2];
        long long div=gcd(t1dif,t2dif);
        if((abs(t2-t1))%div||((in1==0||in2==0)&&t1!=t2))printf("-1\n");
        else
        {
            while(t1!=t2)
            {
                if(t1<t2)t1=t1+(t2-t1)/t1dif*t1dif;
                if(t1<t2)t1=t1+t1dif;
                if(t1>t2)t2=t2+(t1-t2)/t2dif*t2dif;
                if(t1>t2)t2=t2+t2dif;
            }
            printf("%I64d\n",t1);
        }
    }
    return 0;
}


#include<stdio.h>
long long ok1[1000001],ok2[1000001];
long long gcd(long long x,long long y)
{
    return y==0?x:gcd(y,x%y);
}
long long abs(long long x)
{
    return x>0?x:-x;
}
int main()
{
    long long m,h1,a1,x1,y1,h2,a2,x2,y2;
    scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&m,&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
    long long nowh1=h1,nowt1=0,nowh2=h2,nowt2=0;
    long long t1dif,t2dif;
    while(1)
    {
        nowh1=(nowh1*x1+y1)%m;
        //printf("%I64d ",nowh1);
        nowt1++;
        if(ok1[nowh1])
        {
            t1dif=nowt1-ok1[nowh1];
            break;
        }
        ok1[nowh1]=nowt1;
    }
    //printf("\n");
    while(1)
    {
        nowh2=(nowh2*x2+y2)%m;
        //printf("%I64d ",nowh2);
        nowt2++;
        if(ok2[nowh2])
        {
            t2dif=nowt2-ok2[nowh2];
            break;
        }
        ok2[nowh2]=nowt2;
    }
    //printf("\n");
    if(ok1[a1]==0||ok2[a2]==0)printf("-1\n");
    else
    {
        long long flagh1=nowh1,flagh2=nowh2;
        int in1=0,in2=0;
        while(1)
        {
            nowh1=(nowh1*x1+y1)%m;
            if(nowh1==a1)
            {
                in1=1;
                break;
            }
            if(nowh1==flagh1)break;
        }
        while(1)
        {
            nowh2=(nowh2*x2+y2)%m;
            if(nowh2==a2)
            {
                in2=1;
                break;
            }
            if(nowh2==flagh2)break;
        }
        long long t1=ok1[a1],t2=ok2[a2];
        if((in1==0||in2==0)&&t1!=t2)printf("-1\n");
        else
        {long long div=gcd(t1dif,t2dif);
        if((abs(t2-t1))%div)printf("-1\n");
        else
        {
            while(t1!=t2)
            {
                if(t1<t2)t1=t1+(t2-t1)/t1dif*t1dif;
                if(t1<t2)t1=t1+t1dif;
                if(t1>t2)t2=t2+(t1-t2)/t2dif*t2dif;
                if(t1>t2)t2=t2+t2dif;
            }
            printf("%I64d\n",t1);
        }}
    }
    return 0;
}


#include<stdio.h>
int ok1[1000001],ok2[1000001];
long long gcd(long long x,long long y)
{
    return y==0?x:gcd(y,x%y);
}
int main()
{
    long long m,h1,a1,x1,y1,h2,a2,x2,y2;
    scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&m,&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
    long long nowh1=h1,nowt1=0,nowh2=h2,nowt2=0;
    ok1[h1]=1;
    int ok=1;
    while(1)
    {
        nowh1=(nowh1*x1+y1)%m;
        //printf("%I64d ",nowh1);
        nowt1++;
        if(ok1[nowh1])
        {
            ok=0;
            break;
        }
        if(nowh1==a1)break;
        ok1[nowh1]=1;
    }
    //printf("\n");
    while(1)
    {
        nowh2=(nowh2*x2+y2)%m;
        //printf("%I64d ",nowh2);
        nowt2++;
        if(ok2[nowh2])
        {
            ok=0;
            break;
        }
        if(nowh2==a2)break;
        ok2[nowh2]=1;
    }
    //printf("\n");
    if(ok==0)printf("-1\n");
    else
    {
        long long div=gcd(nowt1,nowt2);
        printf("%I64d\n",nowt1*nowt2/div);
    }
    return 0;
}


#include<stdio.h>
int ok1[1000001],ok2[1000001];
long long gcd(long long x,long long y)
{
    return y==0?x:gcd(y,x%y);
}
int main()
{
    long long m,h1,a1,x1,y1,h2,a2,x2,y2;
    scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&m,&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
    long long nowh1=h1,nowt1=0,nowh2=h2,nowt2=0;
    long long t1dif,t2dif;
    ok1[h1]=1;
    while(1)
    {
        nowh1=(nowh1*x1+y1)%m;
        //printf("%I64d ",nowh1);
        nowt1++;
        if(ok1[nowh1])
        {
            t1dif=nowt1-ok1[nowh1];
            break;
        }
        ok1[nowh1]=nowt1;
    }
    //printf("\n");
    ok2[h2]=1;
    while(1)
    {
        nowh2=(nowh2*x2+y2)%m;
        //printf("%I64d ",nowh2);
        nowt2++;
        if(ok2[nowh2])
        {
            t2dif=nowt2-ok2[nowh2];
            break;
        }
        ok2[nowh2]=nowt2;
    }
    //printf("\n");
    if(ok1[a1]==0||ok2[a2]==0)printf("-1\n");
    else
    {
        long long t1=ok1[a1],t2=ok2[a2];
        while(t1!=t2)
        {
            while(t1<t2)t1=t1+t1dif;
            while(t2<t1)t2=t2+t2dif;
        }
        printf("%I64d\n",t1);
    }
    return 0;
}


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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    while(h1 != a1 || h2 != a2)
    {
        ret++;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if(ret == 10000000) break;
    }
    if(ret == 10000000) cout << -1;
    else cout << ret;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 1000000
ll vit1[maxn], vit2[maxn];

ll gcd(ll a, ll b)
{
    ll pr = a, r = b;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
    }
    return pr;
}
ll lcm(ll a, ll b)
{
    return a/gcd(a, b)*b;
}
pair<ll, ll> euclid(ll a, ll b)
{
    ll pr = a, r = b;
    ll ps = 1, s = 0;
    ll pt = 0, t = 1;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
        h = s;
        s = ps - q*s;
        ps = h;
        h = t;
        t = pt - q*t;
        pt = h;
    }
    return make_pair(ps, pt);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, k1 = -1, k2 = -1, t1 = -1, t2 = -1, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    memset(vit1, 0, sizeof(vit1));
    memset(vit2, 0, sizeof(vit2));
    vit1[h1] = vit2[h2] = 1;
    while(h1 != a1 || h2 != a2)
    {
        ret++;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if((vit1[h1] > 0 && vit1[a1] == 0)|| (vit2[h2] > 0 && vit2[a2] == 0))
        {
            ret = -1;
            break;
        }
        if(h1 == a1)
        {
            if(k1 == -1) k1 = ret;
            else
                if(t1 == -1) t1 = ret;
        }
        if(h2 == a2)
        {
            if(k2 == -1) k2 = ret;
            else
                if(t2 == -1) t2 = ret;
        }
        vit1[h1]++;
        vit2[h2]++;
        if(vit1[a1] >= 2 && vit2[a2] >= 2)
        {
            ll a, b, c, d;
            a = t1 - k1;
            b = k1;
            c = t2 - k2;
            d = k2;
            if(b == d)
            {
                ret = b;
                break;
            }
            ll g = gcd(a, -c);
            if((d - b) % g != 0)
            {
                ret = -1;
                break;
            }
            else
            {
                a /= g;
                c /= g;
                b = (d - b)/g;
                pair<ll, ll> p = euclid(a, -c);
                ll x0 = b*p.first, y0 = b*p.second;
                ll a0 = t1 - k1, b0 = k1, c0 = t2 - k2, d0 = k2;
                double up = (d0 - c0*y0)/(1.0*c0*a), dw = (b0 - a0*x0)/(1.0*a0*c);
                if(c0*a > 0 && a0*c > 0)
                {
                    cout << "a";
                    ret = c0*(y0 + a*(max((ll)ceil(up), (ll)ceil(dw)))) + d0;
                }
                if(c0*a > 0 && a0*c < 0)
                {
                    ll ddw = ceil(up), uup = up;
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*ddw) + d0;
                }
                if(c0*a < 0 && a0*c > 0)
                {
                    ll ddw = ceil(dw), uup = floor(up);
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*uup) + d0;
                }
                if(c0*a < 0 && a0*c < 0)
                {
                    ret = c0*(y0 + a*min((ll)floor(up), (ll)floor(dw))) + d0;
                }
            }
            break;
        }
    }
    cout << ret;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 1000000
ll vit1[maxn], vit2[maxn];

ll gcd(ll a, ll b)
{
    ll pr = a, r = b;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
    }
    return pr;
}
ll lcm(ll a, ll b)
{
    return a/gcd(a, b)*b;
}
pair<ll, ll> euclid(ll a, ll b)
{
    ll pr = a, r = b;
    ll ps = 1, s = 0;
    ll pt = 0, t = 1;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
        h = s;
        s = ps - q*s;
        ps = h;
        h = t;
        t = pt - q*t;
        pt = h;
    }
    return make_pair(ps, pt);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, k1 = -1, k2 = -1, t1 = -1, t2 = -1, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    memset(vit1, 0, sizeof(vit1));
    memset(vit2, 0, sizeof(vit2));
    vit1[h1] = vit2[h2] = 1;
    while(h1 != a1 || h2 != a2)
    {
        ret++;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if((vit1[h1] > 0 && vit1[a1] == 0)|| (vit2[h2] > 0 && vit2[a2] == 0))
        {
            ret = -1;
            break;
        }
        if(h1 == a1)
        {
            if(k1 == -1) k1 = ret;
            else
                if(t1 == -1) t1 = ret;
        }
        if(h2 == a2)
        {
            if(k2 == -1) k2 = ret;
            else
                if(t2 == -1) t2 = ret;
        }
        vit1[h1]++;
        vit2[h2]++;
        if(vit1[a1] >= 2 && vit2[a2] >= 2)
        {
            ll a, b, c, d;
            a = t1 - k1;
            b = k1;
            c = t2 - k2;
            d = k2;
            if(b == d)
            {
                ret = b;
                break;
            }
            ll g = gcd(a, c);
            if((d - b) % g != 0)
            {
                ret = -1;
                break;
            }
            else
            {
                a /= g;
                c /= g;
                b = (d - b)/g;
                if(b < 0)
                {
                    b = -b;
                    a = -a;
                    c = -c;
                }
                pair<ll, ll> p = euclid(a, -c);
                ll x0 = b*p.first, y0 = b*p.second;
                ll a0 = t1 - k1, b0 = k1, c0 = t2 - k2, d0 = k2;
                double up = (d0 - c0*y0)/(1.0*c0*a), dw = (b0 - a0*x0)/(1.0*a0*c);
                if(c0*a > 0 && a0*c > 0)
                {
                    ret = c0*(y0 + a*(max((ll)ceil(up), (ll)ceil(dw)))) + d0;
                }
                if(c0*a > 0 && a0*c < 0)
                {
                    ll ddw = ceil(up), uup = up;
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*ddw) + d0;
                }
                if(c0*a < 0 && a0*c > 0)
                {
                    ll ddw = ceil(dw), uup = up;
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*up) + d0;
                }
                if(c0*a < 0 && a0*c < 0)
                {
                    ret = c0*(y0 + a*min((ll)(up), (ll)(dw))) + d0;
                }
            }
            break;
        }
    }
    cout << ret;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 1000000
ll vit1[maxn], vit2[maxn];

ll gcd(ll a, ll b)
{
    ll pr = a, r = b;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
    }
    return pr;
}
ll lcm(ll a, ll b)
{
    return a/gcd(a, b)*b;
}
pair<ll, ll> euclid(ll a, ll b)
{
    ll pr = a, r = b;
    ll ps = 1, s = 0;
    ll pt = 0, t = 1;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
        h = s;
        s = ps - q*s;
        ps = h;
        h = t;
        t = pt - q*t;
        pt = h;
    }
    return make_pair(ps, pt);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, k1 = -1, k2 = -1, t1 = -1, t2 = -1, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    memset(vit1, 0, sizeof(vit1));
    memset(vit2, 0, sizeof(vit2));
    vit1[h1] = vit2[h2] = 1;
    while(h1 != a1 || h2 != a2)
    {
        ret++;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if((vit1[h1] > 0 && vit1[a1] == 0)|| (vit2[h2] > 0 && vit2[a2] == 0))
        {
            ret = -1;
            break;
        }
        if(h1 == a1)
        {
            if(k1 == -1) k1 = ret;
            else
                if(t1 == -1) t1 = ret;
        }
        if(h2 == a2)
        {
            if(k2 == -1) k2 = ret;
            else
                if(t2 == -1) t2 = ret;
        }
        vit1[h1]++;
        vit2[h2]++;
        if(vit1[a1] >= 2 && vit2[a2] >= 2)
        {
            ll a, b, c, d;
            a = t1 - k1;
            b = k1;
            c = t2 - k2;
            d = k2;
            if(b == d)
            {
                ret = b;
                break;
            }
            ll g = gcd(a, c);
            if((d - b) % g != 0)
            {
                ret = -1;
                break;
            }
            else
            {
                a /= g;
                c /= g;
                b = (d - b)/g;
                pair<ll, ll> p = euclid(a, -c);
                ll x0 = b*p.first, y0 = b*p.second;
                ll a0 = t1 - k1, b0 = k1, c0 = t2 - k2, d0 = k2;
                double up = (-d0 - c0*y0)/(1.0*c0*a), dw = (-b0 - a0*x0)/(1.0*a0*c);
                if(c0*a > 0 && a0*c > 0)
                {
//                  cout << "a";
                    ret = c0*(y0 + a*(max((ll)ceil(up), (ll)ceil(dw)))) + d0;
                }
                if(c0*a > 0 && a0*c < 0)
                {
//                  cout << "aa";
                    ll ddw = ceil(up), uup = up;
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*ddw) + d0;
                }
                if(c0*a < 0 && a0*c > 0)
                {
//                  cout << "aaa";
                    ll ddw = ceil(dw), uup = floor(up);
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*uup) + d0;
                }
                if(c0*a < 0 && a0*c < 0)
                {
//                  cout << k1 << " " << t1 << " " << k2 << " " << t2 << endl;
//                  cout << a << " " << c << " " << b << " " << x0 << " " << y0 << " " << c0*a << " " << a0*c << " " << -d0 - c0*y0 << " " << -b0 - a0*x0 << endl;
//                  cout << "aaaa" << endl << up << " " <<  dw << endl;
//                  cout <<a0 << " " << b0 << " " << c0 << " " << d0 << endl;
                    ret = c0*(y0 + a*min((ll)floor(up), (ll)floor(dw))) + d0;
                }
            }
            break;
        }
    }
    cout << ret;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 1000000
ll vit1[maxn], vit2[maxn];

ll gcd(ll a, ll b)
{
    ll pr = a, r = b;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
    }
    return pr;
}
ll lcm(ll a, ll b)
{
    return a/gcd(a, b)*b;
}
pair<ll, ll> euclid(ll a, ll b)
{
    ll pr = a, r = b;
    ll ps = 1, s = 0;
    ll pt = 0, t = 1;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
        h = s;
        s = ps - q*s;
        ps = h;
        h = t;
        t = pt - q*t;
        pt = h;
    }
    return make_pair(ps, pt);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, k1 = -1, k2 = -1, t1 = -1, t2 = -1, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    memset(vit1, 0, sizeof(vit1));
    memset(vit2, 0, sizeof(vit2));
    vit1[h1] = vit2[h2] = 1;
    while(h1 != a1 || h2 != a2)
    {
        ret++;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if((vit1[h1] > 0 && vit1[a1] == 0)|| (vit2[h2] > 0 && vit2[a2] == 0))
        {
            ret = -1;
            break;
        }
        if(h1 == a1)
        {
            if(k1 == -1) k1 = ret;
            else
                if(t1 == -1) t1 = ret;
        }
        if(h2 == a2)
        {
            if(k2 == -1) k2 = ret;
            else
                if(t2 == -1) t2 = ret;
        }
        vit1[h1]++;
        vit2[h2]++;
        if(vit1[a1] >= 2 && vit2[a2] >= 2)
        {
            ll a, b, c, d;
            a = t1 - k1;
            b = k1;
            c = t2 - k2;
            d = k2;
            if(b == d)
            {
                ret = b;
                break;
            }
            ll g = gcd(a, -c);
            if((d - b) % g != 0)
            {
                ret = -1;
                break;
            }
            else
            {
                a /= g;
                c /= g;
                b = (d - b)/g;
                pair<ll, ll> p = euclid(a, -c);
                ll x0 = b*p.first, y0 = b*p.second;
                ll a0 = t1 - k1, b0 = k1, c0 = t2 - k2, d0 = k2;
                double up = -x0*1.0/c, dw = -y0*1.0/a;
                if(c > 0 && a > 0)
                {
                    ret = c0*(y0 + a*(max((ll)ceil(up), (ll)ceil(dw)))) + d0;
                }
                if(c > 0 && a < 0)
                {
                    ll ddw = ceil(up), uup = floor(up);
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*ddw) + d0;
                }
                if(c < 0 && a > 0)
                {
                    ll ddw = ceil(dw), uup = floor(up);
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*uup) + d0;
                }
                if(c < 0 && a < 0)
                {
                    ret = c0*(y0 + a*min((ll)floor(up), (ll)floor(dw))) + d0;
                }
            }
            break;
        }
    }
    cout << ret;
    return 0;
}


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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    while(h1 != a1 || h2 != a2)
    {
        ret++;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if(ret == 100000000) break;
    }
    if(ret == 100000000) cout << -1;
    else cout << ret;
    return 0;
}


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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    while(h1 != a1 || h2 != a2)
    {
        ret++;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if(ret == 19000000) break;
    }
    if(ret == 19000000) cout << -1;
    else cout << ret;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 1000000
ll vit1[maxn], vit2[maxn];

ll gcd(ll a, ll b)
{
    ll pr = a, r = b;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
    }
    return pr;
}
ll lcm(ll a, ll b)
{
    return a/gcd(a, b)*b;
}
pair<ll, ll> euclid(ll a, ll b)
{
    ll pr = a, r = b;
    ll ps = 1, s = 0;
    ll pt = 0, t = 1;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
        h = s;
        s = ps - q*s;
        ps = h;
        h = t;
        t = pt - q*t;
        pt = h;
    }
    return make_pair(ps, pt);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, k1 = -1, k2 = -1, t1 = -1, t2 = -1, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    memset(vit1, 0, sizeof(vit1));
    memset(vit2, 0, sizeof(vit2));
    vit1[h1] = vit2[h2] = 1;
    while(h1 != a1 || h2 != a2)
    {
        ret++;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if((vit1[h1] > 0 && vit1[a1] == 0)|| (vit2[h2] > 0 && vit2[a2] == 0))
        {
            ret = -1;
            break;
        }
        if(h1 == a1)
        {
            if(k1 == -1) k1 = ret;
            else
                if(t1 == -1) t1 = ret;
        }
        if(h2 == a2)
        {
            if(k2 == -1) k2 = ret;
            else
                if(t2 == -1) t2 = ret;
        }
        vit1[h1]++;
        vit2[h2]++;
        if(vit1[a1] >= 2 && vit2[a2] >= 2)
        {
            ll a, b, c, d;
            a = t1 - k1;
            b = k1;
            c = t2 - k2;
            d = k2;
            if(b == d)
            {
                ret = b;
                break;
            }
            ll g = gcd(a, c);
            if((d - b) % g != 0)
            {
                ret = -1;
                break;
            }
            else
            {
                a /= g;
                c /= g;
                b = (d - b)/g;
                if(b < 0)
                {
                    b = -b;
                    a = -a;
                    c = -c;
                }
                pair<ll, ll> p = euclid(a, -c);
                ll x0 = b*p.first, y0 = b*p.second;
                ll a0 = t1 - k1, b0 = k1, c0 = t2 - k2, d0 = k2;
                double up = (d0 - c0*y0)/(1.0*c0*a), dw = (b0 - a0*x0)/(1.0*a0*c);
                if(c0*a > 0 && a0*c > 0)
                {
                    ret = c0*(y0 + a*(max((ll)ceil(up), (ll)ceil(dw)))) + d0;
                }
                if(c0*a > 0 && a0*c < 0)
                {
                    ll ddw = ceil(up), uup = up;
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*ddw) + d0;
                }
                if(c0*a < 0 && a0*c > 0)
                {
                    ll ddw = ceil(dw), uup = up;
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*up) + d0;
                }
                if(c0*a < 0 && a0*c < 0)
                {
                    ret = c0*(y0 + a*min((ll)(up), (ll)(dw))) + d0;
                }
            }
        }
    }
    cout << ret;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 1000000
ll vit1[maxn], vit2[maxn];

ll gcd(ll a, ll b)
{
    ll pr = a, r = b;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
    }
    return pr;
}
ll lcm(ll a, ll b)
{
    return a/gcd(a, b)*b;
}
pair<ll, ll> euclid(ll a, ll b)
{
    ll pr = a, r = b;
    ll ps = 1, s = 0;
    ll pt = 0, t = 1;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
        h = s;
        s = ps - q*s;
        ps = h;
        h = t;
        t = pt - q*t;
        pt = h;
    }
    return make_pair(ps, pt);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, k1 = -1, k2 = -1, t1 = -1, t2 = -1, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    memset(vit1, 0, sizeof(vit1));
    memset(vit2, 0, sizeof(vit2));
    vit1[h1] = vit2[h2] = 1;
    while(h1 != a1 || h2 != a2)
    {
        ret++;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if((vit1[h1] > 0 && vit1[a1] == 0)|| (vit2[h2] > 0 && vit2[a2] == 0))
        {
            ret = -1;
            break;
        }
        if(h1 == a1)
        {
            if(k1 == -1) k1 = ret;
            else
                if(t1 == -1) t1 = ret;
        }
        if(h2 == a2)
        {
            if(k2 == -1) k2 = ret;
            else
                if(t2 == -1) t2 = ret;
        }
        vit1[h1]++;
        vit2[h2]++;
        if(vit1[a1] >= 2 && vit2[a2] >= 2)
        {
            ll a, b, c, d;
            a = t1 - k1;
            b = k1;
            c = t2 - k2;
            d = k2;
            if(b == d)
            {
                ret = b;
                break;
            }
            ll g = gcd(a, -c);
            if((d - b) % g != 0)
            {
                ret = -1;
                break;
            }
            else
            {
                a /= g;
                c /= g;
                b = (d - b)/g;
                pair<ll, ll> p = euclid(a, -c);
                ll x0 = b*p.first, y0 = b*p.second;
                ll a0 = t1 - k1, b0 = k1, c0 = t2 - k2, d0 = k2;
                double up = (d0 - c0*y0)/(1.0*c0*a), dw = (b0 - a0*x0)/(1.0*a0*c);
                if(c0*a > 0 && a0*c > 0)
                {
                    ret = c0*(y0 + a*(max((ll)ceil(up), (ll)ceil(dw)))) + d0;
                }
                if(c0*a > 0 && a0*c < 0)
                {
                    ll ddw = ceil(up), uup = up;
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*ddw) + d0;
                }
                if(c0*a < 0 && a0*c > 0)
                {
                    ll ddw = ceil(dw), uup = up;
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*up) + d0;
                }
                if(c0*a < 0 && a0*c < 0)
                {
                    ret = c0*(y0 + a*min((ll)(up), (ll)(dw))) + d0;
                }
            }
            break;
        }
    }
    cout << ret;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 1000000
ll vit1[maxn], vit2[maxn];

ll gcd(ll a, ll b)
{
    ll pr = a, r = b;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
    }
    return pr;
}
ll lcm(ll a, ll b)
{
    return a/gcd(a, b)*b;
}
pair<ll, ll> euclid(ll a, ll b)
{
    ll pr = a, r = b;
    ll ps = 1, s = 0;
    ll pt = 0, t = 1;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
        h = s;
        s = ps - q*s;
        ps = h;
        h = t;
        t = pt - q*t;
        pt = h;
    }
    return make_pair(s, t);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, k1 = -1, k2 = -1, t1 = -1, t2 = -1, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    memset(vit1, 0, sizeof(vit1));
    memset(vit2, 0, sizeof(vit2));
    vit1[h1] = vit2[h2] = 1;
    while(h1 != a1 || h2 != a2)
    {
        ret++;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if((vit1[h1] > 0 && vit1[a1] == 0)|| (vit2[h2] > 0 && vit2[a2] == 0))
        {
            ret = -1;
            break;
        }
        if(h1 == a1)
        {
            if(k1 == -1) k1 = ret;
            else
                if(t1 == -1) t1 = ret;
        }
        if(h2 == a2)
        {
            if(k2 == -1) k2 = ret;
            else
                if(t2 == -1) t2 = ret;
        }
        vit1[h1]++;
        vit2[h2]++;
        if(vit1[a1] >= 2 && vit2[a2] >= 2)
        {
            ll a, b, c, d;
            a = t1 - k1;
            b = k1;
            c = t2 - k2;
            d = k2;
            if(b == d)
            {
                ret = b;
                break;
            }
            ll g = gcd(a, c);
            if((d - b) % g != 0)
            {
                ret = -1;
                break;
            }
            else
            {
                a /= g;
                c /= g;
                b = (d - b)/g;
                if(b < 0)
                {
                    b = -b;
                    a = -a;
                    c = -c;
                }
                pair<ll, ll> p = euclid(a, -c);
                ll x0 = b*p.first, y0 = b*p.second;
                ll a0 = t1 - k1, b0 = k1, c0 = t2 - k2, d0 = k2;
                double up = (d0 - c0*y0)/(1.0*c0*a), dw = (b0 - a0*x0)/(1.0*a0*c);
                if(c0*a > 0 && a0*c > 0)
                {
                    ret = c0*(y0 + a*(max((ll)ceil(up), (ll)ceil(dw)))) + d0;
                }
                if(c0*a > 0 && a0*c < 0)
                {
                    ll ddw = ceil(up), uup = up;
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*ddw) + d0;
                }
                if(c0*a < 0 && a0*c > 0)
                {
                    ll ddw = ceil(dw), uup = up;
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*up) + d0;
                }
                if(c0*a < 0 && a0*c < 0)
                {
                    ret = c0*(y0 + a*min((ll)(up), (ll)(dw))) + d0;
                }
            }
        }
    }
    cout << ret;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 1000000
bool vit1[maxn], vit2[maxn];

ll gcd(ll a, ll b)
{
    ll pr = a, r = b;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
    }
    return pr;
}
ll lcm(ll a, ll b)
{
    return a/gcd(a, b)*b;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, k1 = -1, k2 = -1, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    memset(vit1, false, sizeof(vit1));
    memset(vit1, false, sizeof(vit1));
    vit1[h1] = vit2[h2] = true;
    while(1)
    {
        ret++;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if((vit1[h1] && !vit1[a1])|| (vit2[h2] && !vit2[a2]))
        {
            ret = -1;
            break;
        }
        if(h1 == a1 && k1 == -1) k1 = ret;
        if(h2 == a2 && k2 == -1) k2 = ret;
        vit1[h1] = vit2[h2] = true;
        if(vit1[k1] && vit2[k2])
        {
            ret = lcm(k1, k2);
            break;
        }
    }
    cout << ret;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 1000000
ll vit1[maxn], vit2[maxn];

ll gcd(ll a, ll b)
{
    ll pr = a, r = b;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
    }
    return pr;
}
ll lcm(ll a, ll b)
{
    return a/gcd(a, b)*b;
}
pair<ll, ll> euclid(ll a, ll b)
{
    ll pr = a, r = b;
    ll ps = 1, s = 0;
    ll pt = 0, t = 1;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
        h = s;
        s = ps - q*s;
        ps = h;
        h = t;
        t = pt - q*t;
        pt = h;
    }
    return make_pair(ps, pt);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, k1 = -1, k2 = -1, t1 = -1, t2 = -1, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    memset(vit1, 0, sizeof(vit1));
    memset(vit2, 0, sizeof(vit2));
    vit1[h1] = vit2[h2] = 1;
    while(h1 != a1 || h2 != a2)
    {
        ret++;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if((vit1[h1] > 0 && vit1[a1] == 0)|| (vit2[h2] > 0 && vit2[a2] == 0))
        {
            ret = -1;
            break;
        }
        if(h1 == a1)
        {
            if(k1 == -1) k1 = ret;
            else
                if(t1 == -1) t1 = ret;
        }
        if(h2 == a2)
        {
            if(k2 == -1) k2 = ret;
            else
                if(t2 == -1) t2 = ret;
        }
        vit1[h1]++;
        vit2[h2]++;
        if(vit1[a1] >= 2 && vit2[a2] >= 2)
        {
            ll a, b, c, d;
            a = t1 - k1;
            b = k1;
            c = t2 - k2;
            d = k2;
            if(b == d)
            {
                ret = b;
                break;
            }
            ll g = gcd(a, -c);
            if((d - b) % g != 0)
            {
                ret = -1;
                break;
            }
            else
            {
                a /= g;
                c /= g;
                b = (d - b)/g;
                pair<ll, ll> p = euclid(a, -c);
                ll x0 = b*p.first, y0 = b*p.second;
                ll a0 = t1 - k1, b0 = k1, c0 = t2 - k2, d0 = k2;
                double up = (d0 - c0*y0)/(1.0*c0*a), dw = (b0 - a0*x0)/(1.0*a0*c);
                if(c0*a > 0 && a0*c > 0)
                {
                    cout << "a";
                    ret = c0*(y0 + a*(max((ll)ceil(up), (ll)ceil(dw)))) + d0;
                }
                if(c0*a > 0 && a0*c < 0)
                {
                    ll ddw = ceil(up), uup = up;
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*ddw) + d0;
                }
                if(c0*a < 0 && a0*c > 0)
                {
                    ll ddw = ceil(dw), uup = floor(up);
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*up) + d0;
                }
                if(c0*a < 0 && a0*c < 0)
                {
                    ret = c0*(y0 + a*min((ll)floor(up), (ll)floor(dw))) + d0;
                }
            }
            break;
        }
    }
    cout << ret;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 1000000
ll vit1[maxn], vit2[maxn];

ll gcd(ll a, ll b)
{
    ll pr = a, r = b;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
    }
    return pr;
}
ll lcm(ll a, ll b)
{
    return a/gcd(a, b)*b;
}
pair<ll, ll> euclid(ll a, ll b)
{
    ll pr = a, r = b;
    ll ps = 1, s = 0;
    ll pt = 0, t = 1;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
        h = s;
        s = ps - q*s;
        ps = h;
        h = t;
        t = pt - q*t;
        pt = h;
    }
    return make_pair(s, t);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, k1 = -1, k2 = -1, t1 = -1, t2 = -1, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    memset(vit1, 0, sizeof(vit1));
    memset(vit2, 0, sizeof(vit2));
    vit1[h1] = vit2[h2] = 1;
    while(h1 != a1 || h2 != a2)
    {
        ret++;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if((vit1[h1] > 0 && vit1[a1] == 0)|| (vit2[h2] > 0 && vit2[a2] == 0))
        {
            ret = -1;
            break;
        }
        if(h1 == a1)
        {
            if(k1 == -1) k1 = ret;
            else
                if(t1 == -1) t1 = ret;
        }
        if(h2 == a2)
        {
            if(k2 == -1) k2 = ret;
            else
                if(t2 == -1) t2 = ret;
        }
        vit1[h1] = vit2[h2] = true;
        if(vit1[a1] >= 2 && vit2[a2] >= 2)
        {
            ll a, b, c, d;
            a = t1 - k1;
            b = k1;
            c = t2 - k2;
            d = k2;
            if(b == d)
            {
                ret = b;
                break;
            }
            ll g = gcd(a, c);
            if((d - b) % g != 0)
            {
                ret = -1;
                break;
            }
            else
            {
                a /= g;
                c /= g;
                b = (d - b)/g;
                if(b < 0)
                {
                    b = -b;
                    a = -a;
                    c = -c;
                }
                pair<ll, ll> p = euclid(a, -c);
                ll x0 = b*p.first, y0 = b*p.second;
                ll a0 = t1 - k1, b0 = k1, c0 = t2 - k2, d0 = k2;
                double up = (d0 - c0*y0)/(1.0*c0*a), dw = (b0 - a0*x0)/(1.0*a0*c);
                if(c0*a > 0 && a0*c > 0)
                {
                    ret = c0*(y0 + a*(max((ll)ceil(up), (ll)ceil(dw)))) + d0;
                }
                if(c0*a > 0 && a0*c < 0)
                {
                    ll ddw = ceil(up), uup = up;
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*ddw) + d0;
                }
                if(c0*a < 0 && a0*c > 0)
                {
                    ll ddw = ceil(dw), uup = up;
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*up) + d0;
                }
                if(c0*a < 0 && a0*c < 0)
                {
                    ret = c0*(y0 + a*min((ll)(up), (ll)(dw))) + d0;
                }
            }
        }
    }
    cout << ret;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 1000000
bool vit1[maxn], vit2[maxn];

ll gcd(ll a, ll b)
{
    ll pr = a, r = b;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
    }
    return pr;
}
ll lcm(ll a, ll b)
{
    return a/gcd(a, b)*b;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, k1 = -1, k2 = -1, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    memset(vit1, false, sizeof(vit1));
    memset(vit1, false, sizeof(vit1));
    vit1[h1] = vit2[h2] = true;
    while(1)
    {
        ret++;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if((vit1[h1] && !vit1[a1])|| (vit2[h2] && !vit2[a2]))
        {
            ret = -1;
            break;
        }
        if(h1 == a1 && k1 == -1) k1 = ret;
        if(h2 == a2 && k2 == -1) k2 = ret;
        vit1[h1] = vit2[h2] = true;
        if(vit1[a1] && vit2[a2])
        {
            ret = lcm(k1, k2);
            break;
        }
    }
    cout << ret;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 1000000
ll vit1[maxn], vit2[maxn];

ll gcd(ll a, ll b)
{
    ll pr = a, r = b;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
    }
    return pr;
}
ll lcm(ll a, ll b)
{
    return a/gcd(a, b)*b;
}
pair<ll, ll> euclid(ll a, ll b)
{
    ll pr = a, r = b;
    ll ps = 1, s = 0;
    ll pt = 0, t = 1;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
        h = s;
        s = ps - q*s;
        ps = h;
        h = t;
        t = pt - q*t;
        pt = h;
    }
    return make_pair(ps, pt);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, ph1, ph2, k1 = -1, k2 = -1, t1 = -1, t2 = -1, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    memset(vit1, 0, sizeof(vit1));
    memset(vit2, 0, sizeof(vit2));
    vit1[h1] = vit2[h2] = 1;
    while(h1 != a1 || h2 != a2)
    {
        ret++;
        ph1 = h1;
        ph2 = h2;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if((vit1[h1] > 0 && vit1[a1] == 0)|| (vit2[h2] > 0 && vit2[a2] == 0))
        {
            ret = -1;
            break;
        }
        if(h1 == a1)
        {
            if(k1 == -1) k1 = ret;
            else
                if(t1 == -1) t1 = ret;
        }
        if(h2 == a2)
        {
            if(k2 == -1) k2 = ret;
            else
                if(t2 == -1) t2 = ret;
        }
        vit1[h1]++;
        vit2[h2]++;
        if(vit1[a1] >= 2 && vit2[a2] >= 2)
        {
            ll a, b, c, d;
            a = t1 - k1;
            b = k1;
            c = t2 - k2;
            d = k2;
            if(b == d)
            {
                ret = b;
                break;
            }
            ll g = gcd(a, -c);
            if((d - b) % g != 0)
            {
                ret = -1;
                break;
            }
            else
            {
                a /= g;
                c /= g;
                b = (d - b)/g;
                pair<ll, ll> p = euclid(a, -c);
                ll x0 = b*p.first, y0 = b*p.second;
                ll a0 = t1 - k1, b0 = k1, c0 = t2 - k2, d0 = k2;
                double up = -x0*1.0/c, dw = -y0*1.0/a;
                if(c > 0 && a > 0)
                {
                    ret = c0*(y0 + a*(max((ll)ceil(up), (ll)ceil(dw)))) + d0;
                }
                if(c > 0 && a < 0)
                {
                    ll ddw = ceil(up), uup = floor(up);
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*ddw) + d0;
                }
                if(c < 0 && a > 0)
                {
                    ll ddw = ceil(dw), uup = floor(up);
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*uup) + d0;
                }
                if(c < 0 && a < 0)
                {
                    ret = c0*(y0 + a*min((ll)floor(up), (ll)floor(dw))) + d0;
                }
            }
            break;
        }
        if((vit1[a1] < 2 && ph1 == h1) || (vit2[a2] < 2 && ph2 == h2))
        {
            ret = -1;
            break;
        }
    }
    cout << ret;
    return 0;
}