#include <stdio.h>

int bl[110];
int gl[110];

int main() {
    int n, m, b, g, i, temp;
    scanf("%d %d", &n, &m);
    scanf("%d", &b);
    for (i=0; i<b; i++) {
        scanf("%d", &temp);
        bl[temp] = 1;
    }
    scanf("%d", &g);
    for (i=0; i<g; i++) {
        scanf("%d", &temp);
        gl[temp] = 1;
    }
    i=0;
    while (b < n || g < m) {
        if (bl[i%n] || gl[i%m]) {
            if (bl[i%n] == 0) {
                bl[i%n] = 1; b++;
            }
            if (gl[i%m] == 0) {
                gl[i%m] = 1; g++;
            }
        }
        else if (i>10000) {
            printf("No\n");
            return 0;
        }

        i++;
    }
    printf("Yes\n");
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int gcd(int m,int n)
{
    if(m==n)
        return m;
    if(n==0)
        return m;
    else
        return gcd(n,m%n);
}
int main()
{
    int i,j,k,l,*a,*b,m,n,gc,*c,t;
    scanf("%d%d",&m,&n);
    a=(int *)malloc(sizeof(int)*m);
    b=(int *)malloc(sizeof(int)*n);
    if(m>n)
    {gc=gcd(m,n);
        for(i=0;i<m;i++)
        {
            a[i]=0;
            if(i<n)
            b[i]=0;
        }}

    else
    {gc=gcd(n,m);
        for(i=0;i<n;i++)
        {
            if(i<m)
            a[i]=0;
            //if(i<m)
            b[i]=0;
        }
    }
    c=(int *)malloc(sizeof(int)*gc);
    for(i=0;i<gc;i++)
        c[i]=0;
scanf("%d",&t);
for(i=0;i<t;i++)
{
    scanf("%d",&k);
    a[k]=1;
}
scanf("%d",&t);
for(i=0;i<t;i++)
{
    scanf("%d",&k);
    b[k]=1;
}
for(i=0;i<m;i++)
{
    if(a[i]==1)
        c[i%gc]=1;
}
for(i=0;i<n;i++)
{
    if(b[i]==1)
        c[i%gc]=1;
}
k=0;
for(i=0;i<gc;i++)
{
    if(c[i]==0)
    {
        k=1;
        break;
    }
}
if(k==0)
    printf("YES\n");
else
printf("NO\n");
return 0;
}


#include <stdio.h>

#define N	100

int main() {
	static int aa[N], bb[N];
	int n, m, b, g, x, y, h, i, j, yes;

	scanf("%d%d", &n, &m);
	scanf("%d", &b);
	for (i = 0; i < b; i++) {
		scanf("%d", &x);
		aa[x] = 1;
	}
	scanf("%d", &g);
	for (i = 0; i < g; i++) {
		scanf("%d", &y);
		bb[y] = 1;
	}
	for (h = 0; h < n + m; h++)
		for (i = 0; i < n * m; i++)
			aa[i % n] = bb[i % m] = aa[i % n] | bb[i % m];
	yes = 1;
	for (i = 0; i < n; i++)
		if (!aa[i]) {
			yes = 0;
			break;
		}
	for (j = 0; j < m; j++)
		if (!bb[j]) {
			yes = 0;
			break;
		}
	printf("%s\n", yes ? "Yes" : "No");
	return 0;
}


#include <stdio.h>
int main()
{
    int n,m,b,g,c,d=0,i,j;
    scanf("%d %d",&n,&m);
    scanf("%d",&b);
    int ara[]={};
    for(i=0;i<b;i++){
        scanf("%d",&ara[i]);
    }
    scanf("%d",&g);
    int str[]={};
    for(i=0;i<g;i++){
        scanf("%d",&str[i]);
    }
    if(n>m){
        if((n-m)%2==0){
            for(j=0;j<g;j++){
                for(i=0;i<b;i++){
                    if(ara[i]>str[j]) {
                            c=ara[i]-str[j];
                    }
                    else if(ara[i]<str[j]) {
                            c=str[j]-ara[i];
                    }
                    if(c%2==1){
                        printf("Yes");
                        j=g-1;
                        i=b-1;
                    }
                    else{
                        d++;
                    }
                }
            }
            if(d==g*b){
                printf("No");
            }
        }
        else if((n-m)%2==1){
            printf("Yes");
        }
        else{
            printf("No");
        }
    }
    else if(n<m){
        if((m-n)%2==0){
            for(j=0;j<g;j++){
                for(i=0;i<b;i++){
                    if(ara[i]>str[j]) {
                            c=ara[i]-str[j];
                    }
                    else if(ara[i]<str[j]) {
                            c=str[j]-ara[i];
                    }
                    if(c%2==1){
                        printf("Yes");
                        j=g-1;
                        i=b-1;
                    }
                    else{
                        d++;
                    }
                }
            }
            if(d==g*b){
                printf("No");
            }
        }
        else if((m-n)%2==1){
            printf("Yes");
        }
        else{
            printf("No");
        }
    }
    else{
        if(b+g==d){
            printf("Yes");
        }
        else{
            printf("No");
        }
    }
    return 0;
}


#include <stdio.h>
int main()
{
    int n,m,lcm=0,a,b,boys,girls,hb[100],hg[100],i,j,index,tg,tb,flag=0;
    scanf("%d",&n);
    scanf("%d",&m);
    a=n;
    b=m;
    scanf("%d",&boys);
    for(i=0;i<100;i++)
    {
        hb[i]=0;
        hg[i]=0;
    }
    for(i=0;i<boys;i++)
    {
        scanf("%d",&index);
        hb[index]=1;
    }
    scanf("%d",&girls);
    for(i=0;i<girls;i++)
    {
        scanf("%d",&index);
        hg[index]=1;
    }
    /*for(i=0;i<a;i++)
        printf("%d ",hb[i]);
    printf("\n");
    for(i=0;i<b;i++)
        printf("%d ",hg[i]);
    printf("\n");
    printf("\n");*/
    while(n!=m)
    {
        if(n<m)
            n=n+a;
        else
            m=m+b;
    }
    //printf("n:%d\n",n);
    j=1;
    i=0;
    while(j<=n)
    {
        tg=i%b;
        tb=i%a;
        if(hg[tg]==1)
            hb[tb]=1;
        else if(hb[tb]==1)
            hg[tg]=1;
        i++;
        j++;
    }
    /*for(i=0;i<a;i++)
        printf("%d ",hb[i]);
    printf("\n");
    for(i=0;i<b;i++)
        printf("%d ",hg[i]);
    printf("\n");*/
    for(i=0;i<a;i++)
    {
        if(hb[i]==0)
        {
            flag=1;
            break;
        }
    }
    if(flag==0)
        printf("YES\n");
    else
    {
        flag=0;
        for(i=0;i<b;i++)
        {
            if(hg[i]==0)
            {
                flag=1;
                break;
            }
        }
        if(flag==0)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


#include <stdio.h>
int main()
{
    int n,m,lcm=0,a,b,boys,girls,hb[100],hg[100],i,j,index,tg,tb,flag=0;
    scanf("%d",&n);
    scanf("%d",&m);
    a=n;
    b=m;
    scanf("%d",&boys);
    for(i=0;i<100;i++)
    {
        hb[i]=0;
        hg[i]=0;
    }
    for(i=0;i<boys;i++)
    {
        scanf("%d",&index);
        hb[index]=1;
    }
    scanf("%d",&girls);
    for(i=0;i<girls;i++)
    {
        scanf("%d",&index);
        hg[index]=1;
    }
    /*for(i=0;i<a;i++)
        printf("%d ",hb[i]);
    printf("\n");
    for(i=0;i<b;i++)
        printf("%d ",hg[i]);
    printf("\n");
    printf("\n");*/
    while(n!=m)
    {
        if(n<m)
            n=n+a;
        else
            m=m+b;
    }
    //printf("n:%d\n",n);
    j=1;
    i=0;
    while(j<=n)
    {
        tg=i%b;
        tb=i%a;
        if(hg[tg]==1)
            hb[tb]=1;
        else if(hb[tb]==1)
            hg[tg]=1;
        i++;
        j++;
    }
    /*for(i=0;i<a;i++)
        printf("%d ",hb[i]);
    printf("\n");
    for(i=0;i<b;i++)
        printf("%d ",hg[i]);
    printf("\n");*/
    for(i=0;i<a;i++)
    {
        if(hb[i]==0)
        {
            flag=1;
            break;
        }
    }
    for(i=0;i<b;i++)
    {
        if(hg[i]==0)
        {
            flag=1;
            break;
        }
    }
    if(flag==0)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

void sifirla(int *s, int size)
{
	int i;
	for(i=0;i<size;i++)
		s[i]=0;
}

int main()
{
	int m,n;
	scanf("%d %d",&m,&n);
	int *boys=(int*)malloc(m*sizeof(int));
	int *girls=(int*)malloc(n*sizeof(int));

	sifirla(boys, m);
	sifirla(girls, n);

	int hb,hg;
	scanf("%d",&hb);
	int i,j;
	for(i=0;i<hb;i++)
	{

		scanf("%d",&j);
		boys[j]=1;
	}
	scanf("%d",&hg);
	for(i=0;i<hg;i++)
	{
		scanf("%d",&j);
		girls[j]=1;
	}

	for(i=0;i<1000000;i++)
	{
		boys[i%m]=(boys[i%m]||girls[i%n]);
		girls[i%n]=(boys[i%m]||girls[i%n]);
	}

	int k=0;
	for(i=0;i<m;i++)
	{
		if(*(boys+i)==0)
			{
			k=1;
			break;
			}
	}
	if(k!=0)
		printf("No");
	else
		printf("Yes");

	return 0;

}


#include<stdio.h>
int i,x,y,a,n,m,erkek[5000],kiz[5000];
int knt(){
	int i;
	for(i=0;i<n;i++)
		if(!erkek[i])
			return 0;

	for(i=0;i<m;i++)
		if(!kiz[i])
			return 0;

	return 1;
}
int main(){
	scanf("%d %d",&n,&m);
	scanf("%d",&x);
	for(i=1;i<=x;i++){
		scanf("%d",&a);
		erkek[a]=1;
	}
	scanf("%d",&y);
	for(i=1;i<=y;i++){
		scanf("%d",&a);
		kiz[a]=1;
	}
	for(i=0;i<=2*m*n;i++){
		if(erkek[i%n] || kiz[i%m])
			erkek[i%n] = kiz[i%m] = 1;

		if(knt()){
			puts("Yes");
			return 0;
		}
	}
	puts("No");
return 0;
}


#include<stdio.h>
int i,x,y,a,n,m,erkek[5000],kiz[5000];
int knt(){
	int i;
	for(i=0;i<n;i++)
		if(!erkek[i])
			return 0;

	for(i=0;i<m;i++)
		if(!kiz[i])
			return 0;

	return 1;
}
int main(){
	scanf("%d %d",&n,&m);
	scanf("%d",&x);
	for(i=1;i<=x;i++){
		scanf("%d",&a);
		erkek[a]=1;
	}
	scanf("%d",&y);
	for(i=1;i<=y;i++){
		scanf("%d",&a);
		kiz[a]=1;
	}
	for(i=0;i<=m*n;i++){
		if(erkek[i%n] || kiz[i%m])
			erkek[i%n] = kiz[i%m] = 1;

		if(knt()){
			puts("Yes");
			return 0;
		}
	}
	puts("No");
return 0;
}


#include<stdio.h>
#include<stdlib.h>

int arr[101],brr[101];
int main()
{
    int i,j,k,m,n,a,b,ind1,ind2,count;
    scanf("%d %d",&n,&m);

    scanf("%d",&a);

    while(a)
    {
              scanf("%d",&i);
              arr[i]=1;
              a--;
    }
    scanf("%d",&b);
    while(b)
    {
              scanf("%d",&i);

              brr[i]=1;
              b--;
    }

    for(i=0;i<=100005;i++)
    {
                     ind1=i%n;
                     ind2=i%m;
                     k=arr[ind1]|brr[ind2];
                     arr[ind1]=k;
                     brr[ind2]=k;

    }

    count=0;

    for(i=0;i<n;i++)
    {
                    if(arr[i]==0)
                    {
                    count=1;
                    break;
                    }
    }
    for(i=0;i<m;i++)
    {
                    if(brr[i]==0)
                    {
                                 count=1;
                                 break;
                    }
    }

    if(count==0)
    printf("Yes");
    else
    printf("No");

    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int arr[101],brr[101];
int main()
{
    int i,j,k,m,n,a,b,ind1,ind2,count;
    scanf("%d %d",&n,&m);

    scanf("%d",&a);

    while(a)
    {
              scanf("%d",&i);
              arr[i]=1;
              a--;
    }
    scanf("%d",&b);
    while(b)
    {
              scanf("%d",&i);

              brr[i]=1;
              b--;
    }

    for(i=0;i<=105;i++)
    {
                     ind1=i%n;
                     ind2=i%m;
                     k=arr[ind1]|brr[ind2];
                     arr[ind1]=k;
                     brr[ind2]=k;

    }

    count=0;

    for(i=0;i<n;i++)
    {
                    if(arr[i]==0)
                    {
                    count=1;
                    break;
                    }
    }
    for(i=0;i<m;i++)
    {
                    if(brr[i]==0)
                    {
                                 count=1;
                                 break;
                    }
    }

    if(count==0)
    printf("Yes");
    else
    printf("No");

    return 0;
}


#include<stdio.h>
#define Max(a,b) (a>b?a:b)
int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m)==2)
    {
        int i,j,o,p,count=0,index,boy[100]={0},girl[100]={0};
        scanf("%d",&o);
        for(i=0;i<o;i++)scanf("%d",&index),boy[index]=1;
        scanf("%d",&p);
        for(i=0;i<p;i++)scanf("%d",&index),girl[index]=1;
        for(i=0;i<(m*n)+100;i++)
        {
            if(boy[i%n] || girl[i%m])boy[i%n]=1,girl[i%m]=1;
        }
        for(i=0;i<(Max(n,m));i++)
        {
            if(i<n && boy[i])count++;
            if(i<m && girl[i])count++;
        }
        if(count==m+n)printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}


#include<stdio.h>
#define Max(a,b) (a>b?a:b)
int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m)==2)
    {
        int i,j,o,p,count=0,index,boy[100]={0},girl[100]={0};
        scanf("%d",&o);
        for(i=0;i<o;i++)scanf("%d",&index),boy[index]=1;
        scanf("%d",&p);
        for(i=0;i<p;i++)scanf("%d",&index),girl[index]=1;
        for(i=0;i<(m*n);i++)
        {
            if(boy[i%n] || girl[i%m])boy[i%n]=1,girl[i%m]=1;
        }
        for(i=0;i<(Max(n,m));i++)
        {
            if(i<n && boy[i])count++;
            if(i<m && girl[i])count++;
        }
        if(count==m+n)printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}


#include<stdio.h>
int B[105];
int G[105];
int main()
{
    int nb,ng,nhb,nhg,i,tmp,flag=0,b,g,a;
    scanf("%d %d",&nb,&ng);
    for(i=0;i<nb;i++)
    {
	B[i]=0;
    }
    for(i=0;i<ng;i++)
    {
	G[i]=0;
    }
    scanf("%d",&nhb);
    while(nhb--)
    {
	scanf("%d",&tmp);
	B[tmp]=1;
    }
    scanf("%d",&nhg);
    while(nhg--)
    {
	scanf("%d",&tmp);
	G[tmp]=1;
    }
    i=0;
    a=nb*ng;
    a=a*a;
    while(a--)
    {
	b=i%nb;
	g=i%ng;
	if(B[b]==1&&G[g]==0)
	{
	    G[g]=1;
	}
	else if(B[b]==0&&G[g]==1)
	{
	    B[b]=1;
	}
	i++;
    }
    for(i=0;i<nb;i++)
    {
	if(B[i]==1)
	{
	    flag=1;
	}
	else
	{
	    flag=0;
	    break;
	}
    }
    if(flag==1)
    {
	for(i=0;i<ng;i++)
	{
	    if(G[i]==1)
	    {
		flag=1;
	    }
	    else
	    {
		flag=0;
		break;
	    }
	}
    }
    if(flag==1)
    {
	printf("YES\n");
    }
    else
    {
	printf("NO\n");
    }
    return  0;
}


#include<stdio.h>
int B[105];
int G[105];
int main()
{
    int nb,ng,nhb,nhg,i,tmp,flag=0,b,g,a;
    scanf("%d %d",&nb,&ng);
    for(i=0;i<nb;i++)
    {
	B[i]=0;
    }
    for(i=0;i<ng;i++)
    {
	G[i]=0;
    }
    scanf("%d",&nhb);
    while(nhb--)
    {
	scanf("%d",&tmp);
	B[tmp]=1;
    }
    scanf("%d",&nhg);
    while(nhg--)
    {
	scanf("%d",&tmp);
	G[tmp]=1;
    }
    i=0;
    a=nb*ng;
    while(a--)
    {
	b=i%nb;
	g=i%ng;
	if(B[b]==1&&G[g]==0)
	{
	    G[g]=1;
	}
	else if(B[b]==0&&G[g]==1)
	{
	    B[b]=1;
	}
	i++;
    }
    for(i=0;i<nb;i++)
    {
	if(B[i]==1)
	{
	    flag=1;
	}
	else
	{
	    flag=0;
	    break;
	}
    }
    if(flag==1)
    {
	for(i=0;i<ng;i++)
	{
	    if(G[i]==1)
	    {
		flag=1;
	    }
	    else
	    {
		flag=0;
		break;
	    }
	}
    }
    if(flag==1)
    {
	printf("YES\n");
    }
    else
    {
	printf("NO\n");
    }
    return  0;
}


#include<stdio.h>
#include<stdlib.h>
int losagu(int a, int b)
{

    int i = 1,j;
    for(i=1;;i++)
    {
        j = a*i;

        if(j%b == 0){break;}
    }
    return j;
}
int main()
{
    int n,m,b,g,i,j=0,k;

    scanf("%d  %d",&n,&m);
    int boy[n], girl[m];

    scanf("%d",&b);
    //new
    for(i=0;i<n;i++)
    {

        if(j<b)
        {
            scanf("%d",&k);
            boy[k] = 1;
            j++;
        }
        if(boy[i] != 1){boy[i] = 0;}

    }
    j=0;
    scanf("%d",&g);
    for(i=0;i<m;i++)
    {

        if(j<g)
        {
            scanf("%d",&k);
            girl[k] = 1;
            j++;
        }
        if(girl[i] != 1){girl[i] = 0;}
    }

    int l = losagu(n,m);
    k =0;
    for(i=0;i<2*l;i++)
    {
        int x,y;
        x = i%n;
        y = i%m;

        if(boy[x]==1 && girl[y]==0){girl[y] = 1;}
        else if(boy[x]==0 && girl[y]==1){boy[x] = 1;}
    }
    for(i=0;i<n;i++)
    {
        if(boy[i] == 0){printf("No");k = 0;break;}
        else if(boy[i] ==1){k = 1;}
    }
    if(k ==1)
    {
        for(i=0;i<m;i++)
        {
            if(girl[i] == 0){printf("No");k = 0;break;}
        }
    }
    if(k == 1){printf("Yes");}

    return 0;
}


#include<stdio.h>
#include<stdlib.h>
int losagu(int a, int b)
{

    int i = 1,j;
    for(i=1;;i++)
    {
        j = a*i;

        if(j%b == 0){break;}
    }
    return j;
}
int main()
{
    int n,m,b,g,i,j=0,k;

    scanf("%d  %d",&n,&m);
    int boy[n], girl[m];

    scanf("%d",&b);
    //new
    for(i=0;i<n;i++)
    {

        if(j<b)
        {
            scanf("%d",&k);
            boy[k] = 1;
            j++;
        }
        if(boy[i] != 1){boy[i] = 0;}

    }
    j=0;
    scanf("%d",&g);
    for(i=0;i<m;i++)
    {

        if(j<g)
        {
            scanf("%d",&k);
            girl[k] = 1;
            j++;
        }
        if(girl[i] != 1){girl[i] = 0;}
    }

    int l = losagu(n,m);
    k =0;
    for(i=0;i<l;i++)
    {
        int x,y;
        x = i%n;
        y = i%m;

        if(boy[x]==1 && girl[y]==0){girl[y] = 1;}
        else if(boy[x]==0 && girl[y]==1){boy[x] = 1;}
    }
    for(i=0;i<n;i++)
    {
        if(boy[i] == 0){printf("No");k = 0;break;}
        else if(boy[i] ==1){k = 1;}
    }
    if(k ==1)
    {
        for(i=0;i<m;i++)
        {
            if(girl[i] == 0){printf("No");k = 0;break;}
        }
    }
    if(k == 1){printf("Yes");}

    return 0;
}


#include<stdio.h>
#include<stdlib.h>
int losagu(int a, int b)
{

    int i = 1,j;
    for(i=1;;i++)
    {
        j = a*i;

        if(j%b == 0){break;}
    }
    return j;
}
int main()
{
    int n,m,b,g,i,j=0,k;

    scanf("%d  %d",&n,&m);
    int boy[n], girl[m];

    scanf("%d",&b);
    //new
    for(i=0;i<n;i++)
    {

        if(j<b)
        {
            scanf("%d",&k);
            boy[k] = 1;
            j++;
        }
        if(boy[i] != 1){boy[i] = 0;}

    }
    j=0;
    scanf("%d",&g);
    for(i=0;i<m;i++)
    {

        if(j<g)
        {
            scanf("%d",&k);
            girl[k] = 1;
            j++;
        }
        if(girl[i] != 1){girl[i] = 0;}
    }

    int l = losagu(n,m);
    k =0;
    for(i=0;i<l;i++)
    {
        int x,y;
        x = i%n;
        y = i%m;

        if(boy[x]==1 && girl[y]==0){girl[y] = 1;}
        else if(boy[x]==0 && girl[y]==1){boy[x] = 1;}
    }
    for(i=0;i<n;i++)
    {
        if(boy[i] == 0){printf("No");k = 0;break;}
        else{k = 1;}
    }
    if(k ==1)
    {
        for(i=0;i<m;i++)
        {
            if(girl[i] == 0){printf("No");k = 0;break;}
        }
    }
    if(k == 1){printf("Yes");}

    return 0;
}


#include<stdio.h>
#include<stdlib.h>
int losagu(int a, int b)
{

    int i = 1,j;
    for(i=1;;i++)
    {
        j = a*i;

        if(j%b == 0){break;}
    }
    return j;
}
int main()
{
    int n,m,b,g,i,j=0,k;

    scanf("%d  %d",&n,&m);
    int boy[n], girl[m];

    scanf("%d",&b);
    //new
    for(i=0;i<n;i++)
    {

        boy[i] = 0;
    }
    for(i=0;i<b;i++)
    {
        scanf("%d",&k);
        boy[k] = 1;
    }

    j=0;
    scanf("%d",&g);
    for(i=0;i<m;i++)
    {
        girl[i] = 0;

    }
    for(i=0;i<g;i++)
    {
        scanf("%d",&k);
        girl[k] = 1;

    }

    int l = losagu(n,m);
    k =0;
    for(i=0;i<2*l;i++)
    {
        int x,y;
        x = i%n;
        y = i%m;

        if(boy[x]==1 && girl[y]==0){girl[y] = 1;}
        else if(boy[x]==0 && girl[y]==1){boy[x] = 1;}
    }
    for(i=0;i<n;i++)
    {
        if(boy[i] == 0){printf("No");k = 0;break;}
        else if(boy[i] ==1){k = 1;}
    }
    if(k ==1)
    {
        for(i=0;i<m;i++)
        {
            if(girl[i] == 0){printf("No");k = 0;break;}
        }
    }
    if(k == 1){printf("Yes");}

    return 0;
}


#include<stdio.h>
int main()
{
int count=0,b,g,l,m,n,k,x,i,j,y,z,flag1[100],flag2[100];
scanf("%d%d",&n,&m);
scanf("%d",&b);
for(l=0;l<n;l++)
flag1[l]=0;
for(k=0;k<m;k++)
flag2[k]=0;
for(i=1;i<=b;i++)
{
scanf("%d",&x);
flag1[x]=1;
count++;
}
scanf("%d",&g);
for(j=1;j<=g;j++)
{
scanf("%d",&x);
flag2[x]=1;
count++;
}
i=0;
while(1)
{
y=i%n;
z=i%m;
if(flag1[y]==1)
{
if(flag2[z]!=1)
{
flag2[z]=1;
count++;
}
}
if(flag2[z]==1)
{
if(flag1[y]!=1)
{
flag1[y]=1;
count++;
}
}
if(count==n+m)
{
printf("Yes\n");
break;
}
else
{
if(i>n*m*n*m)
{
printf("No");
break;
}
else
i++;
continue;
}

}
return 0;
}


#include<stdio.h>
int main()
{
        int n,m,i;
        scanf("%d %d",&n,&m);
        int a[n];
        int b[m];
        for(i=0;i<n;i++)
                a[i]=0;
        for(i=0;i<m;i++)
                b[i]=0;
        int temp,num;
        scanf("%d",&num);
        while(num--){
                scanf("%d",&temp);
                a[temp]=1;
        }
        scanf("%d",&num);
        while(num--)
        {
                scanf("%d",&temp);
                b[temp]=1;
        }
        for(i=0;i<n*m;i++)
        {
                num=i%n;
                temp=i%m;
                if(a[num]==1 || b[temp]==1)
                {
                        a[num]=1;
                        b[temp]=1;
                }

        }
        for(i=0;i<n;i++)
        {
                if(a[i]==0){
                        num=-99;

                        break;
                }
        }
        for(i=0;i<m;i++)
        {
                if(b[i]==0){
                        num=-99;

                        break;
                }
        }
        if(num==-99)
                printf("No");
        else
                printf("Yes");
        return 0;
}


#include<stdio.h>
int main()
{
        int n,m,i;
        scanf("%d %d",&n,&m);
        int a[n];
        int b[m];
        for(i=0;i<n;i++)
                a[i]=0;
        for(i=0;i<m;i++)
                b[i]=0;
        int temp,num;
        scanf("%d",&num);
        while(num--){
                scanf("%d",&temp);
                a[temp]=1;
        }
        scanf("%d",&num);
        while(num--)
        {
                scanf("%d",&temp);
                b[temp]=1;
        }
        for(i=0;i<m+m+n+n;i++)
        {
                num=i%n;
                temp=i%m;
                if(a[num]==1 || b[temp]==1)
                {
                        a[num]=1;
                        b[temp]=1;
                }

        }
        for(i=0;i<n;i++)
        {
                if(a[i]==0){
                        num=-99;

                        break;
                }
        }
        for(i=0;i<m;i++)
        {
                if(b[i]==0){
                        num=-99;

                        break;
                }
        }
        if(num==-99)
                printf("No");
        else
                printf("Yes");
        return 0;
}


#include<stdio.h>
int main()
{
        int n,m,i;
        scanf("%d %d",&n,&m);
        int a[n];
        int b[m];
        for(i=0;i<n;i++)
                a[i]=0;
        for(i=0;i<m;i++)
                b[i]=0;
        int temp,num;
        scanf("%d",&num);
        while(num--){
                scanf("%d",&temp);
                a[temp]=1;
        }
        scanf("%d",&num);
        while(num--)
        {
                scanf("%d",&temp);
                b[temp]=1;
        }
        for(i=0;i<n+m;i++)
        {
                num=i%n;
                temp=i%m;
                if(a[num]==1 || b[temp]==1)
                {
                        a[num]=1;
                        b[temp]=1;
                }

        }
        for(i=0;i<n;i++)
        {
                if(a[i]==0){
                        num=-99;
                        break;
                }
        }
        for(i=0;i<m;i++)
        {
                if(b[i]==0){
                        num=-99;
                        break;
                }
        }
        if(num==-99)
                printf("No");
        else
                printf("Yes");
        return 0;
}


#include<stdio.h>
int main()
{
        int n,m,i;
        scanf("%d %d",&n,&m);
        int a[n];
        int b[m];
        int temp,num;
        scanf("%d",&num);
        while(num--){
                scanf("%d",&temp);
                a[temp]=1;
        }
        scanf("%d",&num);
        while(num--)
        {
                scanf("%d",&temp);
                b[temp]=1;
        }
        for(i=0;i<n+m;i++)
        {
                num=i%n;
                temp=i%m;
                if(a[num]==1 || b[temp]==1)
                {
                        a[num]=1;
                        b[temp]=1;
                }

        }
        for(i=0;i<n;i++)
        {
                if(a[i]==0){
                        num=-99;
                        break;
                }
        }
        for(i=0;i<m;i++)
        {
                if(b[i]==0){
                        num=-99;
                        break;
                }
        }
        if(num==-99)
                printf("No");
        else
                printf("Yes");
        return 0;
}


#include<stdio.h>
int main()
{
        int n,m,i;
        scanf("%d %d",&n,&m);
        int a[n];
        int b[m];
        for(i=0;i<n;i++)
                a[i]=0;
        for(i=0;i<m;i++)
                b[i]=0;
        int temp,num;
        scanf("%d",&num);
        while(num--){
                scanf("%d",&temp);
                a[temp]=1;
        }
        scanf("%d",&num);
        while(num--)
        {
                scanf("%d",&temp);
                b[temp]=1;
        }
        for(i=0;i<(n+1)*(m+1);i++)
        {
                num=i%n;
                temp=i%m;
                if(a[num]==1 || b[temp]==1)
                {
                        a[num]=1;
                        b[temp]=1;
                }

        }
        for(i=0;i<n;i++)
        {
                if(a[i]==0){
                        num=-99;

                        break;
                }
        }
        for(i=0;i<m;i++)
        {
                if(b[i]==0){
                        num=-99;

                        break;
                }
        }
        if(num==-99)
                printf("No");
        else
                printf("Yes");
        return 0;
}


#include<stdio.h>
int main()
{
        int n,m,i;
        scanf("%d %d",&n,&m);
        int a[n];
        int b[m];
        for(i=0;i<n;i++)
                a[i]=0;
        for(i=0;i<m;i++)
                b[i]=0;
        int temp,num;
        scanf("%d",&num);
        while(num--){
                scanf("%d",&temp);
                a[temp]=1;
        }
        scanf("%d",&num);
        while(num--)
        {
                scanf("%d",&temp);
                b[temp]=1;
        }
        for(i=0;i<m+m+n+n;i++)
        {
                num=i%n;
                temp=i%m;
                if(a[num]==1 || b[temp]==1)
                {
                        a[num]=1;
                        b[temp]=1;
                }

        }
        for(i=0;i<n;i++)
        {
                if(a[i]==0){
                        num=-99;
                        printf("%d a",i);
                        break;
                }
        }
        for(i=0;i<m;i++)
        {
                if(b[i]==0){
                        num=-99;
                        printf("%d b",i);
                        break;
                }
        }
        if(num==-99)
                printf("No");
        else
                printf("Yes");
        return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>

int boy[10005],girl[10005],a,c,hboy,hgirl,s1,s2,n,m,i,j;

void read(){
    scanf("%d %d",&n,&m);
    scanf("%d",&hboy);
    for(i = 0; i < hboy; i++){
        scanf("%d",&a);
        boy[a] = 1;
    }

    scanf("%d",&hgirl);
    for(i = 0; i < hgirl; i++){
        scanf("%d",&a);
        girl[a] = 1;
    }
}

void solve(){

    for(i = 0;i < 10000; i++){
        if(boy[i%n] || girl[i%m])
        {
            boy[i%n] = 1;
           girl[i%m] = 1;
        }
      s1 = s2 = 0;
        for(j = 0; j < n; j++) if(!boy[j]) {  s1 = 1; break; }
        for(j = 0; j < m; j++) if(!girl[j]){  s2 = 1; break; }

        if(!s1 && !s2){ puts("Yes"); return; }

    }
    puts("No");

}

int main(){

    read();
    solve();

    return 0;
}


#include<stdio.h>
#define N 100
int main() {
	int n,m;
	scanf("%d%d",&n,&m);
	//printf("n=%d,m=%d\n",n,m);
	int boy[N];
	//统计一下男生中快乐的都有谁！
	int i;
	for(i=0;i<n;i++){
		boy[i]=0;
	}

	int b;
	scanf("%d",&b);
	for(i=0;i<b;i++){
		int x;
		scanf("%d",&x);
		boy[x]=1;
	}
	//统计一下女生中快乐的都有谁！
	int girl[N];
	for(i=0;i<m;i++){
		girl[i]=0;
	}

	int g;
	scanf("%d",&g);
	for(i=0;i<g;i++){
		int x;
		scanf("%d",&x);
		girl[x]=1;
	}
	//发现的规律是：
	//总共有n,m个最大公倍数！
	int aa=n;
	int bb=m;
	while(bb!=0) {
		int tmp=aa%bb;
		aa=bb;
		bb=tmp;
	}
	int gcd=aa;
	int test=m*n/gcd;
	//printf("test=%d\n",test);
	while(1){
		int change=0;
		for(i=0;i<test;i++){
			if(boy[i%n]==1 && girl[i%m]==0){
				girl[i%m]=1;
				change=1;
			} else if(boy[i%n]==0 && girl[i%m]==1){
				boy[i%n]=1;
				change=1;
			}
		}
		if(change==0){
			break;
		}
	}
	int all_happy=1;
	for(i=0;i<n;i++){
		if(boy[i]==0){
			all_happy=0;
			//printf("%d is unhappy\n",i);
			break;
		}
	}
	for(i=0;i<m;i++){
		if(girl[i]==0){
			all_happy=0;
			//printf("%d is unhappy\n",i);
			break;
		}
	}
	if(all_happy)printf("Yes\n");
	else printf("No\n");
	return 0;
}


#include<stdio.h>
#define N 100
int main() {
	int n,m;
	scanf("%d%d",&n,&m);
	int boy[N];
	//统计一下男生中快乐的都有谁！
	int i;
	for(i=0;i<n;i++){
		boy[i]=0;
	}
	int b;
	scanf("%d",&b);
	for(i=0;i<b;i++){
		int x;
		scanf("%d",&x);
		boy[x]=1;
	}
	//统计一下女生中快乐的都有谁！
	int g;
	scanf("%d",&g);
	int girl[N];
	for(i=0;i<m;i++){
		girl[i]=0;
	}
	for(i=0;i<g;i++){
		int x;
		scanf("%d",&x);
		girl[x]=1;
	}
	//发现的规律是：
	//总共有n,m个最大公倍数！
	int aa=n;
	int bb=m;
	while(bb!=0) {
		int tmp=aa%bb;
		aa=bb;
		bb=tmp;
	}
	int gcd=aa;
	int test=m*n/gcd;
	for(i=0;i<test;i++){
		if(boy[i%n]==1 || girl[i%m]==1){
			boy[i%n]=1;
			girl[i%m]=1;
		}
	}
	int all_happy=1;
	for(i=0;i<n;i++){
		if(boy[i]==0){
			all_happy=0;
			break;
		}
	}
	for(i=0;i<m;i++){
		if(girl[i]==0){
			all_happy=0;
			break;
		}
	}
	if(all_happy)printf("YES\n");
	else printf("NO\n");
	return 0;
}


#include<stdio.h>
#define N 100
int main() {
	int n,m;
	scanf("%d%d",&n,&m);

	//printf("n=%d,m=%d\n",n,m);
	int boy[N];
	//统计一下男生中快乐的都有谁！
	int i;
	for(i=0;i<n;i++){
		boy[i]=0;
	}

	int b;
	scanf("%d",&b);
	for(i=0;i<b;i++){
		int x;
		scanf("%d",&x);
		boy[x]=1;
	}
	//统计一下女生中快乐的都有谁！
	int girl[N];
	for(i=0;i<m;i++){
		girl[i]=0;
	}

	int g;
	scanf("%d",&g);
	for(i=0;i<g;i++){
		int x;
		scanf("%d",&x);
		girl[x]=1;
	}
	//发现的规律是：
	//总共有n,m个最大公倍数！
	int aa=n;
	int bb=m;
	while(bb!=0) {
		int tmp=aa%bb;
		aa=bb;
		bb=tmp;
	}
	int gcd=aa;
	int test=m*n/gcd;
	//printf("test=%d\n",test);
	int change=0;
	while(1){
		for(i=0;i<test;i++){
			if(boy[i%n]==1 && girl[i%m]==0){
				girl[i%m]=1;
				change=1;
			} else if(boy[i%n]==0 && girl[i%m]==1){
				boy[i%n]=1;
				change=1;
			}else{
				change=0;
			}

		}
		if(change==0){
			break;
		}
	}
	int all_happy=1;
	for(i=0;i<n;i++){
		if(boy[i]==0){
			all_happy=0;
			//printf("%d is unhappy\n",i);
			break;
		}
	}
	for(i=0;i<m;i++){
		if(girl[i]==0){
			all_happy=0;
			//printf("%d is unhappy\n",i);
			break;
		}
	}
	if(all_happy)printf("YES\n");
	else printf("NO\n");
	return 0;
}


#include<stdio.h>
#define N 100
int main() {
	int n,m;
	scanf("%d%d",&n,&m);

	//printf("n=%d,m=%d\n",n,m);
	int boy[N];
	//统计一下男生中快乐的都有谁！
	int i;
	for(i=0;i<n;i++){
		boy[i]=0;
	}

	int b;
	scanf("%d",&b);
	for(i=0;i<b;i++){
		int x;
		scanf("%d",&x);
		boy[x]=1;
	}
	//统计一下女生中快乐的都有谁！
	int girl[N];
	for(i=0;i<m;i++){
		girl[i]=0;
	}

	int g;
	scanf("%d",&g);
	for(i=0;i<g;i++){
		int x;
		scanf("%d",&x);
		girl[x]=1;
	}
	//发现的规律是：
	//总共有n,m个最大公倍数！
	int aa=n;
	int bb=m;
	while(bb!=0) {
		int tmp=aa%bb;
		aa=bb;
		bb=tmp;
	}
	int gcd=aa;
	int test=m*n/gcd;
	//printf("test=%d\n",test);
	int change=0;
	while(1){
		for(i=0;i<test;i++){
			if(boy[i%n]==1 && girl[i%m]==0){
				girl[i%m]=1;
				change=1;
			} else if(boy[i%n]==0 && girl[i%m]==1){
				boy[i%n]=1;
				change=1;
			}else{
				change=0;
			}

		}
		if(change==0){
			break;
		}
	}
	int all_happy=1;
	for(i=0;i<n;i++){
		if(boy[i]==0){
			all_happy=0;
			printf("%d is unhappy\n",i);
			break;
		}
	}
	for(i=0;i<m;i++){
		if(girl[i]==0){
			all_happy=0;
			printf("%d is unhappy\n",i);
			break;
		}
	}
	if(all_happy)printf("YES\n");
	else printf("NO\n");
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>

int boy[10005],girl[10005],a,c,hboy,hgirl,s1,s2,n,m,i,j;

void read(){
	scanf("%d %d",&n,&m);
	scanf("%d",&hboy);
	for(i = 0; i < hboy; i++){
		scanf("%d",&a);
		boy[a] = 1;
	}

	scanf("%d",&hgirl);
	for(i = 0; i < hgirl; i++){
		scanf("%d",&a);
		girl[a] = 1;
	}
}

void solve(){
    c = pow((n*m),2);
	for(i = 0;i < c; i++){
		if(boy[i%n] || girl[i%m])
		{
			boy[i%n] = 1;
		   girl[i%m] = 1;
		}
      s1 = s2 = 0;
		for(j = 0; j < n; j++) if(!boy[j]) {  s1 = 1; break; }
		for(j = 0; j < m; j++) if(!girl[j]){  s2 = 1; break; }

		if(!s1 && !s2){ puts("Yes"); return; }

	}
	puts("No");

}

int main(){

	read();
	solve();

	return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>

int boy[10005],girl[10005],a,c,hboy,hgirl,s1,s2,n,m,i,j;

void read(){
	scanf("%d %d",&n,&m);
	scanf("%d",&hboy);
	for(i = 0; i < hboy; i++){
		scanf("%d",&a);
		boy[a] = 1;
	}

	scanf("%d",&hgirl);
	for(i = 0; i < hgirl; i++){
		scanf("%d",&a);
		girl[a] = 1;
	}
}

void solve(){

	for(i = 0;i < 10000; i++){
		if(boy[i%n] || girl[i%m])
		{
			boy[i%n] = 1;
		   girl[i%m] = 1;
		}
      s1 = s2 = 0;
		for(j = 0; j < n; j++) if(!boy[j]) {  s1 = 1; break; }
		for(j = 0; j < m; j++) if(!girl[j]){  s2 = 1; break; }

		if(!s1 && !s2){ puts("Yes"); return; }

	}
	puts("No");

}

int main(){

	read();
	solve();

	return 0;
}


#include<stdio.h>

int hcf(int a,int b)
{
	while(a!=b)
	{
		if(a>b)
			a-=b;
		else
			b-=a;
	}
	return a;
}
int main()
{
	int n,m;
	int bh,gh;
	scanf("%d %d",&n,&m);
	int boy[100]={0},girl[100]={0};
	int i,temp,flag=0;
	scanf("%d",&bh);
	for(i=0;i<bh;i++)
	{
		scanf("%d",&temp);
		boy[temp]=1;
	}
	scanf("%d",&gh);
	for(i=0;i<gh;i++)
	{
		scanf("%d",&temp);
		girl[temp]=1;
	}
	int ans=hcf(m,n);
	for(i=0;i<m*n*m*n;i++)
	{
		boy[i%n]=boy[i%n]|girl[i%m];
		girl[i%m]=boy[i%n]|girl[i%m];
	}
	for(i=0;i<n;i++)
	{
		if(boy[i]==0)
		{
			flag=1;
			break;
		}
	}
		for(i=0;i<m;i++)
		{
			if(girl[i]==0)
			{
				flag=1;
				break;
			}
		}
	if(flag==1)
		printf("No\n");
	else
		printf("Yes\n");
	return 0;
}


#include<stdio.h>

int hcf(int a,int b)
{
	while(a!=b)
	{
		if(a>b)
			a-=b;
		else
			b-=a;
	}
	return a;
}
int main()
{
	int n,m;
	int bh,gh;
	scanf("%d %d",&n,&m);
	int boy[100]={0},girl[100]={0};
	int i,temp,flag=0;
	scanf("%d",&bh);
	for(i=0;i<bh;i++)
	{
		scanf("%d",&temp);
		boy[temp]=1;
	}
	scanf("%d",&gh);
	for(i=0;i<gh;i++)
	{
		scanf("%d",&temp);
		girl[temp]=1;
	}
	int ans=hcf(m,n);
	for(i=0;i<m*n/ans;i++)
	{
		boy[i%n]=boy[i%n]|girl[i%m];
		girl[i%m]=boy[i%n]|girl[i%m];
	}
	for(i=0;i<n;i++)
	{
		if(boy[i]==0)
		{
			flag=1;
			break;
		}
	}
	if(flag==0)
	{
		for(i=0;i<m;i++)
		{
			if(girl[i]==0)
			{
				flag=1;
				break;
			}
		}
	}
	if(flag==1)
		printf("No\n");
	else
		printf("Yes\n");
	return 0;
}


#include <stdio.h>
#include <string.h>
int main()
{
    int b, g, i, j, ga[100], ba[100], n, m, ind, x;
    memset(ga, 0, sizeof(ga));
    memset(ba, 0, sizeof(ba));
    scanf("%d", &n);
    scanf("%d", &m);
    scanf("%d", &b);
    for(i = 0;i < b;i++)
    {
        scanf("%d", &ind);
        ba[ind] = 1;
    }
    scanf("%d", &g);
    for(i = 0;i < g;i++)
    {
        scanf("%d", &ind);
        ga[ind] = 1;
    }
    x = n * m;
    for(i = 0;i < 1000000;i++)
    {
        ga[i % m] = ga[i % m] | ba[i % n];
        ba[i % n] = ga[i % m] | ba[i % n];
    }
    for(i = 0;i < n;i++)
        if(!ba[i])
            break;
    if(i == n)
    {
        for(i = 0;i < m;i++)
            if(!ga[i])
                break;
        if(i == m)
        {
            printf("Yes");
            return 0;
        }
    }
    printf("No");
    return 0;
}


#include <stdio.h>
#include <string.h>
int main()
{
    int b, g, i, j, ga[100], ba[100], n, m, ind, x;
    memset(ga, 0, sizeof(ga));
    memset(ba, 0, sizeof(ba));
    scanf("%d", &n);
    scanf("%d", &m);
    scanf("%d", &b);
    for(i = 0;i < b;i++)
    {
        scanf("%d", &ind);
        ba[ind] = 1;
    }
    scanf("%d", &g);
    for(i = 0;i < g;i++)
    {
        scanf("%d", &ind);
        ga[ind] = 1;
    }
    x = n * m;
    for(i = 0;i < x;i++)
    {
        ga[i % m] = ga[i % m] | ba[i % n];
        ba[i % n] = ga[i % m] | ba[i % n];
    }
    for(i = 0;i < n;i++)
        if(!ba[i])
            break;
    if(i == n)
    {
        for(i = 0;i < m;i++)
            if(!ga[i])
                break;
        if(i == m)
        {
            printf("Yes");
            return 0;
        }
    }
    printf("No");
    return 0;
}


#include<stdio.h>
int i=0,max=0,g,girl[1000],boy[1000],b,a,n,m,s;
int maxy(int hallo,int kallo){
	return (hallo>kallo) ? hallo : kallo;
}
int main() {
	scanf("%d %d",&n,&m);
	max=maxy(n,m);
	scanf("%d",&b);
	for(a=0;a<b;a++){
		scanf("%d",&s);
		boy[s]=1;
	}
	scanf("%d",&g);
	for(a=0;a<g;a++){
		scanf("%d",&s);
		girl[s]=1;
	}
	for(i=0;i<=10000;i++){
		//printf("%d %d : %d %d\n",i%n,i%m,boy[i%n],girl[i%m]);
		if(boy[i%n]==1||girl[i%m]==1){
			boy[i%n]=1;
			girl[i%m]=1;
		}
	}
	for(a=0;a<n;a++){
		if(boy[a]!=1){
			printf("No\n");
			return 0;
		}
		//printf("%d ",boy[a]);
	}
	//printf("\n");
	for(a=0;a<m;a++){
		if(girl[a]!=1){
			printf("No\n");
			return 0;
		}
		//printf("%d ",girl[a]);
	}
	printf("Yes\n");
	scanf(" ");
	return 0;
}


#include<stdio.h>
int i=0,max=0,g,girl[1000],boy[1000],b,a,n,m,s;
int maxy(int hallo,int kallo){
	return (hallo>kallo) ? hallo : kallo;
}
int main() {
	scanf("%d %d",&n,&m);
	max=maxy(n,m);
	scanf("%d",&b);
	for(a=0;a<b;a++){
		scanf("%d",&s);
		boy[s]=1;
	}
	scanf("%d",&g);
	for(a=0;a<g;a++){
		scanf("%d",&s);
		girl[s]=1;
	}
	for(i=0;i<=1000;i++){
		//printf("%d %d : %d %d\n",i%n,i%m,boy[i%n],girl[i%m]);
		if(boy[i%n]==1||girl[i%m]==1){
			boy[i%n]=1;
			girl[i%m]=1;
		}
	}
	for(a=0;a<n;a++){
		if(boy[a]!=1){
			printf("No\n");
			return 0;
		}
		//printf("%d ",boy[a]);
	}
	//printf("\n");
	for(a=0;a<m;a++){
		if(girl[a]!=1){
			printf("No\n");
			return 0;
		}
		//printf("%d ",girl[a]);
	}
	printf("Yes\n");
	scanf(" ");
	return 0;
}


#include<stdio.h>
int i=0,max=0,g,girl[1000],boy[1000],b,a,n,m,s;
int maxy(int hallo,int kallo){
	return (hallo>kallo) ? hallo : kallo;
}
int main() {
	scanf("%d %d",&n,&m);
	max=maxy(n,m);
	scanf("%d",&b);
	for(a=0;a<b;a++){
		scanf("%d",&s);
		boy[s]=1;
	}
	scanf("%d",&g);
	for(a=0;a<g;a++){
		scanf("%d",&s);
		girl[s]=1;
	}
	for(i=0;i<100;i++){
		//printf("%d %d : %d %d\n",i%n,i%m,boy[i%n],girl[i%m]);
		if(boy[i%n]==1||girl[i%m]==1){
			boy[i%n]=1;
			girl[i%m]=1;
		}
	}
	for(a=0;a<n;a++){
		if(boy[a]!=1){
			printf("No\n");
			return 0;
		}
		//printf("%d ",boy[a]);
	}
	//printf("\n");
	for(a=0;a<m;a++){
		if(girl[a]!=1){
			printf("No\n");
			return 0;
		}
		//printf("%d ",girl[a]);
	}
	printf("Yes\n");
	scanf(" ");
	return 0;
}


#include<stdio.h>
int i=0,max=0,g,girl[1000],boy[1000],b,a,n,m,s;
int maxy(int hallo,int kallo){
	return (hallo>kallo) ? hallo : kallo;
}
int main() {
	scanf("%d %d",&n,&m);
	max=maxy(n,m);
	scanf("%d",&b);
	for(a=0;a<b;a++){
		scanf("%d",&s);
		boy[s]=1;
	}
	scanf("%d",&g);
	for(a=0;a<g;a++){
		scanf("%d",&s);
		girl[s]=1;
	}
	for(i=0;i<m+n;i++){
		//printf("%d %d : %d %d\n",i%n,i%m,boy[i%n],girl[i%m]);
		if(boy[i%n]==1||girl[i%m]==1){
			boy[i%n]=1;
			girl[i%m]=1;
		}
	}
	for(a=0;a<n;a++){
		if(boy[a]!=1){
			printf("No\n");
			return 0;
		}
		//printf("%d ",boy[a]);
	}
	for(a=0;a<m;a++){
		if(girl[a]!=1){
			printf("No\n");
			return 0;
		}
		//printf("%d ",girl[a]);
	}
	printf("Yes\n");
	scanf(" ");
	return 0;
}


#include<stdio.h>
int i=0,max=0,g,girl[1000],boy[1000],b,a,n,m,s;
int maxy(int hallo,int kallo){
	return (hallo>kallo) ? hallo : kallo;
}
int main() {
	scanf("%d %d",&n,&m);
	max=maxy(n,m);
	scanf("%d",&b);
	for(a=0;a<b;a++){
		scanf("%d",&s);
		boy[s]=1;
	}
	scanf("%d",&g);
	for(a=0;a<g;a++){
		scanf("%d",&s);
		girl[s]=1;
	}
	for(i=0;i<m*n;i++){
		//printf("%d %d : %d %d\n",i%n,i%m,boy[i%n],girl[i%m]);
		if(boy[i%n]==1||girl[i%m]==1){
			boy[i%n]=1;
			girl[i%m]=1;
		}
	}
	for(a=0;a<n;a++){
		if(boy[a]!=1){
			printf("No\n");
			return 0;
		}
		//printf("%d ",boy[a]);
	}
	//printf("\n");
	for(a=0;a<m;a++){
		if(girl[a]!=1){
			printf("No\n");
			return 0;
		}
		//printf("%d ",girl[a]);
	}
	printf("Yes\n");
	scanf(" ");
	return 0;
}


#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "string.h"
#include "stdbool.h"

int main(){
	int n,m,i,g,p;
	scanf("%d %d", &n, &m);
	int sad=n+m;

	int b;
	scanf("%d", &b);
	sad-=b;
	bool boys[n];
	memset(boys, 0, n);
	for (i=0; i<b; i++)
		scanf("%d", &p), boys[p]=1;

	scanf("%d", &b);
	sad-=b;
	bool girls[m];
	memset(girls, 0, m);
	for (i=0; i<b; i++)
		scanf("%d", &p), girls[p]=1;

	b=0,g=0;
	bool workdone = 0;
	while (sad>0){
		if (boys[b])
			if (!girls[g])
				girls[g] = 1, workdone=1, sad--;
		if (girls[g])
			if (!boys[b])
				boys[b] = 1, workdone=1, sad--;
		b = (b+1) % n;
		g = (g+1) % m;
		if (b+g == 0){
			if (workdone == 0)
				break;
			workdone=0;
		}
	}

	if (sad==0)
		printf("YES");
	else
		printf("NO");

	return 0;
}


#include <stdio.h>
#define ff fflush(stdin)
int main () {
    int n,m,b,g,x[1001],y[1001],i,j;

    scanf("%d%d",&n,&m);
    scanf("%d",&b);
    for (i=0;i<b;i++)
        scanf("%d",&x[i]);
    scanf("%d",&g);
    for (i=0;i<g;i++)
        scanf("%d",&y[i]);

    /*for (i=0;i<g;i++)
        printf("%d ",y[i]);*/

    //initialize happy boy/girl
    int boy[1001],girl[1001];
    for (i=0;i<b;i++)
        boy[x[i]]=1;
    for (i=0;i<g;i++)
        girl[y[i]]=1;

    int max=n>m?n:m;

    for (i=0;i<(n*m*2);i++){

        if (boy[i%n]==1 || girl[i%m]==1){
            boy[i%n]=1;
            girl[i%m]=1;

        }

    /*  for (j=0;j<n;j++){
            printf("%d ",boy[j]);
        }

        printf("\n\n");
        for (j=0;j<m;j++){
            printf("%d ",girl[j]);
        }
        printf("\n\n");
        getchar();*/

    }

    int win=1;

    for (i=0;i<n;i++){
        if (boy[i]!=1){
            win=0;
            break;
        }
    }
    for (i=0;i<m;i++){
        if (girl[i]!=1){
            win=0;
            break;
        }
    }

    if (win==0)
        printf("No");
    else
        printf("Yes");

    return 0;
}


#include <stdio.h>
#define ff fflush(stdin)
int main () {
    int n,m,b,g,x[1001],y[1001],i,j;

    scanf("%d%d",&n,&m);
    scanf("%d",&b);
    for (i=0;i<b;i++)
        scanf("%d",&x[i]);
    scanf("%d",&g);
    for (i=0;i<g;i++)
        scanf("%d",&y[i]);

    /*for (i=0;i<g;i++)
        printf("%d ",y[i]);*/

    //initialize happy boy/girl
    int boy[1001],girl[1001];
    for (i=0;i<b;i++)
        boy[x[i]]=1;
    for (i=0;i<g;i++)
        girl[y[i]]=1;

    int max=n>m?n:m;

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

        if (boy[i%n]==1 || girl[i%m]==1){
            boy[i%n]=1;
            girl[i%m]=1;

        }

    /*  for (j=0;j<n;j++){
            printf("%d ",boy[j]);
        }

        printf("\n\n");
        for (j=0;j<m;j++){
            printf("%d ",girl[j]);
        }
        printf("\n\n");
        getchar();*/

    }

    int win=1;

    for (i=0;i<n;i++){
        if (boy[i]!=1){
            win=0;
            break;
        }
    }
    for (i=0;i<m;i++){
        if (girl[i]!=1){
            win=0;
            break;
        }
    }

    if (win==0)
        printf("No");
    else
        printf("Yes");

    return 0;
}


#include <stdio.h>
#define ff fflush(stdin)
int main () {
    int n,m,b,g,x[101],y[101],i,j;

    scanf("%d%d",&n,&m);
    scanf("%d",&b);
    for (i=0;i<b;i++)
        scanf("%d",&x[i]);
    scanf("%d",&g);
    for (i=0;i<g;i++)
        scanf("%d",&y[i]);

    /*for (i=0;i<g;i++)
        printf("%d ",y[i]);*/

    //initialize happy boy/girl
    int boy[101],girl[101];
    for (i=0;i<b;i++)
        boy[x[i]]=1;
    for (i=0;i<g;i++)
        girl[y[i]]=1;

    int max=n>m?n:m;

    for (i=0;i<2*max;i++){

        if (boy[i%n]==1 || girl[i%m]==1){
            boy[i%n]=1;
            girl[i%m]=1;

        }

    /*  for (j=0;j<n;j++){
            printf("%d ",boy[j]);
        }

        printf("\n\n");
        for (j=0;j<m;j++){
            printf("%d ",girl[j]);
        }
        printf("\n\n");
        getchar();*/

    }

    int win=1;

    for (i=0;i<n;i++){
        if (boy[i]!=1){
            win=0;
            break;
        }
    }
    for (i=0;i<m;i++){
        if (girl[i]!=1){
            win=0;
            break;
        }
    }

    if (win==0)
        printf("No");
    else
        printf("Yes");

    return 0;
}


#include <stdio.h>
#define ff fflush(stdin)
int main () {
    int n,m,b,g,x[1001],y[1001],i,j;

    scanf("%d%d",&n,&m);
    scanf("%d",&b);
    for (i=0;i<b;i++)
        scanf("%d",&x[i]);
    scanf("%d",&g);
    for (i=0;i<g;i++)
        scanf("%d",&y[i]);

    /*for (i=0;i<g;i++)
        printf("%d ",y[i]);*/

    //initialize happy boy/girl
    int boy[1001],girl[1001];
    for (i=0;i<b;i++)
        boy[x[i]]=1;
    for (i=0;i<g;i++)
        girl[y[i]]=1;

    int max=n>m?n:m;

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

        if (boy[i%n]==1 || girl[i%m]==1){
            boy[i%n]=1;
            girl[i%m]=1;

        }

    /*  for (j=0;j<n;j++){
            printf("%d ",boy[j]);
        }

        printf("\n\n");
        for (j=0;j<m;j++){
            printf("%d ",girl[j]);
        }
        printf("\n\n");
        getchar();*/

    }

    int win=1;

    for (i=0;i<n;i++){
        if (boy[i]!=1){
            win=0;
            break;
        }
    }
    for (i=0;i<m;i++){
        if (girl[i]!=1){
            win=0;
            break;
        }
    }

    if (win==0)
        printf("No");
    else
        printf("Yes");

    return 0;
}


#include <stdio.h>
#define ff fflush(stdin)
int main () {
    int n,m,b,g,x[1001],y[1001],i,j;

    scanf("%d%d",&n,&m);
    scanf("%d",&b);
    for (i=0;i<b;i++)
        scanf("%d",&x[i]);
    scanf("%d",&g);
    for (i=0;i<g;i++)
        scanf("%d",&y[i]);

    /*for (i=0;i<g;i++)
        printf("%d ",y[i]);*/

    //initialize happy boy/girl
    int boy[1001],girl[1001];
    for (i=0;i<b;i++)
        boy[x[i]]=1;
    for (i=0;i<g;i++)
        girl[y[i]]=1;

    int max=n>m?n:m;

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

        if (boy[i%n]==1 || girl[i%m]==1){
            boy[i%n]=1;
            girl[i%m]=1;

        }

    /*  for (j=0;j<n;j++){
            printf("%d ",boy[j]);
        }

        printf("\n\n");
        for (j=0;j<m;j++){
            printf("%d ",girl[j]);
        }
        printf("\n\n");
        getchar();*/

    }

    int win=1;

    for (i=0;i<n;i++){
        if (boy[i]!=1){
            win=0;
            break;
        }
    }
    for (i=0;i<m;i++){
        if (girl[i]!=1){
            win=0;
            break;
        }
    }

    if (win==0)
        printf("No");
    else
        printf("Yes");

    return 0;
}


#include <stdio.h>
#define ff fflush(stdin)
int main () {
    int n,m,b,g,x[1001],y[1001],i,j;

    scanf("%d%d",&n,&m);
    scanf("%d",&b);
    for (i=0;i<b;i++)
        scanf("%d",&x[i]);
    scanf("%d",&g);
    for (i=0;i<g;i++)
        scanf("%d",&y[i]);

    /*for (i=0;i<g;i++)
        printf("%d ",y[i]);*/

    //initialize happy boy/girl
    int boy[1001],girl[1001];
    for (i=0;i<b;i++)
        boy[x[i]]=1;
    for (i=0;i<g;i++)
        girl[y[i]]=1;

    int max=n>m?n:m;

    for (i=0;i<(n*m+2);i++){

        if (boy[i%n]==1 || girl[i%m]==1){
            boy[i%n]=1;
            girl[i%m]=1;

        }

    /*  for (j=0;j<n;j++){
            printf("%d ",boy[j]);
        }

        printf("\n\n");
        for (j=0;j<m;j++){
            printf("%d ",girl[j]);
        }
        printf("\n\n");
        getchar();*/

    }

    int win=1;

    for (i=0;i<n;i++){
        if (boy[i]!=1){
            win=0;
            break;
        }
    }
    for (i=0;i<m;i++){
        if (girl[i]!=1){
            win=0;
            break;
        }
    }

    if (win==0)
        printf("No");
    else
        printf("Yes");

    return 0;
}


#include <stdio.h>
#define ff fflush(stdin)
int main () {
    int n,m,b,g,x[1001],y[1001],i,j;

    scanf("%d%d",&n,&m);
    scanf("%d",&b);
    for (i=0;i<b;i++)
        scanf("%d",&x[i]);
    scanf("%d",&g);
    for (i=0;i<g;i++)
        scanf("%d",&y[i]);

    /*for (i=0;i<g;i++)
        printf("%d ",y[i]);*/

    //initialize happy boy/girl
    int boy[101],girl[101];
    for (i=0;i<b;i++)
        boy[x[i]]=1;
    for (i=0;i<g;i++)
        girl[y[i]]=1;

    int max=n>m?n:m;

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

        if (boy[i%n]==1 || girl[i%m]==1){
            boy[i%n]=1;
            girl[i%m]=1;

        }

    /*  for (j=0;j<n;j++){
            printf("%d ",boy[j]);
        }

        printf("\n\n");
        for (j=0;j<m;j++){
            printf("%d ",girl[j]);
        }
        printf("\n\n");
        getchar();*/

    }

    int win=1;

    for (i=0;i<n;i++){
        if (boy[i]!=1){
            win=0;
            break;
        }
    }
    for (i=0;i<m;i++){
        if (girl[i]!=1){
            win=0;
            break;
        }
    }

    if (win==0)
        printf("No");
    else
        printf("Yes");

    return 0;
}


#include <stdio.h>
#define ff fflush(stdin)
int main () {
    int n,m,b,g,x[1001],y[1001],i,j;

    scanf("%d%d",&n,&m);
    scanf("%d",&b);
    for (i=0;i<b;i++)
        scanf("%d",&x[i]);
    scanf("%d",&g);
    for (i=0;i<g;i++)
        scanf("%d",&y[i]);

    /*for (i=0;i<g;i++)
        printf("%d ",y[i]);*/

    //initialize happy boy/girl
    int boy[1001],girl[1001];
    for (i=0;i<b;i++)
        boy[x[i]]=1;
    for (i=0;i<g;i++)
        girl[y[i]]=1;

    int max=n>m?n:m;

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

        if (boy[i%n]==1 || girl[i%m]==1){
            boy[i%n]=1;
            girl[i%m]=1;

        }

    /*  for (j=0;j<n;j++){
            printf("%d ",boy[j]);
        }

        printf("\n\n");
        for (j=0;j<m;j++){
            printf("%d ",girl[j]);
        }
        printf("\n\n");
        getchar();*/

    }

    int win=1;

    for (i=0;i<n;i++){
        if (boy[i]!=1){
            win=0;
            break;
        }
    }
    for (i=0;i<m;i++){
        if (girl[i]!=1){
            win=0;
            break;
        }
    }

    if (win==0)
        printf("No");
    else
        printf("Yes");

    return 0;
}


#include <stdio.h>
#define ff fflush(stdin)
int main () {
    int n,m,b,g,x[1001],y[1001],i,j;

    scanf("%d%d",&n,&m);
    scanf("%d",&b);
    for (i=0;i<b;i++)
        scanf("%d",&x[i]);
    scanf("%d",&g);
    for (i=0;i<g;i++)
        scanf("%d",&y[i]);

    /*for (i=0;i<g;i++)
        printf("%d ",y[i]);*/

    //initialize happy boy/girl
    int boy[101],girl[101];
    for (i=0;i<b;i++)
        boy[x[i]]=1;
    for (i=0;i<g;i++)
        girl[y[i]]=1;

    int max=n>m?n:m;

    for (i=0;i<2*max;i++){

        if (boy[i%n]==1 || girl[i%m]==1){
            boy[i%n]=1;
            girl[i%m]=1;

        }

    /*  for (j=0;j<n;j++){
            printf("%d ",boy[j]);
        }

        printf("\n\n");
        for (j=0;j<m;j++){
            printf("%d ",girl[j]);
        }
        printf("\n\n");
        getchar();*/

    }

    int win=1;

    for (i=0;i<n;i++){
        if (boy[i]!=1){
            win=0;
            break;
        }
    }
    for (i=0;i<m;i++){
        if (girl[i]!=1){
            win=0;
            break;
        }
    }

    if (win==0)
        printf("No");
    else
        printf("Yes");

    return 0;
}


#include <stdio.h>
#define ff fflush(stdin)
int main () {
    int n,m,b,g,x[1001],y[1001],i,j;

    scanf("%d%d",&n,&m);
    scanf("%d",&b);
    for (i=0;i<b;i++)
        scanf("%d",&x[i]);
    scanf("%d",&g);
    for (i=0;i<g;i++)
        scanf("%d",&y[i]);

    /*for (i=0;i<g;i++)
        printf("%d ",y[i]);*/

    //initialize happy boy/girl
    int boy[101],girl[101];
    for (i=0;i<b;i++)
        boy[x[i]]=1;
    for (i=0;i<g;i++)
        girl[y[i]]=1;

    int max=n>m?n:m;

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

        if (boy[i%n]==1 || girl[i%m]==1){
            boy[i%n]=1;
            girl[i%m]=1;

        }

    /*  for (j=0;j<n;j++){
            printf("%d ",boy[j]);
        }

        printf("\n\n");
        for (j=0;j<m;j++){
            printf("%d ",girl[j]);
        }
        printf("\n\n");
        getchar();*/

    }

    int win=1;

    for (i=0;i<n;i++){
        if (boy[i]!=1){
            win=0;
            break;
        }
    }
    for (i=0;i<m;i++){
        if (girl[i]!=1){
            win=0;
            break;
        }
    }

    if (win==0)
        printf("No");
    else
        printf("Yes");

    return 0;
}


#include <stdio.h>
#define ff fflush(stdin)
int main () {
    int n,m,b,g,x[101],y[101],i,j;

    scanf("%d%d",&n,&m);
    scanf("%d",&b);
    for (i=0;i<b;i++)
        scanf("%d",&x[i]);
    scanf("%d",&g);
    for (i=0;i<g;i++)
        scanf("%d",&y[i]);

    /*for (i=0;i<g;i++)
        printf("%d ",y[i]);*/

    //initialize happy boy/girl
    int boy[101],girl[101];
    for (i=0;i<b;i++)
        boy[x[i]]=1;
    for (i=0;i<g;i++)
        girl[y[i]]=1;

    int max=n>m?n:m;

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

        if (boy[i%n]==1 || girl[i%m]==1){
            boy[i%n]=1;
            girl[i%m]=1;

        }

    /*  for (j=0;j<n;j++){
            printf("%d ",boy[j]);
        }

        printf("\n\n");
        for (j=0;j<m;j++){
            printf("%d ",girl[j]);
        }
        printf("\n\n");
        getchar();*/

    }

    int win=1;

    for (i=0;i<n;i++){
        if (boy[i]!=1){
            win=0;
            break;
        }
    }
    for (i=0;i<m;i++){
        if (girl[i]!=1){
            win=0;
            break;
        }
    }

    if (win==0)
        printf("No");
    else
        printf("Yes");

    return 0;
}


#include <stdio.h>
#define ff fflush(stdin)
int main () {
    int n,m,b,g,x[1001],y[1001],i,j;

    scanf("%d%d",&n,&m);
    scanf("%d",&b);
    for (i=0;i<b;i++)
        scanf("%d",&x[i]);
    scanf("%d",&g);
    for (i=0;i<g;i++)
        scanf("%d",&y[i]);

    /*for (i=0;i<g;i++)
        printf("%d ",y[i]);*/

    //initialize happy boy/girl
    int boy[1001],girl[1001];
    for (i=0;i<b;i++)
        boy[x[i]]=1;
    for (i=0;i<g;i++)
        girl[y[i]]=1;

    int max=n>m?n:m;

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

        if (boy[i%n]==1 || girl[i%m]==1){
            boy[i%n]=1;
            girl[i%m]=1;

        }

    /*  for (j=0;j<n;j++){
            printf("%d ",boy[j]);
        }

        printf("\n\n");
        for (j=0;j<m;j++){
            printf("%d ",girl[j]);
        }
        printf("\n\n");
        getchar();*/

    }

    int win=1;

    for (i=0;i<n;i++){
        if (boy[i]!=1){
            win=0;
            break;
        }
    }
    for (i=0;i<m;i++){
        if (girl[i]!=1){
            win=0;
            break;
        }
    }

    if (win==0)
        printf("No");
    else
        printf("Yes");

    return 0;
}


#include <stdio.h>
#include <string.h>

int seq[101][2];

int max(int a, int b)
{
    return a > b ? a : b;
}

int main(void) {
    int i;
    memset(seq, 0, sizeof seq);
    int n, m;
    scanf("%d %d", &n, &m);

    int q;
    scanf("%d", &q);
    for (i = 0; i < q; i++)
    {
        int vl;
        scanf("%d", &vl);
        seq[vl][0] = 1;
    }

    scanf("%d", &q);
    for (i = 0; i < q; i++)
    {
        int vl;
        scanf("%d", &vl);
        seq[vl][1] = 1;
    }

    for (i = 0; i <= max(n, m) * max(n, m); i++)
    {
        if (seq[i % n][0] || seq[i % m][1])
        {
            seq[i % n][0] = seq[i % m][1] = 1;
        }
    }

    int fl = 0;
    for (i = 0; i < n; i++)
        fl |= (1 - seq[i][0]);

    for (i = 0; i < m; i++)
        fl |= (1 - seq[i][1]);

    printf("%s\n", !fl ? "Yes" : "No");

    return 0;
}


#include <stdio.h>
#include <string.h>

int seq[101][2];

inline int max(int a, int b)
{
    return a > b ? a : b;
}

int main(void) {
    int i;
    memset(seq, 0, sizeof seq);
    int n, m;
    scanf("%d %d", &n, &m);

    int q;
    scanf("%d", &q);
    for (i = 0; i < q; i++)
    {
        int vl;
        scanf("%d", &vl);
        seq[vl][0] = 1;
    }

    scanf("%d", &q);
    for (i = 0; i < q; i++)
    {
        int vl;
        scanf("%d", &vl);
        seq[vl][1] = 1;
    }

    for (i = 0; i <= max(n, m) * max(n, m); i++)
    {
        if (seq[i % n][0] || seq[i % m][1])
        {
            seq[i % n][0] = seq[i % m][1] = 1;
        }
    }

    int fl = 0;
    for (i = 0; i < n; i++)
        fl |= (1 - seq[i][0]);

    for (i = 0; i < m; i++)
        fl |= (1 - seq[i][1]);

    printf("%s\n", !fl ? "Yes" : "No");

    return 0;
}


#include <stdio.h>
#include <string.h>

int seq[101][2];

int main(void) {
    int i;
    memset(seq, 0, sizeof seq);
    int n, m;
    scanf("%d %d", &n, &m);

    int q;
    scanf("%d", &q);
    for (i = 0; i < q; i++)
    {
        int vl;
        scanf("%d", &vl);
        seq[vl][0] = 1;
    }

    scanf("%d", &q);
    for (i = 0; i < q; i++)
    {
        int vl;
        scanf("%d", &vl);
        seq[vl][1] = 1;
    }

    for (i = 0; i < n * m; i++)
    {
        if (seq[i % n][0] || seq[i % m][1])
        {
            seq[i % n][0] = seq[i % m][1] = 1;
        }
    }

    int fl = 0;
    for (i = 0; i < n; i++)
        fl |= (1 - seq[i][0]);

    for (i = 0; i < m; i++)
        fl |= (1 - seq[i][1]);

    printf("%s\n", !fl ? "Yes" : "No");

    return 0;
}


#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    int n,m,b,g,arr1[1000],arr2[1000],c1,c2,p1,p2,max=0,flag=0,temp;
    memset(arr1,0,sizeof(arr1));
    memset(arr2,0,sizeof(arr2));
    int sum=0;
    cin>>n>>m;
    cin>>b;
    for(int i=0;i<b;i++)
       {    cin>>temp;
          arr1[temp]=1;
       }
        cin>>g;
    for(int i=0;i<g;i++)
        {    cin>>temp;
          arr2[temp]=1;
       }

         max=(n>m) ? n : m;

        while(1)
            {
                if(max%n==0 && max%m==0)
             {
                 flag=max;
            break;
            }
            ++max;
         }
         //cout<<flag<<endl;
         sum=b+g;
         for(int i=0;i<flag;i++)
         {
             int p1=i%n;
             int p2=i%m;
             if(arr1[p1]==1 && arr2[p2]==1)
                continue;
             else if(arr1[p1]==1 && arr2[p2]==0)
             {
                 sum++;
                 arr2[p2]=1;
             }
             else if(arr1[p1]==0 && arr2[p2]==1)
             {
                 sum++;
                 arr1[p1]=1;
             }

         }
         //cout<<"count"<<count<<endl;
         if(sum==n+m)
                cout<<"YES"<<endl;
                else cout<<"NO"<<endl;
}


#include<stdio.h>
#include<string.h>
int main()
{
    int n,m,b,g,arr1[1000],arr2[1000],c1,c2,p1,p2,max=0,flag=0,temp,i;
    memset(arr1,0,sizeof(arr1));
    memset(arr2,0,sizeof(arr2));
    int sum=0;
    scanf("%d",&n);scanf("%d",&m);
    scanf("%d",&b);
    for(i=0;i<b;i++)
       {    scanf("%d",&temp);
          arr1[temp]=1;
       }
     scanf("%d",&g);
    for( i=0;i<g;i++)
        {   scanf("%d",&temp);
          arr2[temp]=1;
       }

         max=(n>m) ? n : m;

        while(1)
            {
                if(max%n==0 && max%m==0)
             {
                 flag=max;
            break;
            }
            ++max;
         }
         //cout<<flag<<endl;
         sum=b+g;
         for(i=0;i<flag;i++)
         {
             int p1=i%n;
             int p2=i%m;
             if(arr1[p1]==1 && arr2[p2]==1)
                continue;
             else if(arr1[p1]==1 && arr2[p2]==0)
             {
                 sum++;
                 arr2[p2]=1;
             }
             else if(arr1[p1]==0 && arr2[p2]==1)
             {
                 sum++;
                 arr1[p1]=1;
             }

         }
         //cout<<"count"<<count<<endl;
         if(sum==n+m)
                printf("YES\n");
                else printf("NO\n");
                return 0;
}


#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    int n,m,b,g,arr1[1000],arr2[1000],c1,c2,p1,p2,max=0,flag=0,temp;
    memset(arr1,0,sizeof(arr1));
    memset(arr2,0,sizeof(arr2));
    int sum=0;
    cin>>n>>m;
    cin>>b;
    for(int i=0;i<b;i++)
       {    cin>>temp;
          arr1[temp]=1;
       }
        cin>>g;
    for(int i=0;i<g;i++)
        {    cin>>temp;
          arr2[temp]=1;
       }

         max=(n>m) ? n : m;

        while(1)
            {
                if(max%n==0 && max%m==0)
             {
                 flag=max;
            break;
            }
            ++max;
         }
         //cout<<flag<<endl;
         sum=b+g;
         for(int i=0;i<flag;i++)
         {
             int p1=i%n;
             int p2=i%m;
             if(arr1[p1]==1 && arr2[p2]==1)
                continue;
             else if(arr1[p1]==1 && arr2[p2]==0)
             {
                 sum++;
                 arr2[p2]=1;
             }
             else if(arr1[p1]==0 && arr2[p2]==1)
             {
                 sum++;
                 arr1[p1]=1;
             }

         }
         //cout<<"count"<<count<<endl;
         if(sum==n+m)
                cout<<"YES"<<endl;
                else cout<<"NO"<<endl;
                return 0;
}


using System;
using System.IO;
using System.Text;

namespace String_Manipulation_1._0
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            int n = Next();

            string s = NextS();
            var counts = new int['z' - 'a' + 1];
            foreach (char c in s)
            {
                counts[c - 'a']++;
            }

            var nn = new string[n];
            for (int i = 0; i < n; i++)
            {
                nn[i] = (string) s.Clone();
            }
            var mm = new int[n][];
            for (int i = 0; i < n; i++)
            {
                mm[i] = (int[]) counts.Clone();
            }

            int m = Next();
            for (int ii = 0; ii < m; ii++)
            {
                int p = Next();
                char cc = NextS()[0];
                int c = cc - 'a';

                for (int i = 0; i < n; i++)
                {
                    if (mm[i][c] >= p)
                    {
                        int count = 0;
                        for (int j = 0; j < nn[i].Length; j++)
                        {
                            if (nn[i][j] == cc)
                                count++;

                            if (count == p)
                            {
                                nn[i] = nn[i].Remove(j, 1);
                                break;
                            }
                        }
                        mm[i][c]--;
                        break;
                    }
                    p -= mm[i][c];
                }
            }

            foreach (string ss in nn)
            {
                writer.Write(ss);
            }
            writer.Flush();
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }

        private static string NextS()
        {
            int c;
            var res = new StringBuilder();
            do
            {
                c = reader.Read();
            } while (c < 'a' || c > 'z');
            res.Append((char) c);
            while (true)
            {
                c = reader.Read();
                if (c < 'a' || c > 'z')
                    return res.ToString();
                res.Append((char) c);
            }
        }
    }
}


using System;
using System.Text;

namespace ConsoleApplication1
{

    internal class Program
    {
        private static void Main(string[] args)
        {
            int k = Convert.ToInt32(Console.ReadLine());
            string text = Console.ReadLine();

            StringBuilder a = new StringBuilder();
            for (int i = 0; i < k; i++)
            {
                a.Append(text);
            }

            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                string[] line = Console.ReadLine().Split(' ');
                int index = Convert.ToInt32(line[0]);
                string s = line[1];

                int counter = 0;

                for (int j = 0; j < a.Length; j++)
                {
                    if (a[j].ToString() == s)
                    {
                        counter++;
                        if (counter == index)
                        {
                            a = a.Remove(j, 1);
                            break;
                        }
                    }
                }
            }

            Console.WriteLine(a);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace ConsoleApplication1
{

    internal class Program
    {
        private static void Main(string[] args)
        {
            int k = Convert.ToInt32(Console.ReadLine());
            string text = Console.ReadLine();
            string a = text;
            for (int i = 1; i < k; i++)
            {
                a += text;
            }

            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                string[] line = Console.ReadLine().Split(' ');
                int index = Convert.ToInt32(line[0]);
                string s = line[1];

                int counter = 0;

                for (int j = 0; j < a.Length; j++)
                {
                    if (a[j].ToString() == s)
                    {
                        counter++;
                        if (counter == index)
                        {
                            a = a.Remove(j, 1);
                            break;
                        }
                    }
                }
            }

            Console.WriteLine(a);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace ConsoleApplication1
{

    internal class Program
    {
        private static void Main(string[] args)
        {
            int k = Convert.ToInt32(Console.ReadLine());
            string text = Console.ReadLine();
            string a = text;
            for (int i = 1; i < k; i++)
            {
                a += text;
            }

            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                string[] line = Console.ReadLine().Split(' ');
                int index = Convert.ToInt32(line[0]);
                string s = line[1];

                int counter = 0;

                for (int j = 0; j < a.Length; j++)
                {
                    if (a[j].ToString() == s)
                    {
                        counter++;
                        if (counter == index)
                        {
                            a = a.Remove(j, 1);
                            break;
                        }
                    }
                }
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace ConsoleApplication1
{

    internal class Program
    {
        private static void Main(string[] args)
        {
            int k = Convert.ToInt32(Console.ReadLine());
            char[] text = Console.ReadLine().ToCharArray();

            StringBuilder a = new StringBuilder();
            for (int i = 0; i < k; i++)
            {
                a.Append(text);
            }

            int n = Convert.ToInt32(Console.ReadLine());

            Dictionary<char, List<int>> dic = new Dictionary<char, List<int>>();
            for (int i = 0; i < a.Length; i++)
            {
                if (!dic.ContainsKey(a[i]))
                {
                    List<int> val = new List<int>();
                    for (int j = 0; j < a.Length; j++)
                    {
                        if (a[j] == a[i])
                        {
                            val.Add(j);
                        }
                    }

                    dic.Add(a[i], val);
                }
            }

            for (int i = 0; i < n; i++)
            {
                string[] line = Console.ReadLine().Split(' ');
                int occurNum = Convert.ToInt32(line[0]);
                string s = line[1];

                int index = dic[s[0]][occurNum-1];
                a[index] = '!';
                dic[s[0]].RemoveAt(occurNum-1);
            }

            //for (int i = 0; i < n; i++)
            //{
            //    string[] line = Console.ReadLine().Split(' ');
            //    int index = Convert.ToInt32(line[0]);
            //    string s = line[1];

            //    int counter = 0;

            //    for (int j = 0; j < a.Length; j++)
            //    {
            //        if (a[j].ToString() == s)
            //        {
            //            counter++;
            //            if (counter == index)
            //            {
            //                a = a.Remove(j, 1);
            //                break;
            //            }
            //        }
            //    }
            //}

            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] != '!')
                {
                    Console.Write(a[i]);
                }
            }
        }
    }
}


using System;
using System.Text;

namespace ConsoleApplication1
{

    internal class Program
    {
        private static void Main(string[] args)
        {
            int k = Convert.ToInt32(Console.ReadLine());
            char[] text = Console.ReadLine().ToCharArray();

            StringBuilder a = new StringBuilder();
            for (int i = 0; i < k; i++)
            {
                a.Append(text);
            }

            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                string[] line = Console.ReadLine().Split(' ');
                int index = Convert.ToInt32(line[0]);
                string s = line[1];

                int counter = 0;

                for (int j = 0; j < a.Length; j++)
                {
                    if (a[j].ToString() == s)
                    {
                        counter++;
                        if (counter == index)
                        {
                            a = a.Remove(j, 1);
                            break;
                        }
                    }
                }
            }

            Console.WriteLine(a);
        }
    }
}


// package ru.spbau.mit

import java.io.InputStream
import java.util.*

class BinarySegmentTree(private val size: Int) {
    private val elements: Array<Int> = Array(4 * size) { 0 }

    fun set(index: Int, value: Boolean) = set(1, 0, size, index, value)

    fun get(index: Int) = get(1, 0, size, index)

    fun getKth(k: Int): Int = getKth(1, 0, size, k)

    private fun getKth(node: Int, left: Int, right: Int, k: Int): Int {
        if (left == right) {
            return left
        }

        val middle = getMiddle(left, right)

        return if (k <= elements[leftNode(node)]) {
            getKth(leftNode(node), left, middle, k)
        } else {
            getKth(rightNode(node), middle + 1, right, k - elements[leftNode(node)])
        }
    }

    private fun set(node: Int, left: Int, right: Int, index: Int, value: Boolean): Unit {
        if (left == right) {
            elements[node] = if (value) 1 else 0
        } else {
            val middle = getMiddle(left, right)

            if (index <= middle) {
                set(leftNode(node), left, middle, index, value)
            } else {
                set(rightNode(node), middle + 1, right, index, value)
            }

            elements[node] = elements[leftNode(node)] + elements[rightNode(node)]
        }
    }

    private fun get(node: Int, left: Int, right: Int, index: Int): Boolean {
        if (left == right) {
            return elements[node] == 1
        }

        val middle = getMiddle(left, right)

        return if (index <= middle) {
            get(leftNode(node), left, middle, index)
        } else {
            get(rightNode(node), middle + 1, right, index)
        }
    }

    private fun leftNode(node: Int) = 2 * node

    private fun rightNode(node: Int) = 2 * node + 1

    private fun getMiddle(left: Int, right: Int): Int = left + (right - left) / 2
}

interface Problem<out T> {
    fun solve(): T
}

class StringManipulation(private val name: String,
                         private val queries: List<Query>) : Problem<String> {
    private val trees: Array<BinarySegmentTree> = Array(LETTERS_COUNT) {
        BinarySegmentTree(name.length)
    }

    override fun solve(): String {
        name.forEachIndexed { index, character -> getTreeByCharacter(character).set(index, true) }

        queries.forEach {
            val tree = getTreeByCharacter(it.character)
            tree.set(tree.getKth(it.index), false)
        }

        return name.filterIndexed { index, character -> getTreeByCharacter(character).get(index) }
    }

    private fun getTreeByCharacter(char: Char): BinarySegmentTree = trees[char - 'a']

    companion object {
        val LETTERS_COUNT: Int = 26

        fun read(inputStream: InputStream): StringManipulation {
            val reader = Scanner(inputStream)

            val repetitionCount = reader.nextInt()
            reader.nextLine()
            val pattern = reader.nextLine()
            val queries = (1..reader.nextInt()).map { Query.read(reader) }

            return StringManipulation(pattern.repeat(repetitionCount), queries)
        }
    }

    data class Query(val index: Int, val character: Char) {
        companion object {
            fun read(reader: Scanner) = Query(reader.nextInt(), reader.nextLine()[1])
        }
    }
}

fun main(args: Array<String>) = println(StringManipulation.read(System.`in`).solve())


// package ru.spbau.mit

import java.io.InputStream
import java.util.*

interface Problem<out T> {
    fun solve(): T
}

data class Query(val index: Int, val character: Char) {
    companion object {
        fun read(reader: Scanner) = Query(reader.nextInt(), reader.nextLine()[1])
    }
}

class BinarySegmentTree(private val size: Int) {
    private val elements: Array<Int> = Array(4 * size) { 0 }

    fun set(index: Int, value: Boolean) = set(1, 0, size, index, value)

    fun get(index: Int) = get(1, 0, size, index)

    fun getKth(k: Int): Int = getKth(1, 0, size, k)

    private fun getKth(node: Int, left: Int, right: Int, k: Int): Int {
        if (left == right) {
            return left
        }

        val middle = left + (right - left) / 2

        return if (k <= elements[2 * node]) {
            getKth(2 * node, left, middle, k)
        } else {
            getKth(2 * node + 1, middle + 1, right, k - elements[2 * node])
        }
    }

    private fun set(node: Int, left: Int, right: Int, index: Int, value: Boolean): Unit {
        if (left == right) {
            elements[node] = if (value) 1 else 0
        } else {
            val middle = left + (right - left) / 2

            if (index <= middle) {
                set(2 * node, left, middle, index, value)
            } else {
                set(2 * node + 1, middle + 1, right, index, value)
            }

            elements[node] = elements[2 * node] + elements[2 * node + 1]
        }
    }

    private fun get(node: Int, left: Int, right: Int, index: Int): Boolean {
        if (left == right) {
            return elements[node] == 1
        }

        val middle = left + (right - left) / 2
        return if (index <= middle) get(2 * node, left, middle, index) else get(2 * node + 1, middle + 1, right, index)
    }
}

class StringManipulation(private val name: String,
                         private val queries: List<Query>) : Problem<String> {
    private val trees: Array<BinarySegmentTree> = Array(LETTERS_COUNT) {
        BinarySegmentTree(name.length)
    }

    private fun getTreeByCharacter(char: Char): BinarySegmentTree = trees[char - 'a']

    override fun solve(): String {
        name.forEachIndexed { index, character -> getTreeByCharacter(character).set(index, true) }

        queries.forEach {
            val tree = getTreeByCharacter(it.character)
            tree.set(tree.getKth(it.index), false)
        }

        return name.filterIndexed { index, character -> getTreeByCharacter(character).get(index) }
    }

    companion object {
        val LETTERS_COUNT: Int = 26

        fun read(inputStream: InputStream): StringManipulation {
            val reader = Scanner(inputStream)

            val repetitionCount = reader.nextInt()
            reader.nextLine()
            val pattern = reader.nextLine()
            val queries = (1..reader.nextInt()).map { Query.read(reader) }

            return StringManipulation(pattern.repeat(repetitionCount), queries)
        }
    }
}

fun main(args: Array<String>) = println(StringManipulation.read(System.`in`).solve())


package ru.spbau.mit

import java.io.InputStream
import java.util.*

class BinarySegmentTree(private val size: Int) {
    private val elements: Array<Int> = Array(4 * size) { 0 }

    fun set(index: Int, value: Boolean) = set(1, 0, size, index, value)

    fun get(index: Int) = get(1, 0, size, index)

    fun getKth(k: Int): Int = getKth(1, 0, size, k)

    private fun getKth(node: Int, left: Int, right: Int, k: Int): Int {
        if (left == right) {
            return left
        }

        val middle = getMiddle(left, right)

        return if (k <= elements[leftNode(node)]) {
            getKth(leftNode(node), left, middle, k)
        } else {
            getKth(rightNode(node), middle + 1, right, k - elements[leftNode(node)])
        }
    }

    private fun set(node: Int, left: Int, right: Int, index: Int, value: Boolean): Unit {
        if (left == right) {
            elements[node] = if (value) 1 else 0
        } else {
            val middle = getMiddle(left, right)

            if (index <= middle) {
                set(leftNode(node), left, middle, index, value)
            } else {
                set(rightNode(node), middle + 1, right, index, value)
            }

            elements[node] = elements[leftNode(node)] + elements[rightNode(node)]
        }
    }

    private fun get(node: Int, left: Int, right: Int, index: Int): Boolean {
        if (left == right) {
            return elements[node] == 1
        }

        val middle = getMiddle(left, right)

        return if (index <= middle) {
            get(leftNode(node), left, middle, index)
        } else {
            get(rightNode(node), middle + 1, right, index)
        }
    }

    private fun leftNode(node: Int) = 2 * node

    private fun rightNode(node: Int) = 2 * node + 1

    private fun getMiddle(left: Int, right: Int): Int = left + (right - left) / 2
}

interface Problem<out T> {
    fun solve(): T
}

class StringManipulation(private val name: String,
                         private val queries: List<Query>) : Problem<String> {
    private val trees: Array<BinarySegmentTree> = Array(LETTERS_COUNT) {
        BinarySegmentTree(name.length)
    }

    override fun solve(): String {
        name.forEachIndexed { index, character -> getTreeByCharacter(character).set(index, true) }

        queries.forEach {
            val tree = getTreeByCharacter(it.character)
            tree.set(tree.getKth(it.index), false)
        }

        return name.filterIndexed { index, character -> getTreeByCharacter(character).get(index) }
    }

    private fun getTreeByCharacter(char: Char): BinarySegmentTree = trees[char - 'a']

    companion object {
        val LETTERS_COUNT: Int = 26

        fun read(inputStream: InputStream): StringManipulation {
            val reader = Scanner(inputStream)

            val repetitionCount = reader.nextInt()
            reader.nextLine()
            val pattern = reader.nextLine()
            val queries = (1..reader.nextInt()).map { Query.read(reader) }

            return StringManipulation(pattern.repeat(repetitionCount), queries)
        }
    }

    data class Query(val index: Int, val character: Char) {
        companion object {
            fun read(reader: Scanner) = Query(reader.nextInt(), reader.nextLine()[1])
        }
    }
}

fun main(args: Array<String>) = println(StringManipulation.read(System.`in`).solve())


// package ru.spbau.mit

import java.io.InputStreamReader
import java.util.*

class BinarySegmentTree(private val size: Int) {
    private val elements: Array<Int> = Array(4 * size) { 0 }

    fun set(index: Int, value: Boolean) = set(1, 0, size, index, value)

    fun get(index: Int) = get(1, 0, size, index)

    fun getKth(k: Int): Int = getKth(1, 0, size, k)

    private fun getKth(node: Int, left: Int, right: Int, k: Int): Int {
        if (left == right) {
            return left
        }

        val middle = getMiddle(left, right)

        return if (k <= elements[leftNode(node)]) {
            getKth(leftNode(node), left, middle, k)
        } else {
            getKth(rightNode(node), middle + 1, right, k - elements[leftNode(node)])
        }
    }

    private fun set(node: Int, left: Int, right: Int, index: Int, value: Boolean): Unit {
        if (left == right) {
            elements[node] = if (value) 1 else 0
        } else {
            val middle = getMiddle(left, right)

            if (index <= middle) {
                set(leftNode(node), left, middle, index, value)
            } else {
                set(rightNode(node), middle + 1, right, index, value)
            }

            elements[node] = elements[leftNode(node)] + elements[rightNode(node)]
        }
    }

    private fun get(node: Int, left: Int, right: Int, index: Int): Boolean {
        if (left == right) {
            return elements[node] == 1
        }

        val middle = getMiddle(left, right)

        return if (index <= middle) {
            get(leftNode(node), left, middle, index)
        } else {
            get(rightNode(node), middle + 1, right, index)
        }
    }

    private fun leftNode(node: Int) = 2 * node

    private fun rightNode(node: Int) = 2 * node + 1

    private fun getMiddle(left: Int, right: Int): Int = left + (right - left) / 2
}

interface Problem<out T> {
    fun solve(): T
}

class StringManipulation(private val name: String,
                         private val queries: List<Query>) : Problem<String> {
    private val trees: Array<BinarySegmentTree> = Array(LETTERS_COUNT) {
        BinarySegmentTree(name.length)
    }

    override fun solve(): String {
        name.forEachIndexed { index, character -> getTreeByCharacter(character).set(index, true) }

        queries.forEach {
            val tree = getTreeByCharacter(it.character)
            tree.set(tree.getKth(it.index), false)
        }

        return name.filterIndexed { index, character -> getTreeByCharacter(character).get(index) }
    }

    private fun getTreeByCharacter(char: Char): BinarySegmentTree = trees[char - 'a']

    companion object {
        val LETTERS_COUNT: Int = 26

        fun read(scanner: Scanner): StringManipulation {
            val repetitionCount = scanner.nextInt()
            scanner.nextLine()
            val pattern = scanner.nextLine()
            val queries = (1..scanner.nextInt()).map { Query.read(scanner) }

            return StringManipulation(pattern.repeat(repetitionCount), queries)
        }
    }

    data class Query(val index: Int, val character: Char) {
        companion object {
            fun read(scanner: Scanner) = Query(scanner.nextInt(), scanner.nextLine()[1])
        }
    }
}

fun main(args: Array<String>) = println(StringManipulation.read(Scanner(InputStreamReader(System.`in`))).solve())


// package ru.spbau.mit

import java.io.InputStream
import java.util.*

interface Problem<out T> {
    fun solve(): T
}

data class Query(val index: Int, val character: Char) {
    companion object {
        fun read(reader: Scanner) = Query(reader.nextInt(), reader.nextLine()[1])
    }
}

class StringManipulation(private val name: String,
                         private val queries: List<Query>) : Problem<String> {

    override fun solve(): String {
        val data = queries.groupByTo(mutableMapOf(), Query::character)

        return name.filter {
            if (data.getOrDefault(it, mutableListOf()).isEmpty()) {
                return@filter true
            }

            val query = data[it]!!.removeAt(0)

            if (query.index == 1) {
                return@filter false
            }

            data[it]?.add(0, Query(query.index - 1, query.character))
            return@filter true
        }
    }

    companion object {
        fun read(inputStream: InputStream): StringManipulation {
            val reader = Scanner(inputStream)

            val repetitionCount = reader.nextInt()
            reader.nextLine()
            val pattern = reader.nextLine()
            val queries = (1..reader.nextInt()).map { Query.read(reader) }

            return StringManipulation(pattern.repeat(repetitionCount), queries)
        }
    }
}

fun main(args: Array<String>) = println(StringManipulation.read(System.`in`).solve())


// package ru.spbau.mit

import java.io.InputStream
import java.util.*

interface Problem<out T> {
    fun solve(): T
}

data class Query(val index: Int, val character: Char) {
    companion object {
        fun read(reader: Scanner) = Query(reader.nextInt(), reader.nextLine()[1])
    }
}

class StringManipulation(private val name: String,
                         private val queries: List<Query>) : Problem<String> {

    override fun solve(): String {
        val data = queries.groupByTo(mutableMapOf(), Query::character)

        return name.filter {
            if (data.getOrDefault(it, mutableListOf()).isEmpty()) {
                return@filter true
            }

            val query = data[it]!!.removeAt(0)

            if (query.index == 1) {
                return@filter false
            }

            data[it]?.add(0, Query(query.index - 1, query.character))
            return@filter true
        }
    }

    companion object {
        fun read(inputStream: InputStream): StringManipulation {
            val reader = Scanner(inputStream)

            val repetitionCount = reader.nextInt()
            val pattern = reader.skip("\n").nextLine()
            val queries = (1..reader.nextInt()).map { Query.read(reader) }

            return StringManipulation(pattern.repeat(repetitionCount), queries)
        }
    }
}

fun main(args: Array<String>) = println(StringManipulation.read(System.`in`).solve())


// package ru.spbau.mit

import java.io.InputStream
import java.util.*
import javax.xml.stream.events.Characters
import kotlin.collections.HashSet

interface Problem<out T> {
    fun solve(): T
}

data class Query(val index: Int, val character: Char) {
    companion object {
        fun read(reader: Scanner) = Query(reader.nextInt(), reader.nextLine()[1])
    }
}

class BinarySegmentTree(private val size: Int) {
    private val root: Node = init(0, size)

    fun set(index: Int, value: Boolean) = set(root, 0, size, index, value)

    fun get(index: Int) = get(root, 0, size, index)

    fun getKth(k: Int): Int = getKth(root, 0, size, k)

    private fun getKth(node: Node, left: Int, right: Int, k: Int): Int = when (node) {
        is Node.Leaf -> left
        is Node.Inner -> {
            val middle = left + (right - left) / 2

            if (k <= node.left.value) {
                getKth(node.left, left, middle, k)
            } else {
                getKth(node.right, middle + 1, right, k - node.left.value)
            }
        }
    }

    private fun set(node: Node, left: Int, right: Int, index: Int, value: Boolean): Unit = when (node) {
        is Node.Leaf -> node.value = if (value) 1 else 0
        is Node.Inner -> {
            val middle = left + (right - left) / 2

            if (index <= middle) {
                set(node.left, left, middle, index, value)
            } else {
                set(node.right, middle + 1, right, index, value)
            }

            node.update()
        }
    }

    private fun get(node: Node, left: Int, right: Int, index: Int): Boolean = when (node) {
        is Node.Leaf -> node.value == 1
        is Node.Inner -> {
            val middle = left + (right - left) / 2
            if (index <= middle) get(node.left, left, middle, index) else get(node.right, middle + 1, right, index)
        }
    }

    private fun init(left: Int, right: Int): Node {
        if (left == right) {
            return Node.Leaf()
        }

        val middle = left + (right - left) / 2
        return Node.Inner(init(left, middle), init(middle + 1, right))
    }

    private sealed class Node(var value: Int = 0) {
        class Leaf : Node()

        data class Inner(val left: Node, val right: Node) : Node() {
            fun update() {
                value = left.value + right.value
            }
        }
    }
}

class StringManipulation(private val name: String,
                         private val queries: List<Query>) : Problem<String> {
    private val trees: List<BinarySegmentTree> = MutableList(LETTERS_COUNT) {
        BinarySegmentTree(name.length)
    }

    private fun getTreeByCharacter(char: Char): BinarySegmentTree = trees[char - 'a']

    override fun solve(): String {
        name.forEachIndexed { index, character -> getTreeByCharacter(character).set(index, true) }

        queries.forEach {
            val tree = getTreeByCharacter(it.character)
            tree.set(tree.getKth(it.index), false)
        }

        return name.filterIndexed { index, character -> getTreeByCharacter(character).get(index) }
    }

    companion object {
        val LETTERS_COUNT: Int = 26

        fun read(inputStream: InputStream): StringManipulation {
            val reader = Scanner(inputStream)

            val repetitionCount = reader.nextInt()
            reader.nextLine()
            val pattern = reader.nextLine()
            val queries = (1..reader.nextInt()).map { Query.read(reader) }

            return StringManipulation(pattern.repeat(repetitionCount), queries)
        }
    }
}

fun main(args: Array<String>) = println(StringManipulation.read(System.`in`).solve())


// package ru.spbau.mit

import java.io.InputStreamReader
import java.util.*

class BinarySegmentTree(private val size: Int) {
    private val elements: IntArray = IntArray(4 * size) { 0 }

    fun set(index: Int, value: Boolean) = set(1, 0, size, index, value)

    fun get(index: Int) = get(1, 0, size, index)

    fun getKth(k: Int): Int = getKth(1, 0, size, k)

    private tailrec fun getKth(node: Int, left: Int, right: Int, k: Int): Int {
        if (left == right) {
            return left
        }

        val middle = getMiddle(left, right)

        return if (k <= elements[getLeftNodeIndex(node)]) {
            getKth(getLeftNodeIndex(node), left, middle, k)
        } else {
            getKth(getRightNodeIndex(node), middle + 1, right, k - elements[getLeftNodeIndex(node)])
        }
    }

    private fun set(node: Int, left: Int, right: Int, index: Int, value: Boolean): Unit {
        if (left == right) {
            elements[node] = if (value) 1 else 0
        } else {
            val middle = getMiddle(left, right)

            if (index <= middle) {
                set(getLeftNodeIndex(node), left, middle, index, value)
            } else {
                set(getRightNodeIndex(node), middle + 1, right, index, value)
            }

            elements[node] = elements[getLeftNodeIndex(node)] + elements[getRightNodeIndex(node)]
        }
    }

    private fun get(node: Int, left: Int, right: Int, index: Int): Boolean {
        if (left == right) {
            return elements[node] == 1
        }

        val middle = getMiddle(left, right)

        return if (index <= middle) {
            get(getLeftNodeIndex(node), left, middle, index)
        } else {
            get(getRightNodeIndex(node), middle + 1, right, index)
        }
    }

    private fun getLeftNodeIndex(node: Int) = 2 * node

    private fun getRightNodeIndex(node: Int) = 2 * node + 1

    private fun getMiddle(left: Int, right: Int): Int = left + (right - left) / 2
}

class StringManipulation(private val name: String,
                         private val queries: List<Query>) {
    private val trees: Array<BinarySegmentTree> = Array(LETTERS_COUNT) {
        BinarySegmentTree(name.length)
    }

    fun solve(): String {
        name.forEachIndexed { index, character -> getTreeByCharacter(character).set(index, true) }

        queries.forEach {
            val tree = getTreeByCharacter(it.character)
            tree.set(tree.getKth(it.index), false)
        }

        return name.filterIndexed { index, character -> getTreeByCharacter(character).get(index) }
    }

    private fun getTreeByCharacter(char: Char): BinarySegmentTree = trees[char - 'a']

    companion object {
        val LETTERS_COUNT: Int = 26

        fun read(scanner: Scanner): StringManipulation {
            val repetitionCount = scanner.nextInt()
            scanner.nextLine()
            val pattern = scanner.nextLine()
            val queries = (1..scanner.nextInt()).map { Query.read(scanner) }

            return StringManipulation(pattern.repeat(repetitionCount), queries)
        }
    }

    data class Query(val index: Int, val character: Char) {
        companion object {
            inline fun read(scanner: Scanner) = Query(scanner.nextInt(), scanner.nextLine()[1])
        }
    }
}

fun main(args: Array<String>) = println(StringManipulation.read(Scanner(InputStreamReader(System.`in`))).solve())


// package ru.spbau.mit

import java.io.InputStream
import java.util.*
import javax.xml.stream.events.Characters
import kotlin.collections.HashSet

interface Problem<out T> {
    fun solve(): T
}

data class Query(val index: Int, val character: Char) {
    companion object {
        fun read(reader: Scanner) = Query(reader.nextInt(), reader.nextLine()[1])
    }
}

class BinarySegmentTree(private val size: Int) {
    private val root: Node = Node()

    fun set(index: Int, value: Boolean) = set(root, 0, size, index, value)

    fun get(index: Int) = get(root, 0, size, index)

    fun getKth(k: Int): Int = getKth(root, 0, size, k)

    private fun getKth(node: Node, left: Int, right: Int, k: Int): Int {
        if (left == right) {
            return left
        }

        val middle = left + (right - left) / 2

        return if (k <= node.left.value) {
            getKth(node.left, left, middle, k)
        } else {
            getKth(node.right, middle + 1, right, k - node.left.value)
        }
    }

    private fun set(node: Node, left: Int, right: Int, index: Int, value: Boolean): Unit {
        if (left == right) {
            node.value = if (value) 1 else 0
        } else {
            val middle = left + (right - left) / 2

            if (index <= middle) {
                set(node.left, left, middle, index, value)
            } else {
                set(node.right, middle + 1, right, index, value)
            }

            node.update()
        }
    }

    private fun get(node: Node, left: Int, right: Int, index: Int): Boolean {
        if (left == right) {
            return node.value == 1
        }

        val middle = left + (right - left) / 2
        return if (index <= middle) get(node.left, left, middle, index) else get(node.right, middle + 1, right, index)
    }

    private class Node {
        var value: Int = 0

        val left by lazy {
            Node()
        }

        val right by lazy {
            Node()
        }

        fun update() {
            value = left.value + right.value
        }
    }
}

class StringManipulation(private val name: String,
                         private val queries: List<Query>) : Problem<String> {
    private val trees: List<BinarySegmentTree> = MutableList(LETTERS_COUNT) {
        BinarySegmentTree(name.length)
    }

    private fun getTreeByCharacter(char: Char): BinarySegmentTree = trees[char - 'a']

    override fun solve(): String {
        name.forEachIndexed { index, character -> getTreeByCharacter(character).set(index, true) }

        queries.forEach {
            val tree = getTreeByCharacter(it.character)
            tree.set(tree.getKth(it.index), false)
        }

        return name.filterIndexed { index, character -> getTreeByCharacter(character).get(index) }
    }

    companion object {
        val LETTERS_COUNT: Int = 26

        fun read(inputStream: InputStream): StringManipulation {
            val reader = Scanner(inputStream)

            val repetitionCount = reader.nextInt()
            reader.nextLine()
            val pattern = reader.nextLine()
            val queries = (1..reader.nextInt()).map { Query.read(reader) }

            return StringManipulation(pattern.repeat(repetitionCount), queries)
        }
    }
}

fun main(args: Array<String>) = println(StringManipulation.read(System.`in`).solve())