Problem:
Score : 100 points

Problem Statement

You are given two strings s and t consisting of lowercase English letters and an integer L.

We will consider generating a string of length L by concatenating one or more copies of s and t. Here, it is allowed to use the same string more than once.

For example, when s = at, t = code and L = 6, the strings atatat, atcode and codeat can be generated.

Among the strings that can be generated in this way, find the lexicographically smallest one. In the cases given as input, it is always possible to generate a string of length L.

Constraints

1 ≤ L ≤ 2 × 10^5

1 ≤ |s|, |t| ≤ L

s and t consist of lowercase English letters.

It is possible to generate a string of length L in the way described in Problem Statement.

Input

Input is given from Standard Input in the following format:

N
x_1 s_1
x_2 s_2
:
x_N s_N

Output

Print the lexicographically smallest string among the ones that can be generated in the way described in Problem Statement.

Sample Input 1

6
at
code

Sample Output 1

atatat

This input corresponds to the example shown in Problem Statement.

Sample Input 2

8
coding
festival

Sample Output 2

festival

It is possible that either s or t cannot be used at all in generating a string of length L.

Sample Input 3

8
same
same

Sample Output 3

samesame

It is also possible that s = t.

Sample Input 4

10
coin
age

Sample Output 4

ageagecoin

Solution in C (GCC 5.4.1) (judge verdict: Accepted)

/*
cat <<EOF >mistaken-paste
*/

#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
 
#define BIG 2000000007
 
#define MOD 1000000007
typedef unsigned long long ull;
typedef   signed long long sll;

#define N_MAX 200000
#define M_MAX 200
 
typedef struct {
	int a;
	int b;
} hw;

typedef struct {
	sll a;
	sll b;
} hwll;
 

typedef struct {
	ull a;
	int i;
} struct_a;

typedef struct {
	ull cost;
	int left;
	int right;
} struct_b;


const hw vector8[8] = {
	{-1, -1},
	{-1,  0},
	{-1, +1},
	{ 0, -1},
	{ 0, +1},
	{+1, -1},
	{+1,  0},
	{+1, +1}
};
 
ull n, m;
ull h, w;
ull k;
ull va, vb, vc, vd, ve, vf;
// ull a[N_MAX];
// sll a[N_MAX];
// ull wall[N_MAX][M_MAX];
// ull b[N_MAX];
// sll b[M_MAX];
char s[N_MAX + 1];
char t[N_MAX + 1];
// char s[N_MAX][M_MAX + 1];

// ull dp[N_MAX];
// ull dp[N_MAX + 1][N_MAX + 1];
// ull dp[N_MAX][M_MAX + 1];

// hw arr[N_MAX];
// hwll arr[N_MAX];
// hw brr[M_MAX];

// ull digitdp[102][   2][    2];
//          pos  less  carry


// struct_a arr[N_MAX * 2];
// struct_b brr[N_MAX * 2];

void swap_adj(ull *a, ull *b){
	ull tmp = *b;
	*b = *a;
	*a = tmp;
	return;
}
 
ull divide(ull a, ull b){
	ull x = MOD - 2;
	ull ans = 1;
	while (x) {
		if (x & 1) ans = (ans * b) % MOD;
		b = (b * b) % MOD;
		x /= 2;
	}
	return (a * ans) % MOD;
}
 
int digits(ull x){
	int i = 1;
	while (x >= 10) {
		x /= 10;
		i++;
	}
	return i;
}
 
ull min(ull x, ull y){
	return (x < y) ? x : y;
}

ull max(ull x, ull y){
	return (x > y) ? x : y;
}

ull gcd(ull x, ull y){
	if (x < y) {
		return gcd(y, x);
	} else if (y == 0) {
		return x;
	} else {
		return gcd(y, x % y);
	}
}
 
ull bitpow(ull a, ull x){
	ull result = 1;
	while (x) {
		if (x & 1) {
			result *= a;
			result %= MOD;
		}
		x /= 2;
		a = (a * a) % MOD;
	}
	return result;
}
 
// int nextroute(int arr[]){
// 	int i = n - 1;
// 	int j, x;
// 	while (arr[i - 1] > arr[i]) i--;
 
// 	x = n;
// 	for (j = i; j < n; j++) {
// 		if (arr[j] < arr[i - 1]) continue;
// 		if (x == n || arr[x] > arr[j]) x = j;
// 	}
// 	arr[i - 1] ^= arr[x];
// 	arr[x] ^= arr[i - 1];
// 	arr[i - 1] ^= arr[x];
 
// 	qsort(&arr[i], n - i, sizeof(int), comp);
// 	return 0;
// }
 
int targetdig(ull x, int index /* 1-indexed */){
	// static...?
	int posmax = digits(x);
	if (posmax < index) return -1;
	while (posmax > index) {
		posmax--;
		x /= 10;
	}
	return x % 10;
}
 
int intcomp(const int *left, const int *right){
	if (*left < *right) {
		return -1;
	} else if (*left > *right) {
		return +1;
	} else {
		return 0;
	}
}

int ullcomp(const ull *left, const ull *right){
	if (*left < *right) {
		return -1;
	} else if (*left > *right) {
		return +1;
	} else {
		return 0;
	}
}

int sllcomp(const sll *left, const sll *right){
	if (*left < *right) {
		return -1;
	} else if (*left > *right) {
		return +1;
	} else {
		return 0;
	}
}

int hwAcomp(const hw *left, const hw *right){
	return intcomp(&(left->a), &(right->a));
}
 
int hwBcomp(const hw *left, const hw *right){
	return intcomp(&(left->b), &(right->b));
}
 
int hwABcomp(const hw *left, const hw *right){
	int x = hwAcomp(left, right);
	if (x) return x;
	return hwBcomp(left, right);
}

int hwllAcomp(const hwll *left, const hwll *right){
	return sllcomp(&(left->a), &(right->a));
}
 
int hwllBcomp(const hwll *left, const hwll *right){
	return sllcomp(&(left->b), &(right->b));
}
 
int hwllABcomp(const hwll *left, const hwll *right){
	int x = hwllAcomp(left, right);
	if (x) return x;
	return hwllBcomp(left, right);
}

// int hw_lowerbound(hw arr[], int length, const hw x){
// 	int ok = 0;
// 	int ng = length;
// 	while (ok + 1 < ng) {
// 		int med = (ok + ng) / 2;
// 		if (hwABcomp(&arr[med], &x) >= 0) {
// 			ok = med;
// 		} else {
// 			ng = med;
// 		}
// 	}
// 	return ok;
// }

// int hw_upperbound(hw arr[], int length, const hw x){
// 	int ok = 0;
// 	int ng = length;
// 	while (ok + 1 < ng) {
// 		int med = (ok + ng) / 2;
// 		if (hwABcomp(&arr[med], &x) > 0) {
// 			ok = med;
// 		} else {
// 			ng = med;
// 		}
// 	}
// 	return ok;
// }

int strAcomp(const struct_a *left, const struct_a *right){
	return ullcomp(&(left->a), &(right->a));
}

int strBcomp(const struct_b *left, const struct_b *right){
	return ullcomp(&(left->cost), &(right->cost));
}

int bitlet(char c){
	return (1 << (c - 'a'));
}

ull ulldiff(ull a, ull b){
	if (a >= b) {
		return a - b;
	} else {
		return b - a;
	}
}


int fullfill(int x, int a, int b){
	int u = x / a;
	while (u >= 0) {
		if ((x - a * u) % b == 0) break;
		u--;
	}
	return u;
}

ull solve(){
	// int i, j, ki;
	ull result = 0;


	ull lena = strlen(s);
	ull lenb = strlen(t);

	int num;


	ull i = 0;
	ull maxloop = (lena / gcd(lena, lenb)) * lenb;
	while (i < maxloop && s[i % lena] == t[i % lenb]) i++;

	// printf("%llu\n", i);

	if (s[i % lena] > t[i % lenb]) {
		num = fullfill(n, lenb, lena);
		for (i = 0; i < num; i++) {
			printf("%s", t);
		}
		num = (n - lenb * num) / lena;
		for (i = 0; i < num; i++) {
			printf("%s", s);
		}
		puts("");
	} else {
		num = fullfill(n, lena, lenb);
		for (i = 0; i < num; i++) {
			printf("%s", s);
		}
		num = (n - lena * num) / lenb;
		for (i = 0; i < num; i++) {
			printf("%s", t);
		}
		puts("");
	}


	// int x = strncmp(s, t, min(lena, lenb)) 
	// if (x < -1
	// 	num = fullfill(n, lena, lenb);
	// 	for (i = 0; i < num; i++) {
	// 		printf("%s", s);
	// 	}
	// 	num = (n - lena * num) / lenb;
	// 	for (i = 0; i < num; i++) {
	// 		printf("%s", t);
	// 	}
	// 	puts("");
	// 	break;
	// case +1:
	// 	num = fullfill(n, lenb, lena);
	// 	for (i = 0; i < num; i++) {
	// 		printf("%s", t);
	// 	}
	// 	num = (n - lenb * num) / lena;
	// 	for (i = 0; i < num; i++) {
	// 		printf("%s", s);
	// 	}
	// 	puts("");
	// 	break;
	// case 0:
	// 	if (lena == lenb) {
	// 		num = n / lena;
	// 		for (i = 0; i < num; i++) {
	// 			printf("%s", s);
	// 		}
	// 		puts("");
	// 	} else {
	// 		ull i = 0;
	// 		while ((i == (ull)lena * (ull)lenb) && s[i % (ull)lena] == t[i % (ull)lenb]) i++;
	// 		if (s[i % lena] > t[i % lenb]) {
	// 			num = fullfill(n, lenb, lena);
	// 			for (i = 0; i < num; i++) {
	// 				printf("%s", t);
	// 			}
	// 			num = (n - lenb * num) / lena;
	// 			for (i = 0; i < num; i++) {
	// 				printf("%s", s);
	// 			}
	// 			puts("");
	// 		} else {
	// 			num = fullfill(n, lena, lenb);
	// 			for (i = 0; i < num; i++) {
	// 				printf("%s", s);
	// 			}
	// 			num = (n - lena * num) / lenb;
	// 			for (i = 0; i < num; i++) {
	// 				printf("%s", t);
	// 			}
	// 			puts("");
	// 		}
	// 	}
	// }

	// printf("%llu\n", ok);

	return 0;
}
 
int main(void){
	int i, j;
	int x, y;

	scanf("%llu", &n, &m);
	// scanf("%llu%llu", &h, &w);
	scanf("%s", s);
	scanf("%s", t);
	// scanf("%llu%llu%llu", &k, &va, &vb);
	// for (i = 0; i < n; i++) {
	// 	scanf("%llu%llu", &a[i], &b[i]);
	// }

	solve();

	// for (i = 0; i < m; i++) {
	// 	scanf("%llu%llu", &va, &vb);
	// 	solve();
	// }

	return 0;
}