#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 5;
const int tag[4][4] = {
    {-1, 5, 0, 1}, {-1, -1, 2, 3}, {-1, -1, -1, 4}, {-1, -1, -1, -1}};
int n, m, cnt[6], ans[2][N];
int trisize, tri[N << 2][4], len[N << 2], fail[N << 2], nxt[N << 2][4],
    q[N << 2];
bool vis[4][N];
pair<char, int> e[4][N], g[4][N];
vector<pair<char, int> > nowcycle, anscycle;
vector<vector<pair<char, int> > > achieve[N << 2];
inline void label(char& x) {
  x = (x == 'T') ? 0 : ((x == 'B') ? 1 : ((x == 'L') ? 2 : 3));
}
inline void mark(char& x, int& y, const pair<char, int>& t) {
  x = t.first;
  y = t.second;
}
inline void display(void) {
  puts("No solution");
  exit(0);
}
inline void addedge1(char x1, int y1, char x2, int y2) {
  label(x1);
  label(x2);
  if (x1 > x2) swap(x1, x2), swap(y1, y2);
  ++cnt[tag[x1][x2]];
  e[x1][y1] = make_pair(x2, y2);
  e[x2][y2] = make_pair(x1, y1);
}
inline void addedge2(char x1, int y1, char x2, int y2) {
  g[x1][y1] = make_pair(x2, y2);
  g[x2][y2] = make_pair(x1, y1);
}
inline bool validate(void) {
  vector<pair<char, int> >::iterator hd1;
  int hd2;
  for (int j = 0, k, L = anscycle.size(), state = 0; j < (L << 1); ++j) {
    for (state = nxt[state][anscycle[k = j % L].first]; len[state] > L;
         state = fail[state])
      ;
    if (len[state] == L && !achieve[state].empty()) {
      nowcycle = *achieve[state].rbegin();
      hd1 = nowcycle.begin();
      hd2 = (k + 1) % L;
      for (; hd1 != nowcycle.end(); ++hd1, (++hd2) %= L)
        if (hd1->first == 2 || hd1->first == 3)
          ans[0][anscycle[hd2].second] = hd1->second;
        else
          ans[1][anscycle[hd2].second] = hd1->second;
      achieve[state].pop_back();
      return 1;
    }
  }
  return 0;
}
inline void find_cycle1(char x, int y) {
  nowcycle.clear();
  int state = 0;
  for (; !vis[x][y];) {
    nowcycle.push_back(make_pair(x, y));
    if (!tri[state][x]) tri[state][x] = ++trisize;
    state = tri[state][x];
    vis[x][y] = 1;
    if (vis[e[x][y].first][e[x][y].second])
      x ^= 1;
    else
      mark(x, y, e[x][y]);
  }
  achieve[state].push_back(nowcycle);
}
inline bool find_cycle2(char x, int y) {
  anscycle.clear();
  int state = 0;
  for (; !vis[x][y];) {
    anscycle.push_back(make_pair(x, y));
    vis[x][y] = 1;
    if (vis[g[x][y].first][g[x][y].second])
      x ^= 1;
    else
      mark(x, y, g[x][y]);
  }
  if (validate()) return 1;
  reverse(anscycle.begin(), anscycle.end());
  if (validate()) return 1;
  return 0;
}
inline void Aho_Corasick(void) {
  for (int l = 1, r = 1, state, i, j; l <= r; ++l) {
    for (state = q[l], i = 0; i < 4; ++i)
      if (tri[state][i])
        q[++r] = nxt[state][i] = tri[state][i],
        len[tri[state][i]] = len[state] + 1;
    if (!state) continue;
    for (i = 0; i < 4; ++i) {
      j = fail[state];
      if (j && !nxt[j][i]) j = 0;
      if (!tri[state][i])
        nxt[state][i] = nxt[j][i];
      else
        fail[tri[state][i]] = nxt[j][i];
    }
  }
}
inline void constructive(void) {
  for (int i = 1; i <= cnt[0]; ++i) addedge2(0, i, 2, i);
  for (int i = 1; i <= cnt[1]; ++i) addedge2(0, m - i + 1, 3, i);
  for (int i = 1; i <= cnt[2]; ++i) addedge2(1, i, 2, n - i + 1);
  for (int i = 1; i <= cnt[3]; ++i) addedge2(1, m - i + 1, 3, n - i + 1);
  for (int i = 1; i <= cnt[4]; ++i) addedge2(2, cnt[0] + i, 3, cnt[1] + i);
  for (int i = 1; i <= cnt[5]; ++i) addedge2(0, cnt[0] + i, 1, cnt[2] + i);
  memset(vis, 0, sizeof vis);
  for (int i = 1; i <= n; ++i) {
    if (!vis[2][i])
      if (!find_cycle2(2, i)) display();
    if (!vis[3][i])
      if (!find_cycle2(3, i)) display();
  }
  for (int i = 1; i <= m; ++i) {
    if (!vis[0][i])
      if (!find_cycle2(0, i)) display();
    if (!vis[1][i])
      if (!find_cycle2(1, i)) display();
  }
  for (int i = 1; i <= n; ++i) printf("%d%c", ans[0][i], i == n ? '\n' : ' ');
  for (int i = 1; i <= m; ++i) printf("%d%c", ans[1][i], i == m ? '\n' : ' ');
}
int main(int argc, char** argv) {
  char x1, x2;
  int y1, y2;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n + m; ++i) {
    for (; !isupper(x1 = getchar());)
      ;
    for (; !isupper(x2 = getchar());)
      ;
    scanf("%d%d", &y1, &y2);
    addedge1(x1, y1, x2, y2);
  }
  if (cnt[4] && cnt[5]) display();
  if (cnt[0] != cnt[3] || cnt[1] != cnt[2]) display();
  for (int i = 1; i <= n; ++i) {
    if (!vis[2][i]) find_cycle1(2, i);
    if (!vis[3][i]) find_cycle1(3, i);
  }
  for (int i = 1; i <= m; ++i) {
    if (!vis[0][i]) find_cycle1(0, i);
    if (!vis[1][i]) find_cycle1(1, i);
  }
  Aho_Corasick();
  constructive();
  return 0;
}
