Language: C

m,i,j,k,t[16],c[16],p,d;
int main(n){
	for(*t=1;++i<16;)t[i]=n*=3;
	for(;scanf("%d%d",&n,&m),n;printf("%d\n",j>m?-1:j=fmin(j,t[n]+~j))){
		for(k=3;k--;)
			for(scanf("%d",&i);i--;c[j]=k)scanf("%d",&j);
		for(p=j=k=0;k++<n;)
			j+=t[n-k]*abs(i=p-c[k]),p=i%2*2+p&3;
	}
	return 0;
}

Compiler output:
main.c:1:1: warning: data definition has no type or storage class
    1 | m,i,j,k,t[16],c[16],p,d;
      | ^
main.c:1:1: error: type defaults to 'int' in declaration of 'm' [-Wimplicit-int]
main.c:1:3: error: type defaults to 'int' in declaration of 'i' [-Wimplicit-int]
    1 | m,i,j,k,t[16],c[16],p,d;
      |   ^
main.c:1:5: error: type defaults to 'int' in declaration of 'j' [-Wimplicit-int]
    1 | m,i,j,k,t[16],c[16],p,d;
      |     ^
main.c:1:7: error: type defaults to 'int' in declaration of 'k' [-Wimplicit-int]
    1 | m,i,j,k,t[16],c[16],p,d;
      |       ^
main.c:1:9: error: type defaults to 'int' in declaration of 't' [-Wimplicit-int]
    1 | m,i,j,k,t[16],c[16],p,d;
      |         ^
main.c:1:15: error: type defaults to 'int' in declaration of 'c' [-Wimplicit-int]
    1 | m,i,j,k,t[16],c[16],p,d;
      |               ^
main.c:1:21: error: type defaults to 'int' in declaration of 'p' [-Wimplicit-int]
    1 | m,i,j,k,t[16],c[16],p,d;
      |                     ^
main.c:1:23: error: type defaults to 'int' in declaration of 'd' [-Wimplicit-int]
    1 | m,i,j,k,t[16],c[16],p,d;
      |                       ^
main.c: In function 'main':
main.c:2:5: error: type of 'n' defaults to 'int' [-Wimplicit-int]
    2 | int main(n){
      |     ^~~~
main.c:4:14: error: implicit declaration of function 'scanf' [-Wimplicit-function-declaration]
    4 |         for(;scanf("%d%d",&n,&m),n;printf("%d\n",j>m?-1:j=fmin(j,t[n]+~j))){
      |              ^~~~~
main.c:1:1: note: include '<stdio.h>' or provide a declaration of 'scanf'
  +++ |+#include <stdio.h>
    1 | m,i,j,k,t[16],c[16],p,d;
main.c:4:14: warning: incompatible implicit declaration of built-in function 'scanf' [-Wbuiltin-declaration-mismatch]
    4 |         for(;scanf("%d%d",&n,&m),n;printf("%d\n",j>m?-1:j=fmin(j,t[n]+~j))){
      |              ^~~~~
main.c:4:14: note: include '<stdio.h>' or provide a declaration of 'scanf'
main.c:4:36: error: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
    4 |         for(;scanf("%d%d",&n,&m),n;printf("%d\n",j>m?-1:j=fmin(j,t[n]+~j))){
      |                                    ^~~~~~
main.c:4:36: note: include '<stdio.h>' or provide a declaration of 'printf'
main.c:4:36: warning: incompatible implicit declaration of built-in function 'printf' [-Wbuiltin-declaration-mismatch]
main.c:4:36: note: include '<stdio.h>' or provide a declaration of 'printf'
main.c:4:59: error: implicit declaration of function 'fmin' [-Wimplicit-function-declaration]
    4 |         for(;scanf("%d%d",&n,&m),n;printf("%d\n",j>m?-1:j=fmin(j,t[n]+~j))){
      |                                                           ^~~~
main.c:1:1: note: include '<math.h>' or provide a declaration of 'fmin'
  +++ |+#include <math.h>
    1 | m,i,j,k,t[16],c[16],p,d;
main.c:4:59: warning: incompatible implicit declaration of built-in function 'fmin' [-Wbuiltin-declaration-mismatch]
    4 |         for(;scanf("%d%d",&n,&m),n;printf("%d\n",j>m?-1:j=fmin(j,t[n]+~j))){
      |                                                           ^~~~
main.c:4:59: note: include '<math.h>' or provide a declaration of 'fmin'
main.c:4:58: error: lvalue required as left operand of assignment
    4 |         for(;scanf("%d%d",&n,&m),n;printf("%d\n",j>m?-1:j=fmin(j,t[n]+~j))){
      |                                                          ^
main.c:8:35: error: implicit declaration of function 'abs' [-Wimplicit-function-declaration]
    8 |                         j+=t[n-k]*abs(i=p-c[k]),p=i%2*2+p&3;
      |                                   ^~~
main.c:1:1: note: include '<stdlib.h>' or provide a declaration of 'abs'
  +++ |+#include <stdlib.h>
    1 | m,i,j,k,t[16],c[16],p,d;