#include <iostream>
#include <vector>

void moveBall(std::vector<int> &tubes, std::vector<int> &balls, int from, int to)
{
    int loc = 2;
    while(tubes[3*from+loc] == 0)
        loc--;
    int tol = 0;
    while(tubes[3*to+tol] != 0)
        tol++;
    int b0, b1;
    for(int i=0; i<3; i++)
    {
        if(balls[3*tubes[3*from+loc] + i] == 3*from+loc)
            b0 = i;
        if(balls[3*tubes[3*to+tol] + i] == 3*to+tol)
            b1 = i;
    }       
    std::swap(balls[3*tubes[3*from+loc] + b0], balls[3*tubes[3*to+tol] + b1]);
    std::swap(tubes[3*from+loc], tubes[3*to+tol]);
}

int main()
{
    int n;
    std::cin >> n;
    std::vector<int> tubes(3*(n+1));
    std::vector<int> balls(3*(n+1));
    std::vector<int> ballcnt(n+1);
    for(int i=0; i<n+1; i++)
    {
        int b[3];
        std::cin >> b[0] >> b[1] >> b[2];
        for(int j=0; j<3; j++)
        {
            tubes[3*i+j] = b[j];
            balls[3*b[j] + ballcnt[b[j]]++] = 3*i+j;
        }     
    }
    
    std::vector<std::pair<int, int> > moves;
    
    for(int i=0; i<n; i++)
    {
        while(tubes[3*i+2] == 0)
        {
            moveBall(tubes, balls, n, i);
            moves.push_back({n+1,i+1});
        }
    }
    
    for(int i=0; i<n; i++)
    {
        int color = tubes[3*i];
        if(tubes[3*i + 2] != color)
        {
            moveBall(tubes, balls, i, n);
            moves.push_back({i+1, n+1});
            
            for(int j=0; j<3; j++)
            {
                if(balls[3*color + j] / 3 != i)
                {                    
                    int fromcol = balls[3*color+j] / 3;
                    for(int k=0; k<3; k++)
                    {
                        if(tubes[3*fromcol + 2 - k] != color)
                        {
                            moveBall(tubes, balls, fromcol, n);
                            moves.push_back({fromcol+1, n+1});
                        }
                        else
                        {
                            moveBall(tubes, balls, fromcol, i);
                            moves.push_back({fromcol+1, i+1});
                            break;
                        }
                    }
                    for(int j=0; j<3; j++)
                    {
                        if(tubes[3*n+2-j] != 0)
                        {
                            moveBall(tubes, balls, n, fromcol);
                            moves.push_back({n+1, fromcol+1});                            
                        }
                    }

                    break;
                }
            }
        }
    
        if(tubes[3*i+1] != color)
        {
            moveBall(tubes, balls, i+1, n);
            moves.push_back({i+2, n+1});
            moveBall(tubes, balls, i, n);
            moves.push_back({i+1, n+1});
            moveBall(tubes, balls, i, i+1);
            moves.push_back({i+1, i+2});
            moveBall(tubes, balls, n, i);
            moves.push_back({n+1, i+1});
            moveBall(tubes, balls, n, i);
            moves.push_back({n+1, i+1});
        }
        
        if(tubes[3*i + 2] != color)
        {
            moveBall(tubes, balls, i, n);
            moves.push_back({i+1, n+1});
            
            for(int j=0; j<3; j++)
            {
                if(balls[3*color + j] / 3 != i)
                {
                    int fromcol = balls[3*color+j] / 3;
                    for(int k=0; k<3; k++)
                    {
                        if(tubes[3*fromcol + 2 - k] != color)
                        {
                            moveBall(tubes, balls, fromcol, n);
                            moves.push_back({fromcol+1, n+1});
                        }
                        else
                        {
                            moveBall(tubes, balls, fromcol, i);
                            moves.push_back({fromcol+1, i+1});
                            break;
                        }
                    }
                    
                    for(int j=0; j<3; j++)
                    {
                        if(tubes[3*n+2-j] != 0)
                        {
                            moveBall(tubes, balls, n, fromcol);
                            moves.push_back({n+1, fromcol+1});                            
                        }
                    }

                    break;
                }
            }
        }       
    }
    
    std::cout << moves.size() << std::endl;
    for(auto it : moves)
        std::cout << it.first << " " << it.second << std::endl;
}
