#include <string>
#include <vector>
#include <stack>
using namespace std;
int GetADoll(vector<vector<int>>& board, int number) {
int boardSize = board.size();
int returnValue = 0;
for(int height = 0; height < boardSize; height++) {
if(board.at(height).at(number-1) > 0) {
returnValue = board.at(height).at(number-1);
board.at(height).at(number-1) = 0;
break;
}
}
return returnValue;
}
int solution(vector<vector<int>> board, vector<int> moves) {
int answer = 0;
stack<int> s;
for(auto move : moves) {
int doll = GetADoll(board, move);
// 뽑을 인형이 있으면
if(doll) {
if(s.empty() || s.top() != doll) {
s.push(doll);
}
else if (s.top() == doll) {
s.pop();
answer+=2;
}
}
}
return answer;
}