#include <cmath>
#include <iostream>

int main() {
    int top;
    std::cout << "Введите верхнюю цифру: ";
    if (!(std::cin >> top) || top < 1 || top > 8) {
        std::cout << "Ошибка входных данных.\n";
        return 0;
    }
    int left = 0, center = 0, right = 0, bottom = 0;
    bool found = false;
    for (int a = 1; a <= 8 && !found; a++)
        for (int b = 1; b <= 8 && !found; b++)
            for (int c = 1; c <= 8 && !found; c++)
                for (int d = 1; d <= 8 && !found; d++) {
                    bool different = top != a && top != b && top != c && top != d &&
                        a != b && a != c && a != d && b != c && b != d && c != d;
                    bool neighbours = std::abs(top - b) != 1 && std::abs(a - b) != 1 &&
                        std::abs(c - b) != 1 && std::abs(d - b) != 1;
                    if (different && neighbours) {
                        left = a; center = b; right = c; bottom = d; found = true;
                    }
                }
    if (found) {
        std::cout << "  " << top << "\n" << left << " " << center << " " << right
                  << "\n  " << bottom << "\n";
    } else std::cout << "Решения нет.\n";
    return 0;
}
