#!/usr/bin/env python3
"""Chebyshev bounds propagator for Minimizing Partridge Packing (MiniZinc blackbox_exec).

Input / output (bounds propagator):
  position[1..P] ++ [max_pos]  (two comma-separated ints [lb, ub] per variable)
"""

from __future__ import annotations

import math
import sys


def cheb_map(i0: int, j0: int) -> int:
    k = i0 if i0 > j0 else j0
    base = k * k
    return base + i0 if i0 < j0 else base + k + j0


def find_n(P: int) -> int:
    if P <= 0:
        return -1
    n = (math.isqrt(1 + 8 * P) - 1) // 2
    return n if n * (n + 1) // 2 == P else -1


_CACHE: dict[int, tuple[int, list[int], list[int], list[int]]] = {}


def get_tables(P: int) -> tuple[int, list[int], list[int], list[int]]:
    tbl = _CACHE.get(P)
    if tbl is not None:
        return tbl
    n = find_n(P)
    L = 2 * P
    sizes = [s for s in range(n, 0, -1) for _ in range(s)]
    inv_x = [0] * (L * L)
    inv_y = [0] * (L * L)
    for v in range(L * L):
        k = math.isqrt(v)
        r = v - k * k
        if r < k:
            inv_x[v] = r
            inv_y[v] = k
        else:
            inv_x[v] = k
            inv_y[v] = r - k
    tbl = (L, sizes, inv_x, inv_y)
    _CACHE[P] = tbl
    return tbl


def propagate(vals: list[int]) -> list[int]:
    out = vals[:]
    P = len(vals) // 2 - 1
    if P <= 0 or find_n(P) < 0:
        return out

    L, sizes, inv_x, inv_y = get_tables(P)
    M = min(L, vals[2 * P + 1])
    if M < P:
        out[0], out[1] = 1, 0
        return out

    fixed: list[tuple[int, int, int, int]] = []
    for p in range(P):
        lp = max(0, out[2 * p])
        s = sizes[p]
        max_coord = M - s
        if max_coord < 0:
            out[0], out[1] = 1, 0
            return out
        up = min(out[2 * p + 1], cheb_map(max_coord, max_coord))
        if lp > up:
            out[0], out[1] = 1, 0
            return out
        out[2 * p] = lp
        out[2 * p + 1] = up

        if lp == up:
            ix, iy = inv_x[lp], inv_y[lp]
            ix1, iy1 = ix + s, iy + s
            if ix1 > M or iy1 > M:
                out[0], out[1] = 1, 0
                return out
            for fx0, fy0, fx1, fy1 in fixed:
                if ix < fx1 and fx0 < ix1 and iy < fy1 and fy0 < iy1:
                    out[0], out[1] = 1, 0
                    return out
            fixed.append((ix, iy, ix1, iy1))

    if not fixed:
        return out

    for q in range(P):
        lp = out[2 * q]
        up = out[2 * q + 1]
        if lp == up:
            continue
        s = sizes[q]
        max_coord = M - s

        while lp <= up:
            ix, iy = inv_x[lp], inv_y[lp]
            if ix <= max_coord and iy <= max_coord:
                ix1, iy1 = ix + s, iy + s
                for fx0, fy0, fx1, fy1 in fixed:
                    if ix < fx1 and fx0 < ix1 and iy < fy1 and fy0 < iy1:
                        break
                else:
                    break
            lp += 1
        if lp > up:
            out[0], out[1] = 1, 0
            return out

        while up > lp:
            ix, iy = inv_x[up], inv_y[up]
            if ix <= max_coord and iy <= max_coord:
                ix1, iy1 = ix + s, iy + s
                for fx0, fy0, fx1, fy1 in fixed:
                    if ix < fx1 and fx0 < ix1 and iy < fy1 and fy0 < iy1:
                        break
                else:
                    break
            up -= 1

        out[2 * q] = lp
        out[2 * q + 1] = up

    return out


def main() -> None:
    write = sys.stdout.write
    flush = sys.stdout.flush
    for line in sys.stdin:
        int_seg, _, _ = line.partition(";")
        ints = [int(tok) for tok in int_seg.split(",") if tok.strip()]
        write(",".join(map(str, propagate(ints))) + ";\n")
        flush()


if __name__ == "__main__":
    main()
