Language: C

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

typedef struct{
	int num;
	int *cups;
}HANOI;

int n,m;
int min;

void HanoiMove(HANOI *Hanoi, HANOI *prev, int count){
	int i,j,k;
	int check, count_left, count_right;
	HANOI *Move_left, *Move_right;

	Move_left=(HANOI *)calloc(4,sizeof(HANOI));
	for(i=0;i<4;i++){
		Move_left[i].cups=(int *)calloc(n,sizeof(int));
	}
	Move_right=(HANOI *)calloc(4,sizeof(HANOI));
	for(i=0;i<4;i++){
		Move_right[i].cups=(int *)calloc(n,sizeof(int));
	}

	for(i=1;i<4;i++){
		if(Hanoi[i].num==n && count<min){
			min = count;
		}
	}
	count_left=count_right=count;

	for(i=1;i<4;i++){
		if(Hanoi[i].num==0) continue;
		if(i>=2){
			for(j=1;j<4;j++){
				Move_left[j].num = Hanoi[j].num;
				for(k=1;k<=Hanoi[j].num;k++){
					Move_left[j].cups[k] = Hanoi[j].cups[k];
				}
			}
			if(prev==NULL){
				prev=(HANOI *)calloc(4,sizeof(HANOI));
				for(j=0;j<4;j++){
					prev[j].cups=(int *)calloc(n,sizeof(int));
				}
			}
			if(Move_left[i].cups[Move_left[i].num]>Move_left[i-1].cups[Move_left[i-1].num]){
				Move_left[i-1].num++;
				Move_left[i-1].cups[Move_left[i-1].num]=Move_left[i].cups[Move_left[i].num];
				Move_left[i].cups[Move_left[i].num]=0;
				Move_left[i].num--;
				count_left++;

				check=0;
				for(j=1;j<4;j++){
					if(prev!=NULL && Move_left[j].num==prev[j].num){
						check+=1;
					}
				}
				if(prev==NULL || check<3){
					for(j=1;j<4;j++){
						prev[j].num = Hanoi[j].num;
						for(k=1;k<=Hanoi[j].num;k++){
							prev[j].cups[k] = Hanoi[j].cups[k];
						}
					}
					for(j=1;j<4;j++){
						Hanoi[j].num = Move_left[j].num;
						for(k=1;k<=Move_left[j].num;k++){
							Hanoi[j].cups[k] = Move_left[j].cups[k];
						}
					}
					HanoiMove(Hanoi, prev, count_left);
				}
			}
		}
		if(i<=2){
			for(j=1;j<4;j++){
				Move_right[j].num = Hanoi[j].num;
				for(k=1;k<=Hanoi[j].num;k++){
					Move_right[j].cups[k] = Hanoi[j].cups[k];
				}
			}
			if(prev==NULL){
				prev=(HANOI *)calloc(4,sizeof(HANOI));
				for(j=0;j<4;j++){
					prev[j].cups=(int *)calloc(n,sizeof(int));
				}
			}
			if(Move_right[i].cups[Move_right[i].num]>Move_right[i+1].cups[Move_right[i+1].num]){
				Move_right[i+1].num++;
				Move_right[i+1].cups[Move_right[i+1].num]=Move_right[i].cups[Move_right[i].num];
				Move_right[i].cups[Move_right[i].num]=0;
				Move_right[i].num--;
				count_right++;

				check=0;
				for(j=1;j<4;j++){
					if(prev!=NULL && Move_right[j].num==prev[j].num){
						check+=1;
					}
				}
				if(prev==NULL || check<3){
					for(j=1;j<4;j++){
						prev[j].num = Hanoi[j].num;
						for(k=1;k<=Hanoi[j].num;k++){
							prev[j].cups[k] = Hanoi[j].cups[k];
						}
					}
					for(j=1;j<4;j++){
						Hanoi[j].num = Move_right[j].num;
						for(k=1;k<=Move_right[j].num;k++){
							Hanoi[j].cups[k] = Move_right[j].cups[k];
						}
					}
					HanoiMove(Hanoi, prev, count_right);
				}
			}
		}
	}

	return;
}

int main(void){
	int i,j;
	HANOI *Hanoi, *prev;

	Hanoi=(HANOI *)calloc(4,sizeof(HANOI));

	while(scanf("%d %d",&n,&m)!=EOF){
		if(n==0 && m==0) break;
		min = INT_MAX;
		for(i=0;i<4;i++) Hanoi[i].cups=(int *)calloc(n,sizeof(int));
		for(i=1;i<4;i++){
			scanf("%d",&Hanoi[i].num);
			for(j=1;j<=Hanoi[i].num;j++){
				scanf("%d",&Hanoi[i].cups[j]);
			}
		}
		prev = NULL;
		HanoiMove(Hanoi,prev,0);
		if(min>m) printf("-1\n");
		else printf("%d\n",min);
		
	}
	return 0;
}

Compiler output:
main.c: In function 'main':
main.c:135:23: error: 'INT_MAX' undeclared (first use in this function)
  135 |                 min = INT_MAX;
      |                       ^~~~~~~
main.c:4:1: note: 'INT_MAX' is defined in header '<limits.h>'; this is probably fixable by adding '#include <limits.h>'
    3 | #include<string.h>
  +++ |+#include <limits.h>
    4 | 
main.c:135:23: note: each undeclared identifier is reported only once for each function it appears in
  135 |                 min = INT_MAX;
      |                       ^~~~~~~