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AgICAgIGsgPSBuLzI7DQogICAgICAgIHdoaWxlIChrPChqKzEpKQ0KICAgICAgICB7DQogICAgICAgICAgICBqID0gaiAtIGs7DQogICAgICAgICAgICBrID0gay8yOw0KICAgICAgICB9DQogICAgICAgIGogPSBqICsgazsNCiAgICB9DQogICAgLy+8xsvjw7/Su7y2tcTK5LP2o6xszqrEs9K7vLajrGnOqs2s0ru8trXEsrvNrMi6o6zKudPDzazSu8TatOajqLy0zrvUy8vjo6kNCiAgICBuMSA9IDE7DQogICAgZm9yIChsPTE7IGw8PW07IGwrKykNCiAgICB7DQogICAgICAgIG4xID0gMipuMTsNCiAgICAgICAgbjIgPSBuMS8yOw0KICAgICAgICBlID0gMy4xNDE1OTI2L24yOw0KICAgICAgICBjID0gMS4wOw0KICAgICAgICBzID0gMC4wOw0KICAgICAgICBjMSA9IGNvcyhlKTsNCiAgICAgICAgczEgPSAtc2lnbipzaW4oZSk7DQogICAgICAgIGZvciAoaj0wOyBqPG4yOyBqKyspDQogICAgICAgIHsNCiAgICAgICAgICAgIGZvciAoaT1qOyBpPG47IGkrPW4xKQ0KICAgICAgICAgICAgew0KICAgICAgICAgICAgICAgIGsgPSBpICsgbjI7DQogICAgICAgICAgICAgICAgdHIgPSBjKnhba10gLSBzKnlba107DQogICAgICAgICAgICAgICAgdGkgPSBjKnlba10gKyBzKnhba107DQogICAgICAgICAgICAgICAgeFtrXSA9IHhbaV0gLSB0cjsNCiAgICAgICAgICAgICAgICB5W2tdID0geVtpXSAtIHRpOw0KICAgICAgICAgICAgICAgIHhbaV0gPSB4W2ldICsgdHI7DQogICAgICAgICAgICAgICAgeVtpXSA9IHlbaV0gKyB0aTsNCiAgICAgICAgICAgIH0NCiAgICAgICAgICAgIHQgPSBjOw0KICAgICAgICAgICAgYyA9IGMqYzEgLSBzKnMxOw0KICAgICAgICAgICAgcyA9IHQqczEgKyBzKmMxOw0KICAgICAgICB9DQogICAgfQ0KICAgIC8vyOe5+8rHx/NJRkZUo6zU2bP90tR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