#include "stdio.h"
#include "stdbool.h"

#define CIRCLES_COUNT 4
#define CIRCLE_CAPACITY 12

static inline void rotate_right(int c[CIRCLE_CAPACITY]) {
	int last = c[CIRCLE_CAPACITY-1];
	for (int i = CIRCLE_CAPACITY-1; i > 0; i--)
		c[i] = c[i-1];
	c[0] = last;
}

void print_circle(const int c[CIRCLE_CAPACITY]) {
	for (int i = 0; i < CIRCLE_CAPACITY; i++)
		printf("%d ", c[i]);
	printf("\n");
}

void print_circles(const int cc[CIRCLES_COUNT][CIRCLE_CAPACITY]) {
	for (int row = 0; row < CIRCLES_COUNT; row++) {
		print_circle(cc[row]);
	}
}

bool all_equal(const int cc[CIRCLES_COUNT][CIRCLE_CAPACITY]) {
	bool first_column = true;
	int prev_sum;
	for (int col = 0; col < CIRCLE_CAPACITY; col++) {
		int sum = 0;
		for (int i = 0; i < CIRCLES_COUNT; i++) {
			sum += cc[i][col];
		}

		if (!first_column && sum != prev_sum)
				return false;
		prev_sum = sum;

		first_column = false;
	}
	return true;
}

void main() {
	int circles[CIRCLES_COUNT][CIRCLE_CAPACITY] = {
		{3, 9, 6, 4, 3, 7, 5, 2, 4, 8, 3, 6},
		{8, 4, 7, 5, 8, 2, 9, 5, 5, 8, 4, 6},
		{6, 5, 8, 1, 6, 6, 7, 1, 3, 7, 1, 9},
		{9, 2, 4, 6, 8, 4, 3, 8, 5, 2, 3, 7}
	};

	int max_rotations = CIRCLE_CAPACITY;
	for (int i = 1; i < CIRCLES_COUNT; i++)
		max_rotations *= CIRCLE_CAPACITY;

	for (int rot = 0; ; rot++) {
		if (rot == max_rotations) {
			printf("No solutions\n");
			break;
		}

		if (!(all_equal(circles))) {
			rotate_right(circles[(rot / CIRCLE_CAPACITY) % CIRCLES_COUNT]);
			continue;
		}

		print_circles(circles);
		break;
	}
}