Language: C++

#include <iostream>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <functional>
#define INPUT_FROM_FILE (1)

#if INPUT_FROM_FILE
#include <fstream>
#endif

const char Max_n = 15;

int f_A(std::deque<char> stack_A, std::deque<char> stack_B, std::deque<char> stack_C);
int f_B(std::deque<char> stack_A, std::deque<char> stack_B, std::deque<char> stack_C);
int f_C(std::deque<char> stack_A, std::deque<char> stack_B, std::deque<char> stack_C);

int f_A(std::deque<char> stack_A, std::deque<char> stack_B, std::deque<char> stack_C){
	if(stack_B.empty() && stack_C.empty()){
		return 0;
	}
	char min_stack_A = !stack_A.empty() ? stack_A.front() : Max_n + 1;
	char min_stack_B = !stack_B.empty() ? stack_B.front() : Max_n + 1;
	char min_stack_C = !stack_C.empty() ? stack_C.front() : Max_n + 1;

	if(min_stack_A < min_stack_B && min_stack_A < min_stack_C){
		stack_A.pop_front();
		return f_A(stack_A, stack_B, stack_C);
	}
	if(min_stack_B < min_stack_A && min_stack_B < min_stack_C){
		int n = 0;

		stack_B.pop_front();
		n = stack_A.size() + stack_B.size() + stack_C.size();
		return f_C(stack_A, stack_B, stack_C) + int(pow(3, n));
	}
	if(min_stack_C < min_stack_A && min_stack_C < min_stack_B){
		int n = 0;
		
		stack_C.pop_front();
		n = stack_A.size() + stack_B.size() + stack_C.size();
		return f_A(stack_A, stack_B, stack_C) + 2 * int(pow(3, n));
	}

	std::cout << "error f_A" << std::endl;
	exit(0);
	return 0;
}

int f_B(std::deque<char> stack_A, std::deque<char> stack_B, std::deque<char> stack_C){
	if(stack_A.empty() && stack_C.empty()){
		return 0;
	}
	char min_stack_A = !stack_A.empty() ? stack_A.front() : Max_n + 1;
	char min_stack_B = !stack_B.empty() ? stack_B.front() : Max_n + 1;
	char min_stack_C = !stack_C.empty() ? stack_C.front() : Max_n + 1;

	if(min_stack_A < min_stack_B && min_stack_A < min_stack_C){
		int n = 0;
		
		stack_A.pop_front();
		n = stack_A.size() + stack_B.size() + stack_C.size();
		return f_C(stack_A, stack_B, stack_C) + (int(pow(3, n)) + 1) / 2;
	}
	if(min_stack_B < min_stack_A && min_stack_B < min_stack_C){
		stack_B.pop_front();
		return f_B(stack_A, stack_B, stack_C);
	}
	if(min_stack_C < min_stack_A && min_stack_C < min_stack_B){
		int n = 0;
		
		stack_C.pop_front();
		n = stack_A.size() + stack_B.size() + stack_C.size();
		return f_A(stack_A, stack_B, stack_C) + (int(pow(3, n)) + 1) / 2;
	}

	std::cout << "error f_B" << std::endl;
	exit(0);
	return 0;
}

int f_C(std::deque<char> stack_A, std::deque<char> stack_B, std::deque<char> stack_C){
	if(stack_A.empty() && stack_B.empty()){
		return 0;
	}
	char min_stack_A = !stack_A.empty() ? stack_A.front() : Max_n + 1;
	char min_stack_B = !stack_B.empty() ? stack_B.front() : Max_n + 1;
	char min_stack_C = !stack_C.empty() ? stack_C.front() : Max_n + 1;

	if(min_stack_A < min_stack_B && min_stack_A < min_stack_C){
		int n = 0;

		stack_A.pop_front();
		n = stack_A.size() + stack_B.size() + stack_C.size();
		return f_C(stack_A, stack_B, stack_C) + 2 * int(pow(3, n));
	}
	if(min_stack_B < min_stack_A && min_stack_B < min_stack_C){
		int n = 0;

		stack_B.pop_front();
		n = stack_A.size() + stack_B.size() + stack_C.size();
		return f_A(stack_A, stack_B, stack_C) + int(pow(3, n));
	}
	if(min_stack_C < min_stack_A && min_stack_C < min_stack_B){
		stack_C.pop_front();
		return f_C(stack_A, stack_B, stack_C);
	}

	std::cout << "error f_C" << std::endl;
	exit(0);
	return 0;
}

int main(int argc, char **argv){
#if INPUT_FROM_FILE
	std::ifstream ifs("test.txt");
#endif
	int n = 0;
	int m = 0;

#if INPUT_FROM_FILE
	while(ifs >> n >> m, (n != 0 || m != 0)){
#else
	while(std::cin >> n >> m, (n != 0 || m != 0)){
#endif
		std::deque<char> tray[3];

		for(int i = 0; i < 3; i++){
			int num_cups = 0;

#if INPUT_FROM_FILE
			ifs >> num_cups;
#else
			std::cin >> num_cups;
#endif
			for(int j = 0; j < num_cups; j++){
				char c = 0;

#if INPUT_FROM_FILE
				ifs >> c;
#else
				std::cin >> c;
#endif
				tray[i].push_back(c - '0');
			}
		}
		//??¢?´¢
		{
			int num_move =std::min(f_A(tray[0], tray[1], tray[2]), f_C(tray[0], tray[1], tray[2]));
			
			if(num_move <= m){
				std::cout << num_move << std::endl;
			}
			else{
				std::cout << -1 << std::endl;
			}
		}
	}
	return 0;
}

Compiler output:
a.cc: In function 'int f_A(std::deque<char>, std::deque<char>, std::deque<char>)':
a.cc:36:61: error: 'pow' was not declared in this scope
   36 |                 return f_C(stack_A, stack_B, stack_C) + int(pow(3, n));
      |                                                             ^~~
a.cc:43:65: error: 'pow' was not declared in this scope
   43 |                 return f_A(stack_A, stack_B, stack_C) + 2 * int(pow(3, n));
      |                                                                 ^~~
a.cc: In function 'int f_B(std::deque<char>, std::deque<char>, std::deque<char>)':
a.cc:64:62: error: 'pow' was not declared in this scope
   64 |                 return f_C(stack_A, stack_B, stack_C) + (int(pow(3, n)) + 1) / 2;
      |                                                              ^~~
a.cc:75:62: error: 'pow' was not declared in this scope
   75 |                 return f_A(stack_A, stack_B, stack_C) + (int(pow(3, n)) + 1) / 2;
      |                                                              ^~~
a.cc: In function 'int f_C(std::deque<char>, std::deque<char>, std::deque<char>)':
a.cc:96:65: error: 'pow' was not declared in this scope
   96 |                 return f_C(stack_A, stack_B, stack_C) + 2 * int(pow(3, n));
      |                                                                 ^~~
a.cc:103:61: error: 'pow' was not declared in this scope
  103 |                 return f_A(stack_A, stack_B, stack_C) + int(pow(3, n));
      |                                                             ^~~