#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int,int> pii;

#define MAXN 1000005

int n, m, cnt[4][4], id[128], GS[4][MAXN], GC[4][MAXN], IS[4][MAXN], IC[4][MAXN], colp[MAXN], rowp[MAXN], vis[4][MAXN], code[4][4], memC[MAXN], pi[3*MAXN], done[4][MAXN];

string small, large, cycle, text;

int findShift(string &s, string &t) {
    if(s.size() != t.size()) return -1;
	text = s + "#" + t + t;
	int sz = s.size();
	
	int l = text.length();
	pi[0] = 0;
	for (int i = 1; i < l; i++) {
		int j = pi[i-1];
		while(j > 0 && text[i] != text[j]) {
			j = pi[j-1];
		}
		if(text[i] == text[j]) {
			j++;
		}
		pi[i] = j;
	}
	
	for(int i = 2*sz; i < l; i++) {
		if(pi[i] == sz) {
			return i-2*sz;
		}
	}
	return -1;
}

int main() {
	freopen("input.in", "r", stdin);
	
	int L = id['L'] = 0;
	int T = id['T'] = 1;
	int R = id['R'] = 2;
	int B = id['B'] = 3;
	
	scanf("%d %d\n", &n, &m);
	for(int i = 0; i < n+m; i++) {
		char a, b; int p, q;
		scanf("%c %c %d %d\n", &a, &b, &p, &q);
		a = id[a];
		b = id[b];
		cnt[a][b]++;
		cnt[b][a]++;
		GS[a][p] = b;
		GC[a][p] = q;
		GS[b][q] = a;
		GC[b][q] = p;
	}
	
	if((cnt[T][B] && cnt[L][R]) || (cnt[L][T] != cnt[B][R])) {
		printf("No solution\n");
		return 0;
	}
	
	int smallPtr[4];
	int d = min(cnt[L][T], cnt[L][B]);
	for(int x = 1; x <= cnt[L][T]; x++) {
		IS[L][x] = T;
		IS[T][x] = L;
		IC[L][x] = x;
		IC[T][x] = x;
		smallPtr[L] = 1;
	}
	for(int x = 1; x <= cnt[T][R]; x++) {
		IS[T][m-x+1] = R;
		IS[R][x] = T;
		IC[T][m-x+1] = x;
		IC[R][x] = m-x+1;
		smallPtr[T] = m-d+1;
	}
	for(int x = 1; x <= cnt[R][B]; x++) {
		IS[R][n-x+1] = B;
		IS[B][m-x+1] = R;
		IC[R][n-x+1] = m-x+1;
		IC[B][m-x+1] = n-x+1;
		smallPtr[R] = n-d+1;
	}
	for(int x = 1; x <= cnt[B][L]; x++) {
		IS[B][x] = L;
		IS[L][n-x+1] = B;
		IC[B][x] = n-x+1;
		IC[L][n-x+1] = x;
		smallPtr[B] = 1;
	}
	for(int x = cnt[L][T]+1, y = cnt[T][R]+1; x <= cnt[L][T]+cnt[L][R]; x++, y++) {
		IS[L][x] = R;
		IS[R][y] = L;
		IC[L][x] = y;
		IC[R][y] = x;
	}
	for(int x = cnt[L][T]+1, y = cnt[L][B]+1; x <= cnt[L][T]+cnt[T][B]; x++, y++) {
		IS[T][x] = B;
		IS[B][y] = T;
		IC[T][x] = y;
		IC[B][y] = x;
	}
	
	char cd = 'A';
	for(int i = 0; i < 4; i++) {
		for(int j = 0; j < 4; j++) {
			code[i][j] = cd++;
		}
	}
	
	int startS = -1, startC = d+1;
	if(cnt[L][R]) {
		startS = L;
	} else if(cnt[T][B]) {
		startS = T;
	} else if(cnt[L][T] != cnt[L][B]) {
		startS = L;
	}
	if(startS != -1) {
		int currS = startS, currC = startC;
		while(1) {
			int nextS = IS[currS][currC];
			int nextC = IC[currS][currC];
			large += code[currS][nextS];
			currS = (nextS+2)%4;
			currC = nextC;
			if(currS==startS && currC==startC) {
				break;
			}
		}
	}
	
	if(startS == -1) {
		startS = 0;
	}
	for(int i = 0; i < 4; i++) {
		int nextS = (startS+1)%4;
		small += code[startS][nextS];
		startS = (nextS+2)%4;
	}
	
	if(d > 0) {
		int smallCnt = 0;
		for(int x = 1; x <= (startS%2==0?n:m); x++) {
			if(!vis[startS][x] && code[startS][GS[startS][x]] == small[0]) {
				cycle = string();
				int currS = startS, currC = x;
				for(int i = 0; ; i++) {
					vis[currS][currC] = true;
					memC[i] = currC;
					int nextS = GS[currS][currC];
					int nextC = GC[currS][currC];
					cycle += code[currS][nextS];
					currS = (nextS+2)%4;
					currC = nextC;
					if(currS==startS && currC==x) {
						break;
					}
				}
				
				if(cycle.length() == small.length()) {
					int p = findShift(small, cycle);
					if(p != -1) {
						smallCnt++;
						
						currS = startS;
						currC = smallPtr[startS]++;
						for(int j = p, k = 0; k < 4; j=(j+1)%4, k++) {
							if(currS%2==0) {
								rowp[currC] = memC[j];
							} else {
								colp[currC] = memC[j];
							}
							done[currS][memC[j]] = 1;
							int nextS = IS[currS][currC];
							int nextC = IC[currS][currC];
							currS = (nextS+2)%4;
							currC = nextC;
						}
					}
				}
			}
		}
		
		if(smallCnt != d) {
			printf("No solution\n");
			return 0;
		}
	}
	
	d = large.size()?(n+m-4*d)/large.size():0;
	if(d > 0) {
		int largeCnt = 0;
		memset(vis, 0, sizeof(vis));
		for(int x = 1; x <= (startS%2==0?n:m); x++) {
			if(!vis[startS][x] && !done[startS][x] && code[startS][GS[startS][x]] == large[0]) {
				cycle = string();
				int currS = startS, currC = x;
				for(int i = 0; ; i++) {
					vis[currS][currC] = true;
					memC[i] = currC;
					int nextS = GS[currS][currC];
					int nextC = GC[currS][currC];
					cycle += code[currS][nextS];
					currS = (nextS+2)%4;
					currC = nextC;
					if(currS==startS && currC==x) {
						break;
					}
				}
				
				if(cycle.length() == large.length()) {
					int p = findShift(large, cycle);
					if(p != -1) {
						largeCnt++;
						
						currS = startS;
						currC = startC++;
						int sz = large.size();
						for(int j = p, k = 0; k < sz; j=(j+1)%sz, k++) {
							if(currS%2==0) {
								rowp[currC] = memC[j];
							} else {
								colp[currC] = memC[j];
							}
							int nextS = IS[currS][currC];
							int nextC = IC[currS][currC];
							currS = (nextS+2)%4;
							currC = nextC;
						}
					}
				}
			}
		}
		
		if(largeCnt != d) {
			printf("No solution\n");
			return 0;
		}
	}
	
	for(int i = 1; i <= n; i++) {
		printf("%d ", rowp[i]);
	}
	printf("\n");
	for(int i = 1; i <= m; i++) {
		printf("%d ", colp[i]);
	}
	printf("\n");
}
