#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>

using namespace std;

template <typename T>
class distr
{
public:
    distr(int l, int r, bool fl = true) :
                       left(l), right(r), from_left(fl), cnt(0) { }
    // formális okokból: a "compare"-nek két T típusú paramétere van:
    bool operator()( const T&, const T&)
    {
        bool ret = from_left;
        const int  max = from_left ? left : right;
        if ( ++cnt == max )
        {
            cnt = 0;
            from_left = ! from_left;
        }
        return ret;
    }
private:
    const int left;
    const int right;
    int from_left;
    int cnt;
};

int main()
{
    int left, right;
    cin >> left >> right;
    ifstream if1("file1.txt");
    ifstream if2("file2.txt");

    merge( istream_iterator<string>(if1), istream_iterator<string>(),
           istream_iterator<string>(if2), istream_iterator<string>(),
           ostream_iterator<string>(cout,"\n"),
                                    distr<std::string>(left,right) );
    return 0;
}