using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Reflection.Metadata;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace AtCoder
{
	public class Program
	{
		static void Main()
		{
			using var cin = new Scanner();
			var (n, k) = cin.Int2();
			var a = new long[n, n];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					a[i, j] = cin.Long();
				}
			}

			long inf = 1000000000;
			int si = n * 2;
			int ti = n * 2 + 1;
			var flow = new McfGraphLong(n * 2 + 2);
			flow.AddEdge(si, ti, n * k, inf);
			for (int i = 0; i < n; i++) {
				flow.AddEdge(si, i, k, 0);
				flow.AddEdge(i + n, ti, k, 0);
			}

			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					flow.AddEdge(i, j + n, 1, inf - a[i, j]);
				}
			}

			var ret = flow.Flow(si, ti, n * k);
			Console.WriteLine(n * k * inf - ret.cost);

			var ans = new char[n][];
			for (int i = 0; i < n; i++) {
				ans[i] = new char[n];
			}

			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					ans[i][j] = '.';
				}
			}

			var edges = flow.Edges();
			foreach (var e in edges) {
				if (e.From == si || e.To == ti || e.Flow == 0) {
					continue;
				}

				ans[e.From][e.To - n] = 'X';
			}

			for (int i = 0; i < n; i++) {
				Console.WriteLine(new string(ans[i]));
			}
		}
	}

	public interface INumOperator<T> : IEqualityComparer<T>, IComparer<T> where T : struct
	{
		/// <summary>
		/// MinValue
		/// </summary>
		public T MinValue { get; }
		/// <summary>
		/// MaxValue
		/// </summary>
		public T MaxValue { get; }
		/// <summary>
		/// Addition operator +
		/// </summary>
		/// <returns><paramref name="x"/> + <paramref name="y"/></returns>
		T Add(T x, T y);
		/// <summary>
		/// Subtraction operator -
		/// </summary>
		/// <returns><paramref name="x"/> - <paramref name="y"/></returns>
		T Subtract(T x, T y);
		/// <summary>
		/// Multiplication operator *
		/// </summary>
		/// <returns><paramref name="x"/> * <paramref name="y"/></returns>
		T Multiply(T x, T y);
		/// <summary>
		/// Division operator /
		/// </summary>
		/// <returns><paramref name="x"/> / <paramref name="y"/></returns>
		T Divide(T x, T y);
		/// <summary>
		/// Remainder operator %
		/// </summary>
		/// <returns><paramref name="x"/> % <paramref name="y"/></returns>
		T Modulo(T x, T y);
		/// <summary>
		/// Unary minus operator -
		/// </summary>
		/// <returns>-<paramref name="x"/></returns>
		T Minus(T x);
		/// <summary>
		/// Increment operator ++
		/// </summary>
		/// <returns>++<paramref name="x"/></returns>
		T Increment(T x);
		/// <summary>
		/// Decrement operator --
		/// </summary>
		/// <returns>--<paramref name="x"/></returns>
		T Decrement(T x);
		/// <summary>
		/// Greater than operator &gt;
		/// </summary>
		/// <returns><paramref name="x"/> &gt; <paramref name="y"/></returns>
		bool GreaterThan(T x, T y);
		/// <summary>
		/// Greater than or equal operator &gt;=
		/// </summary>
		/// <returns><paramref name="x"/> &gt;= <paramref name="y"/></returns>
		bool GreaterThanOrEqual(T x, T y);
		/// <summary>
		/// Less than operator &lt;
		/// </summary>
		/// <returns><paramref name="x"/> &lt; <paramref name="y"/></returns>
		bool LessThan(T x, T y);
		/// <summary>
		/// Less than or equal operator &lt;=
		/// </summary>
		/// <returns><paramref name="x"/> &lt;= <paramref name="y"/></returns>
		bool LessThanOrEqual(T x, T y);
	}
	public readonly struct IntOperator : INumOperator<int>
	{
		public int MinValue => int.MinValue;
		public int MaxValue => int.MaxValue;
		public int Add(int x, int y) => x + y;
		public int Subtract(int x, int y) => x - y;
		public int Multiply(int x, int y) => x * y;
		public int Divide(int x, int y) => x / y;
		public int Modulo(int x, int y) => x % y;
		public int Minus(int x) => -x;
		public int Increment(int x) => ++x;
		public int Decrement(int x) => --x;
		public bool GreaterThan(int x, int y) => x > y;
		public bool GreaterThanOrEqual(int x, int y) => x >= y;
		public bool LessThan(int x, int y) => x < y;
		public bool LessThanOrEqual(int x, int y) => x <= y;
		public int Compare(int x, int y) => x.CompareTo(y);
		public bool Equals(int x, int y) => x == y;
		public int GetHashCode(int obj) => obj.GetHashCode();
	}
	public readonly struct LongOperator : INumOperator<long>
	{
		public long MinValue => long.MinValue;
		public long MaxValue => long.MaxValue;
		public long Add(long x, long y) => x + y;
		public long Subtract(long x, long y) => x - y;
		public long Multiply(long x, long y) => x * y;
		public long Divide(long x, long y) => x / y;
		public long Modulo(long x, long y) => x % y;
		public long Minus(long x) => -x;
		public long Increment(long x) => ++x;
		public long Decrement(long x) => --x;
		public bool GreaterThan(long x, long y) => x > y;
		public bool GreaterThanOrEqual(long x, long y) => x >= y;
		public bool LessThan(long x, long y) => x < y;
		public bool LessThanOrEqual(long x, long y) => x <= y;
		public int Compare(long x, long y) => x.CompareTo(y);
		public bool Equals(long x, long y) => x == y;
		public int GetHashCode(long obj) => obj.GetHashCode();
	}
	public readonly struct UIntOperator : INumOperator<uint>
	{
		public uint MinValue => uint.MinValue;
		public uint MaxValue => uint.MaxValue;
		public uint Add(uint x, uint y) => x + y;
		public uint Subtract(uint x, uint y) => x - y;
		public uint Multiply(uint x, uint y) => x * y;
		public uint Divide(uint x, uint y) => x / y;
		public uint Modulo(uint x, uint y) => x % y;
		public uint Minus(uint x) => throw new InvalidOperationException("Uint type cannot be negative.");
		public uint Increment(uint x) => ++x;
		public uint Decrement(uint x) => --x;
		public bool GreaterThan(uint x, uint y) => x > y;
		public bool GreaterThanOrEqual(uint x, uint y) => x >= y;
		public bool LessThan(uint x, uint y) => x < y;
		public bool LessThanOrEqual(uint x, uint y) => x <= y;
		public int Compare(uint x, uint y) => x.CompareTo(y);
		public bool Equals(uint x, uint y) => x == y;
		public int GetHashCode(uint obj) => obj.GetHashCode();
	}
	public readonly struct ULongOperator : INumOperator<ulong>
	{
		public ulong MinValue => ulong.MinValue;
		public ulong MaxValue => ulong.MaxValue;
		public ulong Add(ulong x, ulong y) => x + y;
		public ulong Subtract(ulong x, ulong y) => x - y;
		public ulong Multiply(ulong x, ulong y) => x * y;
		public ulong Divide(ulong x, ulong y) => x / y;
		public ulong Modulo(ulong x, ulong y) => x % y;
		public ulong Minus(ulong x) => throw new InvalidOperationException("Ulong type cannot be negative.");
		public ulong Increment(ulong x) => ++x;
		public ulong Decrement(ulong x) => --x;
		public bool GreaterThan(ulong x, ulong y) => x > y;
		public bool GreaterThanOrEqual(ulong x, ulong y) => x >= y;
		public bool LessThan(ulong x, ulong y) => x < y;
		public bool LessThanOrEqual(ulong x, ulong y) => x <= y;
		public int Compare(ulong x, ulong y) => x.CompareTo(y);
		public bool Equals(ulong x, ulong y) => x == y;
		public int GetHashCode(ulong obj) => obj.GetHashCode();
	}
	public readonly struct DoubleOperator : INumOperator<double>
	{
		public double MinValue => double.MinValue;
		public double MaxValue => double.MaxValue;
		public double Add(double x, double y) => x + y;
		public double Subtract(double x, double y) => x - y;
		public double Multiply(double x, double y) => x * y;
		public double Divide(double x, double y) => x / y;
		public double Modulo(double x, double y) => x % y;
		public double Minus(double x) => -x;
		public double Increment(double x) => ++x;
		public double Decrement(double x) => --x;
		public bool GreaterThan(double x, double y) => x > y;
		public bool GreaterThanOrEqual(double x, double y) => x >= y;
		public bool LessThan(double x, double y) => x < y;
		public bool LessThanOrEqual(double x, double y) => x <= y;
		public int Compare(double x, double y) => x.CompareTo(y);
		public bool Equals(double x, double y) => x == y;
		public int GetHashCode(double obj) => obj.GetHashCode();
	}

	public interface ICastOperator<TFrom, TTo>
		where TFrom : struct
		where TTo : struct
	{
		TTo Cast(TFrom y);
	}

	public struct SameTypeCastOperator<T> : ICastOperator<T, T>
		where T : struct
	{
		public T Cast(T y) => y;
	}

	public struct IntToLongCastOperator : ICastOperator<int, long>
	{
		public long Cast(int y) => y;
	}

	/// <summary>
	/// Minimum-cost flow problem を扱うライブラリ(int版)です。
	/// </summary>
	public class McfGraphInt
		: McfGraph<int, IntOperator, int, IntOperator, SameTypeCastOperator<int>>
	{
		public McfGraphInt(int n) : base(n) { }
	}

	/// <summary>
	/// Minimum-cost flow problem を扱うライブラリ(long版)です。
	/// </summary>
	public class McfGraphLong
	   : McfGraph<long, LongOperator, long, LongOperator, SameTypeCastOperator<long>>
	{
		public McfGraphLong(int n) : base(n) { }
	}

	/// <summary>
	/// Minimum-cost flow problem を扱うライブラリです。
	/// </summary>
	/// <typeparam name="TCap">容量の型</typeparam>
	/// <typeparam name="TCapOp"><typeparamref name="TCap"/> に対応する演算を提供する型</typeparam>
	/// <typeparam name="TCost">コストの型</typeparam>
	/// <typeparam name="TCostOp"><typeparamref name="TCost"/> に対応する演算を提供する型</typeparam>
	/// <typeparam name="TCast">
	/// <typeparamref name="TCap"/> から <typeparamref name="TCost"/> への型変換を提供する型
	/// </typeparam>
	/// <remarks>
	/// <para>制約: <typeparamref name="TValue"/> は int, long。</para>
	/// </remarks>
	public class McfGraph<TCap, TCapOp, TCost, TCostOp, TCast>
		where TCap : struct
		where TCapOp : struct, INumOperator<TCap>
		where TCost : struct
		where TCostOp : struct, INumOperator<TCost>
		where TCast : ICastOperator<TCap, TCost>
	{
		static readonly TCapOp capOp = default;
		static readonly TCostOp costOp = default;
		static readonly TCast cast = default;

		/// <summary>
		/// <paramref name="n"/> 頂点 0 辺のグラフを作ります。
		/// </summary>
		/// <remarks>
		/// <para>制約: 0 ≤ <paramref name="n"/> ≤ 10^8</para>
		/// <para>計算量: O(<paramref name="n"/>)</para>
		/// </remarks>
		public McfGraph(int n)
		{
			_n = n;
			_g = new List<EdgeInternal>[n];
			for (int i = 0; i < n; i++) {
				_g[i] = new List<EdgeInternal>();
			}
			_pos = new List<(int first, int second)>();
		}

		/// <summary>
		/// <paramref name="from"/> から <paramref name="to"/> へ
		/// 最大容量 <paramref name="cap"/>、コスト <paramref name="cost"/> の辺を追加し、
		/// 何番目に追加された辺かを返します。
		/// </summary>
		/// <remarks>
		/// 制約: 
		/// <list type="bullet">
		/// <item>
		/// <description>0 ≤ <paramref name="from"/>, <paramref name="to"/> &lt; n</description>
		/// </item>
		/// <item>
		/// <description>0 ≤ <paramref name="cap"/>, <paramref name="cost"/></description>
		/// </item>
		/// </list>
		/// <para>計算量: ならしO(1)</para>
		/// </remarks>
		public int AddEdge(int from, int to, TCap cap, TCost cost)
		{
			Debug.Assert(0 <= from && from < _n);
			Debug.Assert(0 <= to && to < _n);
			int m = _pos.Count;
			_pos.Add((from, _g[from].Count));
			_g[from].Add(new EdgeInternal(to, _g[to].Count, cap, cost));
			_g[to].Add(new EdgeInternal(from, _g[from].Count - 1, default, costOp.Minus(cost)));
			return m;
		}

		/// <summary>
		/// 今の内部の辺の状態を返します。
		/// </summary>
		/// <remarks>
		/// <para>AddEdge で <paramref name="i"/> 番目 (0-indexed) に追加された辺を返す。</para>
		/// <para>制約: m を追加された辺数として 0 ≤ i &lt; m</para>
		/// <para>計算量: O(1)</para>
		/// </remarks>
		public Edge GetEdge(int i)
		{
			int m = _pos.Count;
			Debug.Assert(0 <= i && i < m);
			var _e = _g[_pos[i].first][_pos[i].second];
			var _re = _g[_e.To][_e.Rev];
			return new Edge(_pos[i].first, _e.To, capOp.Add(_e.Cap, _re.Cap), _re.Cap, _e.Cost);
		}

		/// <summary>
		/// 今の内部の辺の状態を返します。
		/// </summary>
		/// <remarks>
		/// <para>辺の順番はadd_edgeで追加された順番と同一。</para>
		/// <para>計算量: m を追加された辺数として O(m)</para>
		/// </remarks>
		public List<Edge> Edges()
		{
			int m = _pos.Count;
			var result = new List<Edge>();
			for (int i = 0; i < m; i++) {
				result.Add(GetEdge(i));
			}
			return result;
		}

		/// <summary>
		/// 頂点 <paramref name="s"/> から <paramref name="t"/> へ流せる限り流し、
		/// その流量とコストを返します。
		/// </summary>
		/// <remarks>
		/// <para>制約: Slope関数と同じ</para>
		/// <para>計算量: Slope関数と同じ</para>
		/// </remarks>
		public (TCap cap, TCost cost) Flow(int s, int t)
		{
			return Flow(s, t, capOp.MaxValue);
		}

		/// <summary>
		/// 頂点 <paramref name="s"/> から <paramref name="t"/> へ
		/// 流量 <paramref name="flowLimit"/> に達するまで流せる限り流し、
		/// その流量とコストを返します。
		/// </summary>
		/// <remarks>
		/// <para>制約: Slope関数と同じ</para>
		/// <para>計算量: Slope関数と同じ</para>
		/// </remarks>
		public (TCap cap, TCost cost) Flow(int s, int t, TCap flowLimit)
		{
			return Slope(s, t, flowLimit).Last();
		}

		/// <summary>
		/// 返り値に流量とコストの関係の折れ線が入ります。
		/// 全ての x について、流量 x の時の最小コストを g(x) とすると、
		/// (x, g(x)) は返り値を折れ線として見たものに含まれます。
		/// </summary>
		/// <remarks>
		/// <list type="bullet">
		/// <item>
		/// <description>返り値の最初の要素は (0, 0)。</description>
		/// </item>
		/// <item>
		/// <description>返り値の .first、.second は共に狭義単調増加。</description>
		/// </item>
		/// <item>
		/// <description>3点が同一線上にあることは無い。</description>
		/// </item>
		/// <item>
		/// <description>返り値の最後の要素は最大流量 x として (x, g(x))。</description>
		/// </item>
		/// </list>
		/// 制約: 辺のコストの最大を x として
		/// <list type="bullet">
		/// <item>
		/// <description><paramref name="s"/> ≠ <paramref name="t"/></description>
		/// </item>
		/// <item>
		/// <description>Flow や Slope 関数を合わせて複数回呼んだときの挙動は未定義。</description>
		/// </item>
		/// <item>
		/// <description>
		/// <paramref name="s"/> から <paramref name="t"/> へ流したフローの
		/// 流量が cap に収まる。
		/// </description>
		/// </item>
		/// <item>
		/// <description>流したコストの総和が cost に収まる。</description>
		/// </item>
		/// <item>
		/// <description>(Cost: int) 0 ≤ nx ≤ 2 * 10^9 + 1000 </description>
		/// </item>
		/// <item>
		/// <description>(Cost: long) 0 ≤ nx ≤ 8 * 10^18 + 1000 </description>
		/// </item>
		/// </list>
		/// 計算量: F を流量、m を追加した辺の本数として 
		/// O(F(n + m) log n)
		/// </remarks>
		public List<(TCap cap, TCost cost)> Slope(int s, int t)
		{
			return Slope(s, t, capOp.MaxValue);
		}

		/// <summary>
		/// 返り値に流量とコストの関係の折れ線が入ります。
		/// 全ての x について、流量 x の時の最小コストを g(x) とすると、
		/// (x, g(x)) は返り値を折れ線として見たものに含まれます。
		/// </summary>
		/// <remarks>
		/// <list type="bullet">
		/// <item>
		/// <description>返り値の最初の要素は (0, 0)。</description>
		/// </item>
		/// <item>
		/// <description>返り値の .first、.second は共に狭義単調増加。</description>
		/// </item>
		/// <item>
		/// <description>3点が同一線上にあることは無い。</description>
		/// </item>
		/// <item>
		/// <description>
		/// 返り値の最後の要素は
		/// y = min(x, <paramref name="flowLimit"/>) として (y, g(y))。</description>
		/// </item>
		/// </list>
		/// 制約: 辺のコストの最大を x として
		/// <list type="bullet">
		/// <item>
		/// <description><paramref name="s"/> ≠ <paramref name="t"/></description>
		/// </item>
		/// <item>
		/// <description>Flow や Slope 関数を合わせて複数回呼んだときの挙動は未定義。</description>
		/// </item>
		/// <item>
		/// <description>
		/// <paramref name="s"/> から <paramref name="t"/> へ流したフローの
		/// 流量が cap に収まる。
		/// </description>
		/// </item>
		/// <item>
		/// <description>流したコストの総和が cost に収まる。</description>
		/// </item>
		/// <item>
		/// <description>(Cost: int) 0 ≤ nx ≤ 2 * 10^9 + 1000 </description>
		/// </item>
		/// <item>
		/// <description>(Cost: long) 0 ≤ nx ≤ 8 * 10^18 + 1000 </description>
		/// </item>
		/// </list>
		/// 計算量: F を流量、m を追加した辺の本数として 
		/// O(F(n + m) log n)
		/// </remarks>
		public List<(TCap cap, TCost cost)> Slope(int s, int t, TCap flow_limit)
		{
			Debug.Assert(0 <= s && s < _n);
			Debug.Assert(0 <= t && t < _n);
			Debug.Assert(s != t);
			// variants (C = maxcost):
			// -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0
			// reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge
			var dual = new TCost[_n];
			var dist = new TCost[_n];
			var pv = new int[_n];
			var pe = new int[_n];
			var vis = new bool[_n];

			bool DualRef()
			{
				dist.AsSpan().Fill(costOp.MaxValue);
				pv.AsSpan().Fill(-1);
				pe.AsSpan().Fill(-1);
				vis.AsSpan().Fill(false);

				var que = new PriorityQueueForMcf();
				dist[s] = default;
				que.Enqueue(default, s);
				while (que.Count > 0) {
					int v = que.Dequeue().to;
					if (vis[v]) continue;
					vis[v] = true;
					if (v == t) break;
					// dist[v] = shortest(s, v) + dual[s] - dual[v]
					// dist[v] >= 0 (all reduced cost are positive)
					// dist[v] <= (n-1)C
					for (int i = 0; i < _g[v].Count; i++) {
						var e = _g[v][i];
						if (vis[e.To] || capOp.Equals(e.Cap, default)) continue;
						// |-dual[e.to] + dual[v]| <= (n-1)C
						// cost <= C - -(n-1)C + 0 = nC
						TCost cost = costOp.Add(costOp.Subtract(e.Cost, dual[e.To]), dual[v]);
						if (costOp.GreaterThan(costOp.Subtract(dist[e.To], dist[v]), cost)) {
							dist[e.To] = costOp.Add(dist[v], cost);
							pv[e.To] = v;
							pe[e.To] = i;
							que.Enqueue(dist[e.To], e.To);
						}
					}
				}

				if (!vis[t]) {
					return false;
				}

				for (int v = 0; v < _n; v++) {
					if (!vis[v]) continue;
					// dual[v] = dual[v] - dist[t] + dist[v]
					//         = dual[v] - (shortest(s, t) + dual[s] - dual[t]) + (shortest(s, v) + dual[s] - dual[v])
					//         = - shortest(s, t) + dual[t] + shortest(s, v)
					//         = shortest(s, v) - shortest(s, t) >= 0 - (n-1)C
					dual[v] = costOp.Subtract(dual[v], costOp.Subtract(dist[t], dist[v]));
				}

				return true;
			}

			TCap flow = default;
			TCost cost = default;
			TCost prev_cost = costOp.Decrement(default); //-1
			var result = new List<(TCap cap, TCost cost)>();
			result.Add((flow, cost));
			while (capOp.LessThan(flow, flow_limit)) {
				if (!DualRef()) break;
				TCap c = capOp.Subtract(flow_limit, flow);
				for (int v = t; v != s; v = pv[v]) {
					if (capOp.LessThan(_g[pv[v]][pe[v]].Cap, c)) {
						c = _g[pv[v]][pe[v]].Cap;
					}
				}
				for (int v = t; v != s; v = pv[v]) {
					_g[pv[v]][pe[v]].Cap = capOp.Subtract(_g[pv[v]][pe[v]].Cap, c);
					_g[v][_g[pv[v]][pe[v]].Rev].Cap = capOp.Add(_g[v][_g[pv[v]][pe[v]].Rev].Cap, c);
				}
				TCost d = costOp.Minus(dual[s]);
				flow = capOp.Add(flow, c);
				cost = costOp.Add(cost, costOp.Multiply(cast.Cast(c), d));
				if (costOp.Equals(prev_cost, d)) {
					result.RemoveAt(result.Count - 1);
				}
				result.Add((flow, cost));
				prev_cost = cost;
			}
			return result;
		}

		/// <summary>
		/// フローを流すグラフの各辺に対応した情報を持ちます。
		/// </summary>
		public struct Edge
		{
			/// <summary>フローが流出する頂点。</summary>
			public int From { get; set; }
			/// <summary>フローが流入する頂点。</summary>
			public int To { get; set; }
			/// <summary>辺の容量。</summary>
			public TCap Cap { get; set; }
			/// <summary>辺の流量。</summary>
			public TCap Flow { get; set; }
			/// <summary>辺の費用</summary>
			public TCost Cost { get; set; }
			public Edge(int from, int to, TCap cap, TCap flow, TCost cost)
			{
				From = from;
				To = to;
				Cap = cap;
				Flow = flow;
				Cost = cost;
			}
		};

		private class EdgeInternal
		{
			public int To { get; set; }
			public int Rev { get; set; }
			public TCap Cap { get; set; }
			public TCost Cost { get; set; }
			public EdgeInternal(int to, int rev, TCap cap, TCost cost)
			{
				To = to;
				Rev = rev;
				Cap = cap;
				Cost = cost;
			}
		};

		private readonly int _n;
		private readonly List<(int first, int second)> _pos;
		private readonly List<EdgeInternal>[] _g;

		private class PriorityQueueForMcf
		{
			private (TCost cost, int to)[] _heap;

			public int Count { get; private set; } = 0;
			public PriorityQueueForMcf()
			{
				_heap = new (TCost cost, int to)[1024];
			}

			public void Enqueue(TCost cost, int to)
			{
				var pair = (cost, to);
				if (_heap.Length == Count) {
					var newHeap = new (TCost cost, int to)[_heap.Length * 2];
					_heap.CopyTo(newHeap, 0);
					_heap = newHeap;
				}

				_heap[Count] = pair;
				++Count;

				int c = Count - 1;
				while (c > 0) {
					int p = (c - 1) >> 1;
					if (Compare(_heap[p].cost, cost) < 0) {
						_heap[c] = _heap[p];
						c = p;
					} else {
						break;
					}
				}

				_heap[c] = pair;
			}

			public (TCost cost, int to) Dequeue()
			{
				(TCost cost, int to) ret = _heap[0];
				int n = Count - 1;

				var item = _heap[n];
				int p = 0;
				int c = (p << 1) + 1;
				while (c < n) {
					if (c != n - 1 && Compare(_heap[c + 1].cost, _heap[c].cost) > 0) {
						++c;
					}

					if (Compare(item.cost, _heap[c].cost) < 0) {
						_heap[p] = _heap[c];
						p = c;
						c = (p << 1) + 1;
					} else {
						break;
					}
				}

				_heap[p] = item;
				Count--;

				return ret;
			}

			[MethodImpl(MethodImplOptions.AggressiveInlining)]
			private int Compare(TCost x, TCost y) => costOp.Compare(y, x);
		}
	}

	public class HashMap<TKey, TValue> : Dictionary<TKey, TValue>
	{
		private readonly Func<TKey, TValue> initialzier_;
		public HashMap(Func<TKey, TValue> initialzier)
			: base()
		{
			initialzier_ = initialzier;
		}

		public HashMap(Func<TKey, TValue> initialzier, int capacity)
			: base(capacity)
		{
			initialzier_ = initialzier;
		}

		new public TValue this[TKey key]
		{
			get
			{
				if (ContainsKey(key) == false) {
					base[key] = initialzier_(key);
				}

				return base[key];
			}

			set { base[key] = value; }
		}
	}

	public struct ModInt
	{
		//public const long P = 1000000007;
		public const long P = 998244353;
		public const long ROOT = 3;

		// (924844033, 5)
		// (998244353, 3)
		// (1012924417, 5)
		// (167772161, 3)
		// (469762049, 3)
		// (1224736769, 3)

		public static ModInt Inverse(ModInt value) => Pow(value, P - 2);
		public static ModInt Pow(ModInt value, long k) => Pow(value.value_, k);
		public static ModInt Pow(long value, long k)
		{
			long ret = 1;
			for (k %= P - 1; k > 0; k >>= 1, value = value * value % P) {
				if ((k & 1) == 1) {
					ret = ret * value % P;
				}
			}
			return new ModInt(ret);
		}

		private long value_;

		public ModInt(long value)
			=> value_ = value;
		public ModInt(long value, bool mods)
		{
			if (mods) {
				value %= P;
				if (value < 0) {
					value += P;
				}
			}

			value_ = value;
		}

		public static ModInt operator +(ModInt lhs, ModInt rhs)
		{
			lhs.value_ = (lhs.value_ + rhs.value_) % P;
			return lhs;
		}
		public static ModInt operator +(long lhs, ModInt rhs)
		{
			rhs.value_ = (lhs + rhs.value_) % P;
			return rhs;
		}
		public static ModInt operator +(ModInt lhs, long rhs)
		{
			lhs.value_ = (lhs.value_ + rhs) % P;
			return lhs;
		}

		public static ModInt operator -(ModInt lhs, ModInt rhs)
		{
			lhs.value_ = (P + lhs.value_ - rhs.value_) % P;
			return lhs;
		}
		public static ModInt operator -(long lhs, ModInt rhs)
		{
			rhs.value_ = (P + lhs - rhs.value_) % P;
			return rhs;
		}
		public static ModInt operator -(ModInt lhs, long rhs)
		{
			lhs.value_ = (P + lhs.value_ - rhs) % P;
			return lhs;
		}

		public static ModInt operator *(ModInt lhs, ModInt rhs)
		{
			lhs.value_ = lhs.value_ * rhs.value_ % P;
			return lhs;
		}
		public static ModInt operator *(long lhs, ModInt rhs)
		{
			rhs.value_ = lhs * rhs.value_ % P;
			return rhs;
		}
		public static ModInt operator *(ModInt lhs, long rhs)
		{
			lhs.value_ = lhs.value_ * rhs % P;
			return lhs;
		}

		public static ModInt operator /(ModInt lhs, ModInt rhs)
		{
			long exp = P - 2;
			while (exp > 0) {
				if (exp % 2 > 0) {
					lhs *= rhs;
				}

				rhs *= rhs;
				exp /= 2;
			}

			return lhs;
		}

		public static implicit operator ModInt(long n) => new ModInt(n, true);
		public long ToLong() => value_;
		public override string ToString() => value_.ToString();
	}

	public static class Helper
	{
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void UpdateMin<T>(ref T target, T value) where T : IComparable<T>
			=> target = target.CompareTo(value) > 0 ? value : target;
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void UpdateMin<T>(ref T target, T value, Action<T> onUpdated) where T : IComparable<T>
		{
			if (target.CompareTo(value) > 0) {
				target = value;
				onUpdated(value);
			}
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void UpdateMax<T>(ref T target, T value) where T : IComparable<T>
			=> target = target.CompareTo(value) < 0 ? value : target;
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void UpdateMax<T>(ref T target, T value, Action<T> onUpdated) where T : IComparable<T>
		{
			if (target.CompareTo(value) < 0) {
				target = value;
				onUpdated(value);
			}
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[] Array<T>(int n, Func<int, T> init)
			=> Enumerable.Range(0, n).Select(x => init(x)).ToArray();
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static List<T> List<T>(int n, Func<int, T> init)
			=> Enumerable.Range(0, n).Select(x => init(x)).ToList();

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[,] Array2<T>(int n, int m, T init)
			where T : struct
		{
			var array = new T[n, m];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					array[i, j] = init;
				}
			}

			return array;
		}
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[,] Array2<T>(int n, int m, Func<int, int, T> initializer)
		{
			var array = new T[n, m];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					array[i, j] = initializer(i, j);
				}
			}

			return array;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[,,] Array3<T>(int n1, int n2, int n3, T init)
			where T : struct
		{
			var array = new T[n1, n2, n3];
			for (int i1 = 0; i1 < n1; i1++) {
				for (int i2 = 0; i2 < n2; i2++) {
					for (int i3 = 0; i3 < n3; i3++) {
						array[i1, i2, i3] = init;
					}
				}
			}

			return array;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[,,,] Array4<T>(int n1, int n2, int n3, int n4, T init)
			where T : struct
		{
			var array = new T[n1, n2, n3, n4];
			for (int i1 = 0; i1 < n1; i1++) {
				for (int i2 = 0; i2 < n2; i2++) {
					for (int i3 = 0; i3 < n3; i3++) {
						for (int i4 = 0; i4 < n4; i4++) {
							array[i1, i2, i3, i4] = init;
						}
					}
				}
			}

			return array;
		}

		private static readonly int[] delta4_ = { 1, 0, -1, 0, 1 };
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void DoAt4(int i, int j, int imax, int jmax, Action<int, int> action)
		{
			for (int dn = 0; dn < 4; dn++) {
				int d4i = i + delta4_[dn];
				int d4j = j + delta4_[dn + 1];
				if ((uint)d4i < (uint)imax && (uint)d4j < (uint)jmax) {
					action(d4i, d4j);
				}
			}
		}

		private static readonly int[] delta8_ = { 1, 0, -1, 0, 1, 1, -1, -1, 1 };
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void DoAt8(int i, int j, int imax, int jmax, Action<int, int> action)
		{
			for (int dn = 0; dn < 8; dn++) {
				int d8i = i + delta8_[dn];
				int d8j = j + delta8_[dn + 1];
				if ((uint)d8i < (uint)imax && (uint)d8j < (uint)jmax) {
					action(d8i, d8j);
				}
			}
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void ForEachSubBit(int bit, Action<int> action)
		{
			for (int sub = bit; sub >= 0; sub--) {
				sub &= bit;
				action(sub);
			}
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static string Reverse(string src)
		{
			var chars = src.ToCharArray();
			for (int i = 0, j = chars.Length - 1; i < j; i++, j--) {
				var tmp = chars[i];
				chars[i] = chars[j];
				chars[j] = tmp;
			}
			return new string(chars);
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static string Join<T>(this IEnumerable<T> values, string separator = "")
			=> string.Join(separator, values);
	}

	public class Scanner : IDisposable
	{
		private const int BUFFER_SIZE = 1024;
		private const int ASCII_CHAR_BEGIN = 33;
		private const int ASCII_CHAR_END = 126;
		private readonly string filePath_;
		private readonly Stream stream_;
		private readonly byte[] buf_ = new byte[BUFFER_SIZE];
		private int length_ = 0;
		private int index_ = 0;
		private bool isEof_ = false;

		public Scanner(string file = "")
		{
			if (string.IsNullOrWhiteSpace(file)) {
				stream_ = Console.OpenStandardInput();
			} else {
				filePath_ = file;
				stream_ = new FileStream(file, FileMode.Open);
			}

			Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) {
				AutoFlush = false
			});
		}

		public void Dispose()
		{
			Console.Out.Flush();
			stream_.Dispose();
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public char Char()
		{
			byte b;
			do {
				b = Read();
			} while (b < ASCII_CHAR_BEGIN || ASCII_CHAR_END < b);

			return (char)b;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public string Next()
		{
			var sb = new StringBuilder();
			for (var b = Char(); b >= ASCII_CHAR_BEGIN && b <= ASCII_CHAR_END; b = (char)Read()) {
				sb.Append(b);
			}

			return sb.ToString();
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public int Int() => (int)Long();
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public int Int(int offset) => Int() + offset;
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (int, int) Int2(int offset = 0)
			=> (Int(offset), Int(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (int, int, int) Int3(int offset = 0)
			=> (Int(offset), Int(offset), Int(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (int, int, int, int) Int4(int offset = 0)
			=> (Int(offset), Int(offset), Int(offset), Int(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public int[] ArrayInt(int length, int offset = 0)
		{
			int[] Array = new int[length];
			for (int i = 0; i < length; i++) {
				Array[i] = Int(offset);
			}
			return Array;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public long Long()
		{
			long ret = 0;
			byte b;
			bool ng = false;
			do {
				b = Read();
			} while (b != '-' && (b < '0' || '9' < b));

			if (b == '-') {
				ng = true;
				b = Read();
			}

			for (; true; b = Read()) {
				if (b < '0' || '9' < b) {
					return ng ? -ret : ret;
				} else {
					ret = ret * 10 + b - '0';
				}
			}
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public long Long(long offset) => Long() + offset;
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (long, long) Long2(long offset = 0)
			=> (Long(offset), Long(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (long, long, long) Long3(long offset = 0)
			=> (Long(offset), Long(offset), Long(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (long, long, long, long) Long4(long offset = 0)
			=> (Long(offset), Long(offset), Long(offset), Long(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public long[] ArrayLong(int length, long offset = 0)
		{
			long[] Array = new long[length];
			for (int i = 0; i < length; i++) {
				Array[i] = Long(offset);
			}
			return Array;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public double Double() => double.Parse(Next(), CultureInfo.InvariantCulture);
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public double Double(double offset) => Double() + offset;
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (double, double) Double2(double offset = 0)
			=> (Double(offset), Double(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (double, double, double) Double3(double offset = 0)
			=> (Double(offset), Double(offset), Double(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (double, double, double, double) Double4(double offset = 0)
			=> (Double(offset), Double(offset), Double(offset), Double(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public double[] ArrayDouble(int length, double offset = 0)
		{
			double[] Array = new double[length];
			for (int i = 0; i < length; i++) {
				Array[i] = Double(offset);
			}
			return Array;
		}

		private byte Read()
		{
			if (isEof_) {
				throw new EndOfStreamException();
			}

			if (index_ >= length_) {
				index_ = 0;
				if ((length_ = stream_.Read(buf_, 0, BUFFER_SIZE)) <= 0) {
					isEof_ = true;
					return 0;
				}
			}

			return buf_[index_++];
		}

		public void Save(string text)
		{
			if (string.IsNullOrWhiteSpace(filePath_)) {
				return;
			}

			File.WriteAllText(filePath_ + "_output.txt", text);
		}
	}
}


Measured memory_bytes: 38188