#include <iostream>
#include <vector>
#include <algorithm>

std::vector<std::vector<int>> getSmallestMatrix(const std::vector<std::vector<int>>& matrix, const std::vector<int>& extraNums) {
    std::vector<int> combined;

    // Flatten the 3x3 matrix into a 1D vector
    for (const auto& row : matrix) {
        for (int num : row) {
            combined.push_back(num);
        }
    }

    // Add extra numbers to the list
    combined.insert(combined.end(), extraNums.begin(), extraNums.end());

    // Sort the combined numbers
    std::sort(combined.begin(), combined.end());

    // Take the smallest 9 elements and form a new 3x3 matrix
    std::vector<std::vector<int>> newMatrix(3, std::vector<int>(3));
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            newMatrix[i][j] = combined[i * 3 + j];

    return newMatrix;
}

int main() {
    // Input 3x3 matrix
    std::vector<std::vector<int>> matrix = {
        {2, 4, 6},
        {8, 10, 12},
        {14, 16, 18}
    };

    // Input additional numbers (5 numbers: 1, 3, 5, 7, 9)
    std::vector<int> extraNums = {1, 3, 5, 7, 9};

    // Get new matrix with the smallest numbers
    std::vector<std::vector<int>> newMatrix = getSmallestMatrix(matrix, extraNums);

    // Output the new matrix
    for (const auto& row : newMatrix) {
        for (int num : row) {
            std::cout << num << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}