Language: C++

#define _USE_MATH_DEFINES
#define INF 10000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <hash_map>

using namespace std;

typedef long long ll;
typedef pair <int,vector<int> > P;

static const double eps = 1e-8;

int bfs(int m,vector<int>& root, hash_map<string,int> visited){

	priority_queue<P,vector<P>,greater<P> > que;
	que.push(P(0,root));
	
	vector<int> tmp;
	vector<int> sets;
	while(!que.empty()){
		int cost = que.top().first;
		
		sets = que.top().second;

		if(sets[1]==0 && sets[2]==0) return cost;
		if(sets[0]==0 && sets[1]==0) return cost;

		if(cost >= m) break;
		que.pop();
		for(int i=0;i+1<sets.size();i++){
			if((sets[i] && sets[i+1] && sets[i] < sets[i+1]) 
				|| (!sets[i] && sets[i+1])){
				tmp = sets;

				int pos=0;
				for(int j=15;j>=0;j--){
					if(tmp[i+1] & (1<<j)){
						pos = j;
						break;
					}
				}
				tmp[i+1] &= ~(1<<pos);
				tmp[i] |= (1<<pos);

				
				char buf[32];
				sprintf(buf,"%09d%09d%09d",tmp[0],tmp[1],tmp[2]);
				string next;
				next = buf;
				if(visited.find(next) == visited.end()){
					visited[next] = cost+1;
					que.push(P(cost+1,tmp));
				}
			}

			else if((sets[i] && sets[i+1] && sets[i] > sets[i+1]) 
				|| (sets[i] && !sets[i+1])){
				tmp = sets;

				int pos=0;
				for(int j=15;j>=0;j--){
					if(tmp[i] & (1<<j)){
						pos = j;
						break;
					}
				}
				tmp[i] &= ~(1<<pos);
				tmp[i+1] |= (1<<pos);

				char buf[32];
				sprintf(buf,"%09d%09d%09d",tmp[0],tmp[1],tmp[2]);
				string next;
				next = buf;
				if(visited.find(next) == visited.end()){
					visited[next] = cost+1;
					que.push(P(cost+1,tmp));
				}
			}
		}
	}
	return -1;
}

int main(){

	int n,m;
	while(~scanf("%d %d",&n,&m)){
		if(n==m && m==0) break;
		vector<int> sets(3);
		hash_map<string,int> visited;
		for(int i=0;i<3;i++){
			int amount;
			scanf("%d",&amount);
			for(int j=0;j<amount;j++){
				int size;
				scanf("%d",&size);
				sets[i] |= (1<<(size-1)); 
			}
		}

		printf("%d\n",bfs(m,sets,visited));
	}
}

Compiler output:
In file included from /usr/include/c++/14/backward/hash_map:60,
                 from a.cc:17:
/usr/include/c++/14/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. [-Wcpp]
   32 | #warning \
      |  ^~~~~~~
a.cc:26:34: error: 'hash_map' has not been declared
   26 | int bfs(int m,vector<int>& root, hash_map<string,int> visited){
      |                                  ^~~~~~~~
a.cc:26:42: error: expected ',' or '...' before '<' token
   26 | int bfs(int m,vector<int>& root, hash_map<string,int> visited){
      |                                          ^
a.cc: In function 'int bfs(int, std::vector<int>&, int)':
a.cc:63:36: error: 'visited' was not declared in this scope
   63 |                                 if(visited.find(next) == visited.end()){
      |                                    ^~~~~~~
a.cc:87:36: error: 'visited' was not declared in this scope
   87 |                                 if(visited.find(next) == visited.end()){
      |                                    ^~~~~~~
a.cc: In function 'int main()':
a.cc:103:17: error: 'hash_map' was not declared in this scope; did you mean '__gnu_cxx::hash_map'?
  103 |                 hash_map<string,int> visited;
      |                 ^~~~~~~~
      |                 __gnu_cxx::hash_map
/usr/include/c++/14/backward/hash_map:83:11: note: '__gnu_cxx::hash_map' declared here
   83 |     class hash_map
      |           ^~~~~~~~
a.cc:103:32: error: expected primary-expression before ',' token
  103 |                 hash_map<string,int> visited;
      |                                ^
a.cc:103:33: error: expected primary-expression before 'int'
  103 |                 hash_map<string,int> visited;
      |                                 ^~~
a.cc:114:42: error: 'visited' was not declared in this scope
  114 |                 printf("%d\n",bfs(m,sets,visited));
      |                                          ^~~~~~~