#include using namespace std; struct Solution { int C; vector> color; vector dir; }; Solution transpose(Solution s) { int H = s.color.size(), W = s.color[0].size(); Solution t{s.C, vector(W, vector(H)), vector(W, string(H, '?'))}; string from = ">= 10) C = 4; Solution s{C, vector(H, vector(W)), vector(H, string(W, '?'))}; auto horizontal = [&](int r) { for (int c = 0; c < W; c += 2) s.dir[r][c] = '>', s.dir[r][c + 1] = '<'; }; auto vertical = [&](int r) { for (int c = 0; c < W; ++c) s.dir[r][c] = 'v', s.dir[r + 1][c] = '^'; }; if (H == 1) { for (int c = 0; c < W; ++c) s.color[0][c] = (c + 1) / 2; horizontal(0); } else if (H == 2) { fill(s.color[1].begin(), s.color[1].end(), 1); vertical(0); } else if (H == 3) { fill(s.color[1].begin(), s.color[1].end(), 1); for (int c = 0; c < W; ++c) s.color[2][c] = c % 4 == 0 || c % 4 == 3 ? 1 : 2 + c / 4; vertical(0); horizontal(2); } else if (H == 4) { fill(s.color[1].begin(), s.color[1].end(), 1); fill(s.color[2].begin(), s.color[2].end(), 1); fill(s.color[3].begin(), s.color[3].end(), 2); vertical(0); vertical(2); } else if (W == 6 || W == 8) { vector a = W == 6 ? vector{"122223", "121123", "122123", "111123", "222223"} : vector{"12222223", "12111123", "12211223", "11111123", "22222223"}; for (int r = 0; r < H; ++r) for (int c = 0; c < W; ++c) s.color[r][c] = a[r][c] - '1'; horizontal(0); for (int c = 2; c < W - 2; ++c) s.dir[0][c] = 'v', s.dir[1][c] = '^'; s.dir[1][0] = '>'; s.dir[1][1] = '<'; s.dir[1][W - 2] = '>'; s.dir[1][W - 1] = '<'; horizontal(2); for (int c = 0; c < W - 2; ++c) s.dir[3][c] = 'v', s.dir[4][c] = '^'; s.dir[3][W - 2] = '>'; s.dir[3][W - 1] = '<'; s.dir[4][W - 2] = '>'; s.dir[4][W - 1] = '<'; } else { fill(s.color[0].begin(), s.color[0].end(), 0); fill(s.color[1].begin(), s.color[1].end(), 1); for (int c = 0; c < W; ++c) s.color[2][c] = 1 + c % 2; fill(s.color[3].begin(), s.color[3].end(), 2); fill(s.color[4].begin(), s.color[4].end(), 3); vertical(0); horizontal(2); vertical(3); } return s; } Solution large(int H, int W) { Solution s{3, vector(H, vector(W)), vector(H, string(W, '?'))}; int A = 0, B = 1, C = 2; fill(s.color[0].begin(), s.color[0].end(), A); s.color[0][W - 1] = B; fill(s.color[1].begin(), s.color[1].end(), B); s.color[1][W - 2] = A; fill(s.color[2].begin(), s.color[2].end(), B); s.color[2][1] = s.color[2][W - 2] = A; fill(s.color[3].begin(), s.color[3].end(), A); s.color[3][0] = s.color[3][W - 1] = B; for (int r = 4; r <= H - 3; ++r) { for (int c = 0; c < W - 2; ++c) s.color[r][c] = c % 2 ? A : B; s.color[r][W - 2] = A; s.color[r][W - 1] = B; } fill(s.color[H - 2].begin(), s.color[H - 2].end(), B); fill(s.color[H - 1].begin(), s.color[H - 1].end(), C); for (int c = 0; c < W - 2; ++c) s.dir[0][c] = 'v', s.dir[1][c] = '^'; for (int r = 0; r < 4; ++r) s.dir[r][W - 2] = '>', s.dir[r][W - 1] = '<'; s.dir[2][0] = '>'; s.dir[2][1] = '<'; s.dir[3][0] = '>'; s.dir[3][1] = '<'; for (int c = 2; c < W - 2; ++c) s.dir[2][c] = 'v', s.dir[3][c] = '^'; for (int r = 4; r <= H - 3; ++r) for (int c = 0; c < W; c += 2) s.dir[r][c] = '>', s.dir[r][c + 1] = '<'; for (int c = 0; c < W; ++c) s.dir[H - 2][c] = 'v', s.dir[H - 1][c] = '^'; return s; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int H, W; cin >> H >> W; bool swapped = H > W; int h = min(H, W), w = max(H, W); Solution ans; if (h <= 5) ans = small(h, w); else if (w % 2 == 0) ans = large(h, w); else ans = transpose(large(w, h)); if (swapped) ans = transpose(ans); cout << ans.C << '\n'; for (int r = 0; r < H; ++r) for (int c = 0; c < W; ++c) { if (ans.dir[r][c] != '>' && ans.dir[r][c] != 'v') continue; int nr = r + (ans.dir[r][c] == 'v'); int nc = c + (ans.dir[r][c] == '>'); cout << r + 1 << ' ' << c + 1 << ' ' << ans.color[r][c] + 1 << ' ' << nr + 1 << ' ' << nc + 1 << ' ' << ans.color[nr][nc] + 1 << '\n'; } }