import std.algorithm, std.complex, std.conv, std.math, std.numeric, std.range, std.stdio;

immutable int limit = 1 << 19, half = 512;

void main ()
{
	int n, x;
	readf (" %s %s ", &n, &x);
	auto a = readln.splitter.map !(to!int).array ~ int.min;
	auto v = new long [limit];
	int pos = 0, cur = 0;
	foreach (c; a)
	{
		++cur;
		if (c < x) v[pos++] = cur, cur = 0;
	}
	auto u = v.map !(c => Complex!real (c % half, c / half)).array;
	reverse (u[0..pos]);
	auto f = new Fft (limit);
	auto g = f.fft!real (u), h = f.fft!real (v);
	g[] *= h[];
	auto w = f.inverseFft!real (g).take (n + 1).map !(c => cast (long) (c.re.round + half * c.im.round)).array;
	w[pos - 1] = 0;
	foreach (i; 0..pos) w[pos - 1] += (v[i] * (v[i] - 1) / 2);
	reverse (w[0..pos]);
	w[pos..$] = 0;
	writefln ("%(%s %)", w);
}

import std.algorithm;
import std.array;
import std.complex;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;

immutable int limit = 1 << 19;
immutable int half = 512;

void main ()
{
	int n, x;
	while (readf (" %s %s", &n, &x) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto v = new real [limit];
		v[] = 0;
		int pos = 0;
		int cur = 1;
		foreach (ref c; a)
		{
			if (c < x)
			{
				v[pos] = cur;
				pos += 1;
				cur = 1;
			}
			else
			{
				cur += 1;
			}
		}
		v[pos] = cur;
		pos += 1;
		auto u = v.map !(c => Complex !(real) (cast (long) (c) % half, cast (long) (c) / half)).array;
		reverse (u[0..pos]);
//		debug {writeln (u.take (n).map !(c => c.round.to !(long)));}
		debug {writeln (v.take (n).map !(c => c.round.to !(long)));}

		auto f = new Fft (limit);
		auto uf = f.fft !(real) (u);
		auto vf = f.fft !(real) (v);
		auto wf = uf.dup;
		wf[] *= vf[];
		auto w = f.inverseFft !(real) (wf);
		debug {writeln (w.take (n + 1).map !(c => c.re.round.to !(long)));}
		auto wr = w.take (n + 1).map !(c => c.re.round.to !(long) + half * c.im.round.to !(long)).array;
		wr[pos - 1] = 0;
		foreach (i; 0..pos)
		{
			wr[pos - 1] += cast (long) (v[i] * (v[i] - 1) / 2);
		}
		reverse (wr[0..pos]);
		wr[pos..$][] = 0;
		writefln ("%(%s %)", wr);
	}
}

import std.algorithm, std.complex, std.conv, std.math, std.numeric, std.range, std.stdio;
immutable int L = 1 << 19, H = 512;
void main () {
	int n, x;
	readf (" %s %s ", &n, &x);
	auto a = readln.splitter.map !(to!int).array;
	auto v = a.splitter !(t => t < x).map !(t => t.length + 1L).array;
	auto k = v.length;
	v.length = L;
	auto u = v.map !(c => Complex!real (c % H, c / H)).array;
	reverse (u[0..k]);
	auto g = u.fft!real, h = v.fft!real;
	g[] *= h[];
	auto w = g.inverseFft!real[0..k].retro.map !(c => to!long (c.re.round + H * c.im.round)).array;
	w[0] = k.iota.map !(i => (v[i] * (v[i] - 1) / 2)).sum;
	w.length = n + 1;
	writefln ("%(%s %)", w);
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

class Fft(int K) {
  // 1, 1/4, 1/8, 3/8, 1/16, 5/16, 3/16, 7/16, ...
  Complex!real[] g;
  this() {
    static assert(K >= 2, "Fft: K >= 2 must hold");
    g.length = 1 << (K - 1);
    g[0] = 1;
    g[1 << (K - 2)] = fromPolar(1.0L, (2.0L * PI) / (1 << K));
    for (int l = 1 << (K - 2); l >= 2; l >>= 1) {
      g[l >> 1] = g[l] * g[l];
    }
    for (int l = 2; l <= 1 << (K - 2); l <<= 1) {
      for (int i = 1; i < l; ++i) {
        g[l + i] = g[l] * g[i];
      }
    }
  }
  void fft(Complex!real[] x) const {
    const n = cast(int)(x.length);
    assert(!(n & (n - 1)) && n <= 1 << K);
    for (int l = n; l >>= 1; ) {
      for (int i = 0; i < (n >> 1) / l; ++i) {
        for (int j = (i << 1) * l; j < (i << 1 | 1) * l; ++j) {
          const t = g[i] * x[j + l];
          x[j + l] = x[j] - t;
          x[j] += t;
        }
      }
    }
    for (int i = 0, j = 0; i < n; ++i) {
      if (i < j) swap(x[i], x[j]);
      for (int l = n; (l >>= 1) && !((j ^= l) & l); ) {}
    }
  }
}
enum FFT_K = 19;
Fft!FFT_K FFT;

void main() {
  FFT = new Fft!FFT_K();

  try {
    for (; ; ) {
      const N = readInt();
      const X = readInt();
      auto A = new int[N];
      foreach (i; 0 .. N) {
        A[i] = readInt();
      }

      auto ss = new int[N + 1];
      foreach (i; 0 .. N) {
        ss[i + 1] = ss[i] + ((A[i] < X) ? 1 : 0);
      }
      debug {
        writeln("ss = ", ss);
      }

      auto fs = new Complex!real[1 << FFT_K];
      auto gs = new Complex!real[1 << FFT_K];
      fs[] = complex(0.0L, 0.0L);
      gs[] = complex(0.0L, 0.0L);
      foreach (i; 0 .. N + 1) {
        fs[ss[i]] += 1.0L;
        gs[N - ss[i]] += 1.0L;
      }
      FFT.fft(fs);
      FFT.fft(gs);
      foreach (j; 0 .. 1 << FFT_K) {
        fs[j] *= gs[j] / (1 << FFT_K);
      }
      fs[1 .. $].reverse;
      FFT.fft(fs);
      auto ans = new long[N + 1];
      foreach (k; 1 .. N + 1) {
        ans[k] = cast(long)(fs[N + k].re + 0.5L);
      }
      ans[0] = 1L * N * (N + 1) / 2 - ans[1 .. N + 1].sum;

      foreach (k; 0 .. N + 1) {
        if (k > 0) write(" ");
        write(ans[k]);
      }
      writeln();
    }
  } catch (EOFException e) {
  }
}

import std.algorithm;
import std.array;
import std.complex : g = abs;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;

immutable int limit = 1 << 20;

void main ()
{
	int n, x;
	while (readf (" %s %s", &n, &x) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto v = new real [limit];
		v[] = 0;
		int pos = 0;
		int cur = 1;
		foreach (ref c; a)
		{
			if (c < x)
			{
				v[pos] = cur;
				pos += 1;
				cur = 1;
			}
			else
			{
				cur += 1;
			}
		}
		v[pos] = cur;
		pos += 1;
		auto u = v.dup;
		reverse (u[0..pos]);
		debug {writeln (u.take (n).map !(c => c.round.to !(long)));}
		debug {writeln (v.take (n).map !(c => c.round.to !(long)));}

		auto f = new Fft (limit);
		auto uf = f.fft !(real) (u);
		auto vf = f.fft !(real) (v);
		auto wf = uf.dup;
		wf[] *= vf[];
		auto w = f.inverseFft !(real) (wf);
		debug {writeln (w.take (n + 1).map !(c => g (c).round.to !(long)));}
		auto wr = w.take (n + 1).map !(c => g (c).round.to !(long)).array;
		wr[pos - 1] = 0;
		foreach (i; 0..pos)
		{
			wr[pos - 1] += cast (long) (v[i] * (v[i] - 1) / 2);
		}
		reverse (wr[0..pos]);
		wr[pos..$][] = 0;
		writefln ("%(%s %)", wr);
	}
}

import std.algorithm, std.complex, std.conv, std.math, std.numeric, std.range, std.stdio;
immutable int L = 1 << 19, H = 512;
void main () {
	int n, x;
	readf (" %s %s ", &n, &x);
	auto a = readln.splitter.map !(to!int).array;
	auto v = a.splitter !(t => t < x).map !(t => t.length + 1).array;
	auto k = v.length;
	v.length = L;
	auto u = v.map !(c => Complex!real (c % H, c / H)).array;
	reverse (u[0..k]);
	auto g = u.fft!real, h = v.fft!real;
	g[] *= h[];
	auto w = g.inverseFft!real[0..k].retro.map !(c => to!long (c.re.round + H * c.im.round)).array;
	w[0] = k.iota.map !(i => (v[i] * (v[i] - 1) / 2)).sum;
	w.length = n + 1;
	writefln ("%(%s %)", w);
}

import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;

immutable int limit = 1 << 19;

void main ()
{
	int n, x;
	while (readf (" %s %s", &n, &x) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto v = new real [limit];
		v[] = 0;
		int pos = 0;
		int cur = 1;
		foreach (ref c; a)
		{
			if (c < x)
			{
				v[pos] = cur;
				pos += 1;
				cur = 1;
			}
			else
			{
				cur += 1;
			}
		}
		v[pos] = cur;
		pos += 1;
		auto u = v.dup;
		reverse (u[0..pos]);
		debug {writeln (u.take (n).map !(c => c.round.to !(long)));}
		debug {writeln (v.take (n).map !(c => c.round.to !(long)));}

		auto f = new Fft (limit);
		auto uf = f.fft !(real) (u);
		auto vf = f.fft !(real) (v);
		auto wf = uf.dup;
		wf[] *= vf[];
		auto w = f.inverseFft !(real) (wf);
		debug {writeln (w.take (n + 1).map !(c => c.re.round.to !(long)));}
		auto wr = w.take (n + 1).map !(c => c.re.round.to !(long)).array;
		wr[pos - 1] = 0;
		foreach (i; 0..pos)
		{
			wr[pos - 1] += cast (long) (v[i] * (v[i] - 1) / 2);
		}
		reverse (wr[0..pos]);
		wr[pos..$][] = 0;
		writefln ("%(%s %)", wr);
	}
}


import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import core.bitop;

immutable int MOD = 1_000_000_007;
immutable int MAX_N = 1003;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = new byte [MAX_N * MAX_N];
		foreach (i; 0..n)
		{
			auto t = readln;
			foreach (j; 0..n)
			{
				a[(i + 1) * MAX_N + (j + 1)] =
				    cast (byte) (t[j] - '0');
			}
		}
		debug {writeln (a.chunks (MAX_N));}
		real resR = 0;
		long res = 0;
		foreach (i; 1..n + 1)
		{
			foreach (j; 1..n + 1)
			{
				void fun (int d1, int d2, int d3, int d4) ()
				{
					auto p1 = &a[i * MAX_N + j];
					auto p2 = &a[i * MAX_N + j];
					auto p3 = &a[i * MAX_N + j];
					auto p4 = &a[i * MAX_N + j];
					real curR = *p1;
					long cur = *p1;

					if (curR)
					{
						for (int step = 0; ; step++)
						{
							p1 += d1;
							p2 += d2;
							p3 += d3;
							p4 += d4;
							int m = *p1 * *p2 *
							    *p3 * *p4;
							if (!m)
							{
								break;
							}
							curR *= m;
							cur *= m;
							if (!(step & 3))
							{
								cur %= MOD;
							}
						}
					}
					if (resR < curR)
					{
						resR = curR;
						res = cur;
					}
				}

				fun !(-MAX_N - 1, -MAX_N + 1,
				    +MAX_N - 1, +MAX_N + 1) ();
				fun !(-MAX_N, -1, +1, +MAX_N) ();
			}
		}

		debug {writeln (resR);}
		writeln (res % MOD);
	}
}

import std.stdio;

immutable int MOD = 1_000_000_007;
immutable int MAX_N = 1003;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = new byte [MAX_N * MAX_N];
		foreach (i; 0..n)
		{
			auto t = readln;
			foreach (j; 0..n)
			{
				a[(i + 1) * MAX_N + (j + 1)] =
				    cast (byte) (t[j] - '0');
			}
		}
		real resR = 0;
		long res = 0;
		foreach (i; 1..n + 1)
		{
			foreach (j; 1..n + 1)
			{
				void fun (int d1, int d2, int d3, int d4) ()
				{
					auto p1 = &a[i * MAX_N + j];
					auto p2 = &a[i * MAX_N + j];
					auto p3 = &a[i * MAX_N + j];
					auto p4 = &a[i * MAX_N + j];
					real curR = *p1;
					long cur = *p1;

					if (curR)
					{
						for (int step = 0; ; step++)
						{
							p1 += d1;
							p2 += d2;
							p3 += d3;
							p4 += d4;
							int m = *p1 * *p2 *
							    *p3 * *p4;
							if (!m)
							{
								break;
							}
							curR *= m;
							cur *= m;
							if (!(step & 3))
							{
								cur %= MOD;
							}
						}
					}
					if (resR < curR)
					{
						resR = curR;
						res = cur;
					}
				}

				fun !(-MAX_N - 1, -MAX_N + 1,
				    +MAX_N - 1, +MAX_N + 1) ();
				fun !(-MAX_N, -1, +1, +MAX_N) ();
			}
		}
		writeln (res % MOD);
	}
}

import std.stdio;
immutable int MOD = 10 ^^ 9 + 7, MAX_N = 1002;
void main () {
	int n;
	while (readf (" %s ", &n) > 0) {
		auto a = new byte [MAX_N * MAX_N];
		foreach (i; 1..n + 1) {
			auto t = readln;
			foreach (j; 1..n + 1) a[i * MAX_N + j] = cast (byte) (t[j - 1] - '0');
		}
		real resR = 0; long res = 0;
		void fun (int d1, int d2, int d3, int d4) (int i, int j) {
			auto p1 = &a[i * MAX_N + j], p2 = p1, p3 = p1, p4 = p1;
			real curR = *p1; long cur = *p1;
			if (curR) for (int step = 0; ; step++) {
				p1 += d1; p2 += d2; p3 += d3; p4 += d4;
				int m = *p1 * *p2 * *p3 * *p4;
				if (!m) break;
				curR *= m; cur *= m;
				if (!(step & 3)) cur %= MOD;
			}
			if (resR < curR) {resR = curR; res = cur;}
		}
		foreach (i; 1..n + 1)
			foreach (j; 1..n + 1) {
				fun !(-MAX_N - 1, -MAX_N + 1, +MAX_N - 1, +MAX_N + 1) (i, j);
				fun !(-MAX_N, -1, +1, +MAX_N) (i, j);
			}
		writeln (res % MOD);
	}
}


import std.algorithm, std.conv, std.random, std.range, std.stdio, std.string, std.typecons;
void main () {
	auto n = readln.strip.to !(int), a = readln.splitter.map !(to !(long)).array;
	int [long] d;
	foreach (s; 0..30) {
		auto y = a[uniform (0, n)];
		foreach (z; [y - 1, y, y + 1]) {
			for (long c = 2; c * c <= z; c++) if (z % c == 0) {
				d[c] = 1;
				while (z % c == 0) z /= c;
			}
			if (z > 1) d[z] = 1;
		}
	}
	long r = n;
	foreach (k; d.byKey) r = min (r, a.map !(z => z < k ? k - z : min (z % k, k - z % k)).sum);
	r.writeln;
}

import std.algorithm;
import std.conv;
import std.random;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

immutable int limit = 10 ^^ 6;
immutable int steps = 10 ^^ 3;
immutable int divs  = 10 ^^ 3 * 2;
immutable int first = 10 ^^ 2 * 2;

int [] p;

void prepare ()
{
	auto s = new bool [limit];
	s[] = true;
	s[0] = false;
	s[1] = false;
	p = [];
	p.reserve (limit / 12);
	for (int v = 2; v < limit; v++)
	{
		if (s[v])
		{
			p ~= v;
			if (v * 1L * v < limit)
			{
				for (int u = v * v; u < limit; u += v)
				{
					s[u] = false;
				}
			}
		}
	}
}

void main ()
{
	rndGen.seed (236426);
	prepare ();

	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(long)).array;
		sort (a);
		bool [long] d;
		foreach (x; p[0..first])
		{
			d[x] = true;
		}
		for (int step = 0; step < steps && d.length < divs; step++)
		{
			auto z0 = a[uniform (0, n)];
			foreach (z; [z0 - 1, z0, z0 + 1])
			{
				foreach (const c; p)
				{
					if (c * 1L * c > z)
					{
						break;
					}
					if (z % c == 0)
					{
						d[c] = true;
						do
						{
							z /= c;
						}
						while (z % c == 0);
					}
				}
				if (z > 1)
				{
					d[z] = true;
				}
			}
		}

		long res = n;
		auto e = d.byKey.array;
		sort (e);
		debug {writeln (e);}
		foreach (const k; e)
		{
			long cur = 0;
			foreach (const z; a)
			{
				if (z < k)
				{
					cur += k - z;
				}
				else
				{
					long rem = z % k;
					cur += min (rem, k - rem);
				}
				if (cur >= res)
				{
					break;
				}
			}
			debug {writeln (k, ": ", cur);}
			res = min (res, cur);
		}
		writeln (res);
	}
}

// Code By H~$~C

module main;

import core.stdc.stdio, core.stdc.stdlib, core.stdc.string;
import std.stdio, std.array, std.string, std.math;
import std.algorithm, std.range, std.random;
import std.conv, std.typecons;
alias to!(string) to_string;
alias to!(int) to_int;
alias to!(long) to_long;
alias to!(double) to_double;
immutable int inf = 0x3f3f3f3f;
immutable long lnf = 0x3f3f3f3f3f3f3f3f;
immutable typeof(null) NULL = null;

int n;
long[] arr;

void main() {
  n = readln.strip.to_int;
  arr = randomShuffle(readln.splitter.map!(to_long).array, rndGen);
  int[long] mp;
  foreach(i; 0 .. min(30, to_int(arr.length))) {
    foreach(x; [arr[i] - 1, arr[i], arr[i] + 1]) {
      for(long p = 2; p * p <= x; p++) {
        if (x % p == 0) {
          mp[p] = 1;
          while (x % p == 0) x /= p;
        }
      }
      if (x > 1) mp[x] = 1;
    }
  }
  long ans = n;
  foreach(k; mp.byKey) {
    ans = min(ans, arr.map!(x => x < k ? k - x : min(x % k, k - x % k)).sum);
  }
  ans.writeln;
}

import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;
import std.random;
import std.functional;
struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    if (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}
Input input;
void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t; get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}

alias Z = long;
alias N = long;

N[N] factorize(Z n)
{
  N[N] res;
  for(N d = 2; d * d <= n; d++)
    while (n % d == 0)
      res[d]++, n /= d;
  if (n != 1)
    res[n]++;
  return res;
}

N pmod(N x, N m)
{
  return (x%m+m)%m;
}

void main()
{
  N n; get(n);
  auto a = new N[cast(size_t)(n)]; get(a);
  a = a.randomShuffle;
  N minops = n;
  N minOps(N m)
  {
    if (m == 0)
      return N.max;
    N minOpsPrime(N p)
    {
      N ops = 0;
      foreach(e; a)
	ops += min((e > pmod(e, p))? pmod(e, p) : N.max, pmod(-e, p));
      return ops;
    }
    auto factorization = m.factorize;
    N ops = n;
    foreach(prime, order; factorization)
	ops = min(ops, memoize!minOpsPrime(prime));
    return ops;
  }
  foreach(e; a[0 .. min(18, cast(size_t)(n))])
    foreach(t; [e - 1, e, e + 1])
      minops = min(minops, memoize!minOps(t));
  ans(minops);
}

import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;
import std.random;
import std.functional;
struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    while (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}
Input input;
void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t; get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}

alias Z = long;
alias N = long;

N[] factorize(Z n)
{
  N[] res;
  for(N d = 2; d * d <= n; d++)
    if (n % d == 0)
      {
	res ~= d;
	n /= d;
	while (n % d == 0)
	  n /= d;
      }
  if (n != 1)
    res ~= n;
  return res;
}

N pmod(N x, N m)
{
  return (x%m+m)%m;
}

void main()
{
  N n; get(n);
  auto a = new N[cast(size_t)(n)]; get(a);
  a = a.randomShuffle;
  N minOpsPrime(N p)
  {
    return a.map!(e => min((e > pmod(e, p))? pmod(e, p) : N.max, pmod(-e, p))).sum;
  }
  N minOps(N m)
  {
    return m == 0? N.max : m.factorize.map!(memoize!minOpsPrime).fold!min(N.max);
  }
  ans(a[0..min(18, cast(size_t)(n))]
      .map!(e => [e - 1, e, e + 1]
	    .map!(memoize!minOps)
	    .fold!min(N.max))
      .fold!min(N.max));
}

import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;
import std.random;
import std.functional;
struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    if (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}
Input input;
void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t; get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}

alias Z = long;
alias N = long;

N[] factorize(Z n)
{
  N[] res;
  for(N d = 2; d * d <= n; d++)
    if(n % d == 0)
      {
	res ~= d;
	while (n % d == 0)
	  n /= d;
      }
  if (n != 1)
    res ~= n;
  return res;
}

N pmod(N x, N m)
{
  return (x%m+m)%m;
}

void main()
{
  N n; get(n);
  auto a = new N[cast(size_t)(n)]; get(a);
  a = a.randomShuffle;
  N minops = n;
  N minOps(N m)
  {
    if (m == 0)
      return N.max;
    N minOpsPrime(N p)
    {
      N ops = 0;
      foreach(e; a)
	ops += min((e > pmod(e, p))? pmod(e, p) : N.max, pmod(-e, p));
      return ops;
    }
    auto factorization = m.factorize;
    N ops = n;
    foreach(prime; factorization)
	ops = min(ops, memoize!minOpsPrime(prime));
    return ops;
  }
  foreach(e; a[0 .. min(18, cast(size_t)(n))])
    foreach(t; [e - 1, e, e + 1])
      minops = min(minops, memoize!minOps(t));
  ans(minops);
}

import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;
import std.random;
import std.functional;
struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    if (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}
Input input;
void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t; get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}

alias Z = long;
alias N = long;

N[] factorize(Z n)
{
  N[] res;
  for(N d = 2; d * d <= n; d++)
    if (n % d == 0)
      {
	res ~= d;
	n /= d;
	while (n % d == 0)
	  n /= d;
      }
  if (n != 1)
    res ~= n;
  return res;
}

N pmod(N x, N m)
{
  return (x%m+m)%m;
}

void main()
{
  N n; get(n);
  auto a = new N[cast(size_t)(n)]; get(a);
  a = a.randomShuffle;
  N minops = n;

  N minOpsElem(N e, N p)
  {
    return min((e > pmod(e, p))? pmod(e, p) : N.max, pmod(-e, p));
  }
  N minOpsPrime(N p)
  {
    return a.map!(e => minOpsElem(e, p)).sum;
  }
  N minOps(N m)
  {
    return m == 0? N.max : m.factorize.map!(memoize!minOpsPrime).fold!min(N.max);
  }
  foreach(e; a[0 .. min(18, cast(size_t)(n))])
    foreach(t; [e - 1, e, e + 1])
      minops = min(minops, memoize!minOps(t));
  ans(minops);
}

import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;
import std.random;
struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    if (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}
Input input;
void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t; get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}

alias Z = long;
alias N = long;

N[N] factorize(Z n)
{
  N[N] res;
  for(N d = 2; d * d <= n; d++)
    while (n % d == 0)
      res[d]++, n /= d;
  if (n != 1)
    res[n]++;
  return res;
}

N pmod(N x, N m)
{
  return (x%m+m)%m;
}

void main()
{
  N n; get(n);
  auto a = new N[cast(size_t)(n)]; get(a);
  a = a.randomShuffle;
  N minops = n;
  N minOps(N m)
  {
    if (m == 0)
      return N.max;
    N minOpsPrime(N p)
    {
      bool shallPrint = p == 3;
      N ops = 0;
      foreach(e; a)
	  ops += min(pmod(e, p) > e? pmod(e, p) : N.max, pmod(-e, p));
      return ops;
    }
    auto factorization = m.factorize;
    N ops = n;
    foreach(prime, order; factorization)
	ops = min(ops, minOpsPrime(prime));
    return ops;
  }
  foreach(e; a[0 .. min(40, cast(size_t)(n))])
    foreach(t; [e - 1, e, e + 2])
      minops = min(minops, minOps(e));
  ans(minops);
}

import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;
import std.random;
struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    if (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}
Input input;
void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t; get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}

alias Z = long;
alias N = long;

N[N] factorize(Z n)
{
  N[N] res;
  for(N d = 2; d * d <= n; d++)
    while (n % d == 0)
      res[d]++, n /= d;
  if (n != 1)
    res[n]++;
  return res;
}

N pmod(N x, N m)
{
  return (x%m+m)%m;
}

void main()
{
  N n; get(n);
  auto a = new N[cast(size_t)(n)]; get(a);
  a = a.randomShuffle;
  N minops = n;
  N minOps(N m)
  {
    if (m == 0)
      return N.max;
    N minOpsPrime(N p)
    {
      bool shallPrint = p == 3;
      N ops = 0;
      foreach(e; a)
	  ops += min(pmod(e, p) > e? pmod(e, p) : N.max, pmod(-e, p));
      return ops;
    }
    auto factorization = m.factorize;
    N ops = n;
    foreach(prime, order; factorization)
	ops = min(ops, minOpsPrime(prime));
    return ops;
  }
  foreach(e; a[0 .. min(40, cast(size_t)(n))])
    foreach(t; [e - 1, e, e + 1])
      minops = min(minops, minOps(e));
  ans(minops);
}

import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;
import std.random;
struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    if (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}
Input input;
void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t; get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}

alias Z = long;
alias N = long;

N[N] factorize(Z n)
{
  N[N] res;
  for(N d = 2; d * d <= n; d++)
    while (n % d == 0)
      res[d]++, n /= d;
  if (n != 1)
    res[n]++;
  return res;
}

N pmod(N x, N m)
{
  return (x%m+m)%m;
}

void main()
{
  N n; get(n);
  auto a = new N[cast(size_t)(n)]; get(a);
  a = a.randomShuffle;
  N minops = n;
  N minOps(N m)
  {
    if (m == 0)
      return N.max;
    N minOpsPrime(N p)
    {
      bool shallPrint = p == 3;
      N ops = 0;
      foreach(e; a)
	  ops += min(pmod(e, p) > e? pmod(e, p) : N.max, pmod(-e, p));
      return ops;
    }
    auto factorization = m.factorize;
    N ops = n;
    foreach(prime, order; factorization)
	ops = min(ops, minOpsPrime(prime));
    return ops;
  }
  foreach(e; a[0 .. min(40, cast(size_t)(n))])
    foreach(t; [e - 1, e, e + 1])
      minops = min(minops, minOps(t));
  ans(minops);
}

import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;
import std.random;
import std.functional;
struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    if (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}
Input input;
void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t; get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}

alias Z = long;
alias N = long;

N[] factorize(Z n)
{
  N[] res;
  for(N d = 2; d * d <= n; d++)
    if (n % d)
      {
	res ~= d;
	n /= d;
	while (n % d == 0)
	  n /= d;
      }
  if (n != 1)
    res ~= n;
  return res;
}

N pmod(N x, N m)
{
  return (x%m+m)%m;
}

void main()
{
  N n; get(n);
  auto a = new N[cast(size_t)(n)]; get(a);
  a = a.randomShuffle;
  N minops = n;

  N minOpsElem(N e, N p)
  {
    return min((e > pmod(e, p))? pmod(e, p) : N.max, pmod(-e, p));
  }
  N minOpsPrime(N p)
  {
    return a.map!(e => minOpsElem(e, p)).sum;
  }
  N minOps(N m)
  {
    return m == 0? N.max : m.factorize.map!(memoize!minOpsPrime).fold!min(N.max);
  }
  foreach(e; a[0 .. min(18, cast(size_t)(n))])
    foreach(t; [e - 1, e, e + 1])
      minops = min(minops, memoize!minOps(t));
  ans(minops);
}

import std.algorithm;
import std.conv;
import std.random;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

immutable int limit = 10 ^^ 6;
immutable int steps = 10 ^^ 3 * 2;
immutable int divs  = 10 ^^ 3 * 4;
immutable int first = 10 ^^ 2 * 4;

int [] p;

void prepare ()
{
	auto s = new bool [limit];
	s[] = true;
	s[0] = false;
	s[1] = false;
	p = [];
	p.reserve (limit / 12);
	for (int v = 2; v < limit; v++)
	{
		if (s[v])
		{
			p ~= v;
			if (v * 1L * v < limit)
			{
				for (int u = v * v; u < limit; u += v)
				{
					s[u] = false;
				}
			}
		}
	}
}

void main ()
{
	rndGen.seed (236426);
	prepare ();

	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(long)).array;
		randomShuffle (a);
//		sort (a);
		bool [long] d;
		foreach (x; p[0..first])
		{
			d[x] = true;
		}
		for (int step = 0; step < steps && d.length < divs; step++)
		{
			auto z = a[uniform (0, n)];
			foreach (const c; p)
			{
				if (c * 1L * c > z)
				{
					break;
				}
				if (z % c == 0)
				{
					d[c] = true;
					do
					{
						z /= c;
					}
					while (z % c == 0);
				}
			}
			if (z > 1)
			{
				d[z] = true;
			}
		}

		long res = n;
		auto e = d.byKey.array;
		sort (e);
		debug {writeln (e);}
		foreach (const k; e)
		{
			long cur = 0;
			foreach (const z; a)
			{
				if (z < k)
				{
					cur += k - z;
				}
				else
				{
					long rem = z % k;
					cur += min (rem, k - rem);
				}
				if (cur >= res)
				{
					break;
				}
			}
			debug {writeln (k, ": ", cur);}
			res = min (res, cur);
		}
		writeln (res);
	}
}

import std.algorithm;
import std.conv;
import std.random;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

immutable int limit = 10 ^^ 6;
immutable int steps = 10 ^^ 3;
immutable int divs  = 10 ^^ 3 * 2;
immutable int first = 10 ^^ 2 * 2;

int [] p;

void prepare ()
{
	auto s = new bool [limit];
	s[] = true;
	s[0] = false;
	s[1] = false;
	p = [];
	p.reserve (limit / 12);
	for (int v = 2; v < limit; v++)
	{
		if (s[v])
		{
			p ~= v;
			if (v * 1L * v < limit)
			{
				for (int u = v * v; u < limit; u += v)
				{
					s[u] = false;
				}
			}
		}
	}
}

void main ()
{
	rndGen.seed (236426);
	prepare ();

	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(long)).array;
		sort (a);
		bool [long] d;
		foreach (x; p[0..first])
		{
			d[x] = true;
		}
		for (int step = 0; step < steps && d.length < divs; step++)
		{
			auto z = a[uniform (0, n)];
			foreach (const c; p)
			{
				if (c * 1L * c > z)
				{
					break;
				}
				if (z % c == 0)
				{
					d[c] = true;
					do
					{
						z /= c;
					}
					while (z % c == 0);
				}
			}
			if (z > 1)
			{
				d[z] = true;
			}
		}

		long res = n;
		auto e = d.byKey.array;
		sort (e);
		debug {writeln (e);}
		foreach (const k; e)
		{
			long cur = 0;
			foreach (const z; a)
			{
				if (z < k)
				{
					cur += k - z;
				}
				else
				{
					long rem = z % k;
					cur += min (rem, k - rem);
				}
				if (cur >= res)
				{
					break;
				}
			}
			debug {writeln (k, ": ", cur);}
			res = min (res, cur);
		}
		writeln (res);
	}
}

import std.algorithm;
import std.conv;
import std.random;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

immutable int limit = 10 ^^ 6;
immutable int steps = 10 ^^ 3 * 2;
immutable int divs  = 10 ^^ 3 * 6;
immutable int first = 10 ^^ 2 * 9;

int [] p;

void prepare ()
{
	auto s = new bool [limit];
	s[] = true;
	s[0] = false;
	s[1] = false;
	p = [];
	p.reserve (limit / 12);
	for (int v = 2; v < limit; v++)
	{
		if (s[v])
		{
			p ~= v;
			if (v * 1L * v < limit)
			{
				for (int u = v * v; u < limit; u += v)
				{
					s[u] = false;
				}
			}
		}
	}
}

void main ()
{
	rndGen.seed (236426);
	prepare ();

	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(long)).array;
//		randomShuffle (a);
		sort (a);
		bool [long] d;
		foreach (x; p[0..first])
		{
			d[x] = true;
		}
		for (int step = 0; step < steps && d.length < divs; step++)
		{
			auto z = a[uniform (0, n)];
			foreach (const c; p)
			{
				if (c * 1L * c > z)
				{
					break;
				}
				if (z % c == 0)
				{
					d[c] = true;
					do
					{
						z /= c;
					}
					while (z % c == 0);
				}
			}
			if (z > 1)
			{
				d[z] = true;
			}
		}

		long res = n;
		auto e = d.byKey.array;
		sort (e);
		debug {writeln (e);}
		foreach (const k; e)
		{
			long cur = 0;
			foreach (const z; a)
			{
				if (z < k)
				{
					cur += k - z;
				}
				else
				{
					long rem = z % k;
					cur += min (rem, k - rem);
				}
				if (cur >= res)
				{
					break;
				}
			}
			debug {writeln (k, ": ", cur);}
			res = min (res, cur);
		}
		writeln (res);
	}
}

import std.algorithm;
import std.conv;
import std.random;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

immutable int limit = 10 ^^ 6;
immutable int steps = 10 ^^ 3 * 2;
immutable int divs  = 10 ^^ 3 * 5;
immutable int first = 10 ^^ 2 * 8;

int [] p;

void prepare ()
{
	auto s = new bool [limit];
	s[] = true;
	s[0] = false;
	s[1] = false;
	p = [];
	p.reserve (limit / 12);
	for (int v = 2; v < limit; v++)
	{
		if (s[v])
		{
			p ~= v;
			if (v * 1L * v < limit)
			{
				for (int u = v * v; u < limit; u += v)
				{
					s[u] = false;
				}
			}
		}
	}
}

void main ()
{
	rndGen.seed (1236426);
	prepare ();

	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(long)).array;
//		randomShuffle (a);
		sort (a);
		bool [long] d;
		foreach (x; p[0..first])
		{
			d[x] = true;
		}
		for (int step = 0; step < steps && d.length < divs; step++)
		{
			auto z = a[uniform (0, n)];
			foreach (const c; p)
			{
				if (c * 1L * c > z)
				{
					break;
				}
				if (z % c == 0)
				{
					d[c] = true;
					do
					{
						z /= c;
					}
					while (z % c == 0);
				}
			}
			if (z > 1)
			{
				d[z] = true;
			}
		}

		long res = n;
		auto e = d.byKey.array;
		sort (e);
		debug {writeln (e);}
		foreach (const k; e)
		{
			long cur = 0;
			foreach (const z; a)
			{
				if (z < k)
				{
					cur += k - z;
				}
				else
				{
					long rem = z % k;
					cur += min (rem, k - rem);
				}
				if (cur >= res)
				{
					break;
				}
			}
			debug {writeln (k, ": ", cur);}
			res = min (res, cur);
		}
		writeln (res);
	}
}

import std.algorithm;
import std.conv;
import std.random;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

immutable int limit = 10 ^^ 6;
immutable int steps = 10 ^^ 3 * 2;
immutable int divs  = 10 ^^ 3 * 4;
immutable int first = 10 ^^ 2 * 4;

int [] p;

void prepare ()
{
	auto s = new bool [limit];
	s[] = true;
	s[0] = false;
	s[1] = false;
	p = [];
	p.reserve (limit / 12);
	for (int v = 2; v < limit; v++)
	{
		if (s[v])
		{
			p ~= v;
			if (v * 1L * v < limit)
			{
				for (int u = v * v; u < limit; u += v)
				{
					s[u] = false;
				}
			}
		}
	}
}

void main ()
{
	rndGen.seed (236426);
	prepare ();

	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(long)).array;
		sort (a);
		bool [long] d;
		foreach (x; p[0..first])
		{
			d[x] = true;
		}
		for (int step = 0; step < steps && d.length < divs; step++)
		{
			auto z = a[uniform (0, n)];
			foreach (const c; p)
			{
				if (c * 1L * c > z)
				{
					break;
				}
				if (z % c == 0)
				{
					d[c] = true;
					do
					{
						z /= c;
					}
					while (z % c == 0);
				}
			}
			if (z > 1)
			{
				d[z] = true;
			}
		}

		long res = n;
		auto e = d.byKey.array;
		sort (e);
		debug {writeln (e);}
		foreach (const k; e)
		{
			long cur = 0;
			foreach (const z; a)
			{
				if (z < k)
				{
					cur += k - z;
				}
				else
				{
					long rem = z % k;
					cur += min (rem, k - rem);
				}
				if (cur >= res)
				{
					break;
				}
			}
			debug {writeln (k, ": ", cur);}
			res = min (res, cur);
		}
		writeln (res);
	}
}


import core.bitop;
import core.checkedint;
import core.simd;
import core.stdc.stdlib;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.format;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

__gshared:

bool read(T...)(T ptrs) if (ptrs.length > 0) {
    return readf(' ' ~ replicate("%s ", ptrs.length), ptrs) == ptrs.length;
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

struct Dsu(size_t n) {
    int[n] p;

    int get(int x) {
        return x == p[x]? x : (p[x] = get(p[x]));
    }

    void merge(int x, int y) {
        p[get(x)] = get(y);
    }
}

alias Girl = Tuple!(int, `weight`, int, `beauty`);

struct Group {
    int id;
    Girl[ ] girls;
    Girl sum;
    Group* next;
}

int n;
Girl[1000] _girls;
Girl[ ] girls;
Dsu!1000 dsu;
Group[1000] groups;
int[1001][1000] dp;

int dyn(int gid, int wcap) {
    assert(gid < n);
    assert(wcap >= 0);
    if (~dp[gid][wcap])
        return dp[gid][wcap];
    const grp = &groups[gid];
    assert(!grp.girls.empty);
    int result = 0;
    if (grp.next is null) {
        if (wcap >= grp.sum.weight)
            result = grp.sum.beauty;
        else
            foreach (g; grp.girls)
                if (wcap >= g.weight)
                    result = max(result, g.beauty);
    } else {
        assert(grp.next == &groups[grp.next.id]);
        result = dyn(grp.next.id, wcap);
        if (wcap >= grp.sum.weight)
            result = max(result, dyn(grp.next.id, wcap - grp.sum.weight) + grp.sum.beauty);
        foreach (g; grp.girls)
            if (wcap >= g.weight)
                result = max(result, dyn(grp.next.id, wcap - g.weight) + g.beauty);
    }
    return (dp[gid][wcap] = result);
}

void main() {
    int m, wl;
    while (read(&n, &m, &wl)) {
        girls = _girls[0 .. n];
        foreach (int i, ref grp; groups) {
            grp.id = i;
            grp.girls = null;
            grp.sum = Girl(0, 0);
        }
        iota(0, n).copy(dsu.p[ ]);
        foreach (ref g; girls)
            read(&g.weight);
        foreach (ref g; girls)
            read(&g.beauty);
        while (m--) {
            int a, b;
            read(&a, &b);
            a--;
            b--;
            dsu.merge(a, b);
        }
        foreach (int i, g; girls) {
            auto grp = &groups[dsu.get(i)];
            grp.girls ~= g;
            grp.sum.weight += g.weight;
            grp.sum.beauty += g.beauty;
        }
        Group* last = null, first = null;
        foreach (ref grp; groups[0 .. n].filter!`!a.girls.empty`) {
            if (first is null)
                first = &grp;
            else
                last.next = &grp;
            last = &grp;
        }
        assert(last !is null);
        last.next = null;
        memset(dp.ptr, 0xFF, dp.sizeof);
        writeln(dyn(first.id, wl));
    }
}

import core.bitop, std.bitmanip;
import std.algorithm, std.functional;
import std.array, std.container;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

immutable int INF = 10L ^^ 9 + 23;

void main()
{
    int n, m, w;
    readf("%s %s %s", &n, &m, &w);
    readln;

    auto ws = readln.chomp.split.map!(to!int).array;
    auto bs = readln.chomp.split.map!(to!int).array;

    auto grpid = n.iota.array;

    foreach (_; 0 .. m) {
        int a, b;
        readf("%s %s", &a, &b);
        readln;
        --a, --b;

        int old = grpid[b];
        foreach (ref e; grpid) if (e == old) e = grpid[a];
    }

    auto vis = (false).repeat(n).array;
    auto idsForGrp = new int[][] (grpid.dup.sort.uniq.array.length);

    debug { grpid.writeln; }

    Tuple!(int, int)[] grpsVals;
    foreach (i, e; grpid) {
        if (vis[i]) continue;

        int wsm = 0, bsm = 0;
        foreach (j, f; grpid) {
            if (e != f) continue;

            idsForGrp[grpsVals.length] ~= j.to!int;
            vis[j] = true;
            wsm += ws[j];
            bsm += bs[j];
        }

        grpsVals ~= tuple(wsm, bsm);
    }

    debug { grpsVals.writeln; }
    debug { idsForGrp.writeln; }

    auto dp = new int[][] (grpsVals.length, w+1);
    foreach (i, t; grpsVals) {
        int grw = t[0], grb = t[1];

        if (i > 0) dp[i][] = dp[i-1][];

        for (int cw = w; cw - grw >= 0; --cw) {
            int prevVal = i > 0 ? dp[i-1][cw - grw] : 0;
            dp[i][cw] = max(dp[i][cw], prevVal + grb);
        }

        foreach (e; idsForGrp[i]) {
            for (int cw = w; cw - ws[e] >= 0; --cw) {
                int prevVal = i > 0 ? dp[i-1][cw - ws[e]] : 0;
                dp[i][cw] = max(dp[i][cw], prevVal + bs[e]);
            }
        }
    }

    debug { dp.writeln; }

    int ans = dp[$-1].maxElement;

    ans.writeln;
}

import std.algorithm;
import std.array;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	int n, m, tw;
	while (readf (" %s %s %s", &n, &m, &tw) > 0)
	{
		readln;
		auto w = readln.split.map !(to !(int)).array;
		auto b = readln.split.map !(to !(int)).array;
		auto a = new int [] [n];
		foreach (j; 0..m)
		{
			int u, v;
			readf (" %s %s", &u, &v);
			u -= 1;
			v -= 1;
			a[u] ~= v;
			a[v] ~= u;
		}

		auto d = new bool [n];
		int [] component;

		void recur (int v)
		{
			if (d[v])
			{
				return;
			}
			d[v] = true;
			component ~= v;
			foreach (u; a[v])
			{
				recur (u);
			}
		}

		int [] [] c;

		foreach (u; 0..n)
		{
			if (!d[u])
			{
				component.length = 0;
				component.assumeSafeAppend ();
				recur (u);
				c ~= component.dup;
			}
		}
		debug {writeln (c);}

		int [] [2] f;
		foreach (ref g; f)
		{
			g = new int [tw + 1];
		}
		int e = 0;
		f[e][] = 0;
		foreach (cur; c)
		{
			e ^= 1;
			f[e][] = f[!e][];
			int sw = 0;
			int sb = 0;

			foreach (k; cur)
			{
				foreach (i; w[k]..tw + 1)
				{
					f[e][i] = max (f[e][i],
					    f[!e][i - w[k]] + b[k]);
				}
				sw += w[k];
				sb += b[k];
			}

			foreach (i; sw..tw + 1)
			{
				f[e][i] = max (f[e][i],
				    f[!e][i - sw] + sb);
			}
		}

		writeln (f[e][].reduce !(max));
	}
}

import core.bitop, std.bitmanip;
import std.algorithm, std.functional;
import std.array, std.container;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

immutable int INF = 10L ^^ 9 + 23;

void main()
{
    int n, m, w;
    readf("%s %s %s", &n, &m, &w);
    readln;

    auto ws = readln.chomp.split.map!(to!int).array;
    auto bs = readln.chomp.split.map!(to!int).array;

    auto grpid = n.iota.array;

    foreach (_; 0 .. m) {
        int a, b;
        readf("%s %s", &a, &b);
        readln;
        --a, --b;

        foreach (ref e; grpid) if (e == grpid[b]) e = grpid[a];
    }

    auto vis = (false).repeat(n).array;
    auto idsForGrp = new int[][] (grpid.dup.sort.uniq.array.length);

    debug { grpid.writeln; }

    Tuple!(int, int)[] grpsVals;
    foreach (i, e; grpid) {
        if (vis[i]) continue;

        int wsm = 0, bsm = 0;
        foreach (j, f; grpid) {
            if (e != f) continue;

            idsForGrp[grpsVals.length] ~= j.to!int;
            vis[j] = true;
            wsm += ws[j];
            bsm += bs[j];
        }

        grpsVals ~= tuple(wsm, bsm);
    }

    debug { grpsVals.writeln; }
    debug { idsForGrp.writeln; }

    auto dp = new int[][] (grpsVals.length, w+1);
    foreach (i, t; grpsVals) {
        int grw = t[0], grb = t[1];

        if (i > 0) dp[i][] = dp[i-1][];

        for (int cw = w; cw - grw >= 0; --cw) {
            int prevVal = i > 0 ? dp[i-1][cw - grw] : 0;
            dp[i][cw] = max(dp[i][cw], prevVal + grb);
        }

        foreach (e; idsForGrp[i]) {
            for (int cw = w; cw - ws[e] >= 0; --cw) {
                int prevVal = i > 0 ? dp[i-1][cw - ws[e]] : 0;
                dp[i][cw] = max(dp[i][cw], prevVal + bs[e]);
            }
        }
    }

    debug { dp.writeln; }

    int ans = dp[$-1].maxElement;

    ans.writeln;
}

import core.bitop;
import core.checkedint;
import core.simd;
import core.stdc.stdlib;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.format;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

__gshared:

bool read(T...)(T ptrs) if (ptrs.length > 0) {
    return readf(' ' ~ replicate("%s ", ptrs.length), ptrs) == ptrs.length;
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

struct Dsu(size_t n) {
    int[n] p;

    int get(int x) {
        return x == p[x]? x : (p[x] = get(p[x]));
    }

    void merge(int x, int y) {
        p[get(x)] = get(y);
    }
}

alias Girl = Tuple!(int, `weight`, int, `beauty`);

struct Group {
    int id;
    Girl[ ] girls;
    Girl sum;
    Group* next;
}

int n, m, wl;
Girl[1000] _girls;
Girl[ ] girls;
Dsu!1000 dsu;
Group[1000] groups;
int[1001][1000] dp;

int dyn(int gid, int curWeight) {
    assert(gid < n);
    if (~dp[gid][curWeight])
        return dp[gid][curWeight];
    const grp = &groups[gid];
    assert (!grp.girls.empty);
    if (grp.next is null) {
        if (curWeight + grp.sum.weight <= wl)
            return (dp[gid][curWeight] = grp.sum.beauty);
        int b = 0;
        foreach (g; grp.girls)
            if (curWeight + g.weight <= wl)
                b = max(b, g.beauty);
        return (dp[gid][curWeight] = b);
    } else {
        int result = 0;
        if (curWeight + grp.sum.weight <= wl)
            result = dyn(grp.next.id, curWeight + grp.sum.weight) + grp.sum.beauty;
        foreach (g; grp.girls)
            if (curWeight + g.weight <= wl)
                result = max(result, dyn(grp.next.id, curWeight + g.weight) + g.beauty);
        return (dp[gid][curWeight] = result);
    }
}

void main() {
    while (read(&n, &m, &wl)) {
        girls = _girls[0 .. n];
        foreach (int i, ref grp; groups) {
            grp.id = i;
            grp.girls = null;
            grp.sum = Girl(0, 0);
        }
        iota(0, n).copy(dsu.p[ ]);
        foreach (ref g; girls)
            read(&g.weight);
        foreach (ref g; girls)
            read(&g.beauty);
        while (m--) {
            int a, b;
            read(&a, &b);
            a--;
            b--;
            dsu.merge(a, b);
        }
        foreach (int i, g; girls) {
            auto grp = &groups[dsu.get(i)];
            grp.girls ~= g;
            grp.sum.weight += g.weight;
            grp.sum.beauty += g.beauty;
        }
        Group* last = null, first = null;
        foreach (ref grp; groups[0 .. n].filter!`!a.girls.empty`) {
            if (last is null)
                first = &grp;
            else
                last.next = &grp;
            last = &grp;
        }
        assert(last !is null);
        last.next = null;
        memset(dp.ptr, 0xFF, dp.sizeof);
        writeln(dyn(first.id, 0));
    }
}


import core.bitop;
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.format;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

string solve (int n, int a, int b)
{
	int k = n;
	int [] p;

	void addCycle (int len)
	{
		int cur = p.length.to !(int) + 1;
		foreach (i; 1..len)
		{
			p ~= p.length.to !(int) + 2;
		}
		p ~= cur;
	}

	while (k % b != 0)
	{
		if (k < a)
		{
			return "-1";
		}
		k -= a;
		addCycle (a);
	}
	while (k > 0)
	{
		addCycle (b);
		k -= b;
	}
	assert (k == 0);
	return format ("%(%s %)", p);
}

void main ()
{
	int n, a, b;
	while (readf (" %s %s %s", &n, &a, &b) > 0)
	{
		writeln (solve (n, a, b));
	}
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const A = readInt();
      const B = readInt();

      for (int x = 0; A * x <= N; ++x) {
        if ((N - A * x) % B == 0) {
          const y = (N - A * x) / B;
          int[] ans;
          int pos;
          foreach (i; 0 .. x) {
            foreach (j; 0 .. A) {
              ans ~= pos + (j + 1) % A;
            }
            pos += A;
          }
          foreach (i; 0 .. y) {
            foreach (j; 0 .. B) {
              ans ~= pos + (j + 1) % B;
            }
            pos += B;
          }
          foreach (i; 0 .. N) {
            if (i > 0) write(" ");
            write(ans[i] + 1);
          }
          writeln();
          goto found;
        }
      }
      writeln(-1);
     found:
    }
  } catch (EOFException e) {
  }
}


import std.stdio, std.string, std.algorithm, std.conv, std.range;
void main()
{
    int q;
    readf!"%d\n"(q);
    foreach (_; 0 .. q)
    {
        int n;
        readf!"%d\n"(n);
        int[] a = readln.split.map!(to!int).array, b = a.dup;
        b.sort();
        foreach (i; 0 .. n)
            if (a[i] != b[1])
                writeln(i + 1);
    }
}

import std.stdio, std.string, std.algorithm, std.conv, std.range;
void main()
{
    int q;
    readf!"%d\n"(q);
    foreach (_; 0 .. q)
    {
        int n;
        readf!"%d\n"(n);
        int[] a = readln.split.map!(to!int).array;
        if (a[0] == a[1])
        {
            foreach (j; 0 .. n)
                if (a[j] != a[0])
                    writeln(j + 1);
        }
        else
        {
            if (a[0] == a[2])
                writeln(2);
            else
                writeln(1);
        }
    }
}

import std;

void main() {
    int t;
    readf("%d\n", t);

    foreach (_; 0 .. t) {
        int n;
        readf("%d\n", n);

        auto a = readln.chomp.split.to!(int[]);

        if (a[0] == a[1] && a[1] == a[2]) {
            foreach (i; 3 .. n) {
                if (a[i] != a[0]) {
                    writeln(i+1);
                }
            }
        }
        else {
            if (a[0] == a[1]) writeln(3);
            if (a[0] == a[2]) writeln(2);
            if (a[1] == a[2]) writeln(1);
        }
    }
}

// Vicfred
// https://codeforces.com/contest/1512/problem/A
// simulation, implementation
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

void main() {
    int t = readln.chomp.to!int;

while(t--) {
    int n = readln.chomp.to!int;
    int[] a = readln.split.map!(to!int).array;

    int[] counter = new int[101];

    foreach(item; a) {
        counter[item] += 1;
    }

    for(int i = 0; i < n; ++i) {
        if(counter[a[i]] == 1) {
            (i + 1).writeln;
            break;
        }
    }
}
}

import std.stdio, std.string, std.algorithm, std.conv, std.range;
void main()
{
    int q;
    readf!"%d\n"(q);
    foreach (_; 0 .. q)
    {
        int n;
        readf!"%d\n"(n);
        int[] a = readln.split.map!(to!int).array, b = a.dup;
        b.sort();
        foreach (i; 0 .. n)
            if (b[i] != a[1])
                writeln(i + 1);
    }
}


module main;

import std.stdio, std.string;

static auto mod = 1000000007;

void init(int[] dp, int[] s, int k)
{
    foreach (i; 0 .. k)
    {
        dp[i] = 1;
    }
    foreach (i; k .. dp.length)
    {
        dp[i] = (dp[i - 1] + dp[i - k]) % mod;
    }
    s[0] = 0;
    foreach (i; 1 .. s.length)
    {
        s[i] = (s[i - 1] + dp[i]) % mod;
    }
}

int main(string[] args)
{
    int k, t;
    while (scanf("%d%d", &t, &k) == 2)
    {
        auto dp = new int[100001];
        auto s = new int[100001];
        init(dp, s, k);
        foreach (i; 0 .. t)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            auto ans = s[b] - s[a - 1];
            if (ans < 0)
            {
                ans += mod;
            }
            writefln("%d", ans);
        }
    }
    return 0;
}

import std.stdio;
import std.exception;

void main() {
	size_t t, k;
	enforce(readf(" %s %s", &t, &k));

	enum MOD = cast(int)(1e9 + 7);
	enum N = 100500;
	int[N] z;
	z[0] = 1;
	foreach (cur; 1..N) {
		z[cur] = z[cur - 1];
		if (cur >= k)
			z[cur] += z[cur - k];
		z[cur] %= MOD;
	}
	foreach (cur; 1..N) {
		z[cur] += z[cur - 1];
		z[cur] %= MOD;
	}

	foreach (test; 0..t) {
		size_t a, b;
		enforce(readf(" %s %s", &a, &b));
		auto ans = (z[b] - z[a - 1] + MOD) % MOD;
		writeln(ans);
	}
}

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip;

void main() {
    immutable int MAX = 10^^5+1;
    immutable int MOD = 10^^9+7;

    int T, K;
    scanf("%d %d", &T, &K);

    auto dp = new int[](MAX);
    foreach (i; 1..MAX) {
        dp[i] = (dp[i-1] + 1) % MOD;
        if (i - K >= 0)
            dp[i] = ((dp[i] + dp[i-K]) % MOD + 1) % MOD;
    }

    int a, b;
    foreach (_; 0..T) {
        scanf("%d %d", &a, &b);
        writeln(((dp[b] - dp[a-1]) % MOD + MOD) % MOD);
    }
}

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip;

void main() {
    immutable int MAX = 10^^5+1;
    immutable int MOD = 10^^9+7;

    int T, K;
    scanf("%d %d", &T, &K);

    auto dp = new int[](MAX);
    foreach (i; 1..MAX) {
        dp[i] = (dp[i-1] + 1) % MOD;
        if (i - K >= 0)
            dp[i] = ((dp[i] + dp[i-K]) % MOD + 1);
    }

    int a, b;
    foreach (_; 0..T) {
        scanf("%d %d", &a, &b);
        writeln(dp[b] - dp[a-1]);
    }
}

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip;

void main() {
    immutable int MAX = 10^^5+1;
    immutable int MOD = 10^^9+7;

    int T, K;
    scanf("%d %d", &T, &K);

    auto dp = new int[](MAX);
    foreach (i; 1..MAX) {
        dp[i] = (dp[i-1] + 1) % MOD;
        if (i - K >= 0)
            dp[i] = ((dp[i] + dp[i-K]) % MOD + 1);
    }

    int a, b;
    foreach (_; 0..T) {
        scanf("%d %d", &a, &b);
        writeln((dp[b] - dp[a-1]) % MOD + MOD % MOD);
    }
}

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip;

void main() {
    immutable int MAX = 10^^5+1;
    immutable int MOD = 10^^9+7;

    int T, K;
    scanf("%d %d", &T, &K);

    auto dp = new int[](MAX);
    foreach (i; 1..MAX) {
        dp[i] = (dp[i-1] + 1) % MOD;
        if (i - K >= 0)
            dp[i] = ((dp[i] + dp[i-K]) % MOD + 1) % MOD;
    }

    int a, b;
    foreach (_; 0..T) {
        scanf("%d %d", &a, &b);
        writeln((dp[b] - dp[a-1]) % MOD + MOD % MOD);
    }
}

module main;

import std.stdio, std.string;

void init(int[] dp, int[] s, int k)
{
    static auto mod = 1000000007;
    foreach (i; 0 .. k)
    {
        dp[i] = 1;
    }
    foreach (i; k .. dp.length)
    {
        dp[i] = (dp[i - 1] + dp[i - k]) % mod;
    }
    s[1] = dp[1];
    foreach (i; 2 .. s.length)
    {
        s[i] = (s[i - 1] + dp[i]) % mod;
    }
}

int main(string[] args)
{
    int k, t;
    while (scanf("%d%d", &t, &k) == 2)
    {
        auto dp = new int[100001];
        auto s = new int[100001];
        init(dp, s, k);
        foreach (i; 0 .. t)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            writefln("%d", s[b] - s[a - 1]);
        }
    }
    return 0;
}

import std.stdio;
import std.exception;

void main() {
	size_t t, k;
	enforce(readf(" %s %s", &t, &k));

	enum MOD = cast(int)(1e9 + 7);
	enum N = 100500;
	int[N] z;
	z[0] = 1;
	foreach (cur; 1..N) {
		z[cur] = z[cur - 1];
		if (cur >= k)
			z[cur] += z[cur - k];
		z[cur] %= MOD;
	}
	foreach (cur; 1..N) {
		z[cur] += z[cur - 1];
		z[cur] %= MOD;
	}

	foreach (test; 0..t) {
		size_t a, b;
		enforce(readf(" %s %s", &a, &b));
		auto ans = z[b] - z[a - 1];
		writeln(ans);
	}
}


import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto s = readln.strip;
		bool ok = false;
		foreach (i; 0..3)
		{
			auto t = "Yes".cycle.drop (i).take (s.length);
			if (equal (s, t))
			{
				ok = true;
			}
		}
		writeln (ok ? "YES" : "NO");
	}
}

import std.stdio;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;
import std.functional;

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void solve() {
    auto x = readln.strip();
    bool norm = true;
outer: foreach(i, e; x) {
        if (i == 0) {
            if ((e != 'Y') && (e != 'e') && (e !='s')) {
                norm = false;
                break outer;
            }
        }
        else {
            if ((e == 'Y' && x[i-1] == 's') || (e == 'e' && x[i-1] == 'Y') || (e == 's' && x[i-1] == 'e'))
                continue;
            else {
                norm = false;
                break outer;
            }
        }
    }
    if (norm)
        writeln("yes");
    else
        writeln("no");

}

void main() {
    int T = read!int;
    foreach (t; 0 .. T) solve();
}

import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    auto s = readln.strip;
    char prevch = s[0];
    bool good = (prevch == 'Y' || prevch == 'e' || prevch == 's');
    foreach (ch ; s[1 .. $]) {
      if ((ch == 'Y' && prevch == 's') ||
          (ch == 'e' && prevch == 'Y') ||
          (ch == 's' && prevch == 'e')) {
        prevch = ch;
      } else {
        good = false;
        break;
      }
    }
    writeln(good ? "YES" : "NO");
  }
}

import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto s = readln.strip.toLower;
		bool ok = false;
		foreach (i; 0..3)
		{
			auto t = "yes".cycle.drop (i).take (s.length);
			if (equal (s, t))
			{
				ok = true;
			}
		}
		writeln (ok ? "YES" : "NO");
	}
}


import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(long)).array;
		auto m = a.maxElement * (n - 1L);
		auto t = a.sum (0L);
		auto s = max (m, t);
		s += ((n - 1) - (s % (n - 1))) % (n - 1);
		writeln (s - t);
	}
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;

		long x, tot;
		foreach (e; a)
		{
			x.chmax(e);
			tot += e;
		}
		auto y = x * (n-1);
		if (tot <= y)
			ans[ti] = y - tot;
		else
			ans[ti] = ((n-1) - (tot % (n-1))) % (n-1);
	}

	foreach (e; ans)
	{
		writeln(e);
	}
	stdout.flush;
	debug readln;
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;

		long x, tot;
		foreach (e; a)
		{
			x.chmax(e);
			tot += e;
		}
		auto y = x * (n-1);
		if (tot <= y)
			ans[ti] = y - tot;
		else
			ans[ti] = (n-1) - (tot % (n-1));
	}

	foreach (e; ans)
	{
		writeln(e);
	}
	stdout.flush;
	debug readln;
}


import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;

void main() {
	auto nab = readln.chomp.split.map!(to!int);
	int n = nab[0];
	long a = nab[1];
	long b = nab[2];
	long[] s = readln.chomp.split.map!(to!long).array;
	long S = s.sum;
	long[] t = std.algorithm.sort!"a > b"(s[1..$]).array;
	long f = s[0] * a;
	int ans = n - 1;
	foreach (i, v; t) {
		debug stderr.writefln("[%d] %d %d", i, v, S);
		if (f >= b * S) {
			ans = cast(int) i;
			break;
		}
		S -= v;
	}
	ans.writeln;
}

import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  int n, a, b; readV(n, a, b);
  int[] s; readA(n, s);

  int t = s.sum;

  auto s2 = s[1..$];
  s2.sort!"a > b";

  auto t2 = 0;
  foreach (i; 0..n) {
    if (a.to!long*s[0] >= b.to!long*(t-t2)) {
      writeln(i);
      return;
    }
    t2 += s2[i];
  }
}

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;

void main() {
	auto nab = readln.chomp.split.map!(to!int);
	int n = nab[0];
	int a = nab[1];
	int b = nab[2];
	int[] s = readln.chomp.split.map!(to!int).array;
	int S = s.sum;
	int[] t = std.algorithm.sort!"a > b"(s[1..$]).array;
	int f = s[0] * a;
	int ans = n - 1;
	foreach (i, v; t) {
		if (f >= b * S) {
			ans = cast(int) i;
			break;
		}
		S -= v;
	}
	ans.writeln;
}

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;

void main() {
	auto nab = readln.chomp.split.map!(to!int);
	int n = nab[0];
	int a = nab[1];
	int b = nab[2];
	int[] s = readln.chomp.split.map!(to!int).array;
	int S = s.sum;
	int f = s[0] * a;
	int ans = n - 1;
	foreach (i, v; s[1..$]) {
		if (f >= b * S) {
			ans = cast(int) i;
			break;
		}
		S -= v;
	}
	ans.writeln;
}


void main(){
  import std.stdio, std.string, std.conv, std.algorithm;

  int w; rd(w);
  auto a=new long[][](2, w);
  a[0]=readln.split.to!(long[]);
  a[1]=readln.split.to!(long[]);

  auto s=new long[](w+1);
  auto sr=new long[][](2, w+1);
  auto sl=new long[][](2, w+1);
  foreach(j; 0..w){
    s[j+1]=s[j];
    if(j%2==0) s[j+1]+=a[0][j]*(j*2)+a[1][j]*(j*2+1);
    else s[j+1]+=a[1][j]*(j*2)+a[0][j]*(j*2+1);
  }
  auto sub=new long[][](2, w+1);
  foreach(i; 0..2)foreach(j; 0..w) sub[i][j+1]=sub[i][j]+a[i][j];
  foreach(i; 0..2)for(int j=w-1; j>=0; j--){
    sr[i][j]=sr[i][j+1];
    sr[i][j]-=(sub[i][w]-sub[i][j+1]);
    sr[i][j]+=a[i][j]*(j*2); // ちょっとサボる

    sl[i][j]=sl[i][j+1];
    sl[i][j]-=(sub[i][w]-sub[i][j+1]);
    sl[i][j]+=a[i][j]*(w*2-1);
  }
  long mx=0;
  for(int j=0; j<=w; j++){
    if(j%2==0){
      mx=max(mx, s[j]+sr[0][j]+sl[1][j]);
      // writeln(s[j]+sr[0][j]+sl[1][j]);
    }else{
      mx=max(mx, s[j]+sr[1][j]+sl[0][j]);
      // writeln(s[j]+sr[1][j]+sl[0][j]);
    }
  }
  writeln(mx);
}

/*
  s[j]:=j列目までジグザグ (1-indexed)
    j=0, 1, ..., w
    s[0]=0
  sr[i][j]:=i行をj列から右端まで (0-indexed)
  sl[i][j]:=i行を右端からj列まで (0-indexed)

  sl[*][w]=sr[*][w]=0

  j列までジグザグ j=0, 1, ..., w
  jが偶数
    0行目をj+1列目から右端まで、1行目を右端からj+1列目まで
    s[j]+sr[0][j]+sl[1][j]
  jが奇数
    s[j]+sr[1][j]+sl[0][j]

*/

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x) e=l[i].to!(typeof(e));
}

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

void main() {
    auto N = readln.chomp.to!int;
    auto A = 2.iota.map!(_ => readln.split.map!(to!long).array).array;
    auto B = new long[][](2, N+1);

    foreach (i; 0..2)
        foreach (j; 0..N)
            B[i][j+1] = B[i][j] + A[i][j];

    long ans = 0;
    long tmp = 0;
    long a = 0;
    long b = 0;
    foreach (i; 0..N) a += i * A[0][i];
    foreach (i; 0..N) a += (N+N-i-1) * A[1][i];

    foreach (i; 1..N) b += (i + 1) * A[1][i];
    foreach (i; 1..N) b += (N+N-i) * A[0][i];

    foreach (i; 0..N) {
        long t = i*2;
        if (i % 2 == 0) {
            ans = max(ans, tmp + a);
            tmp += t * A[0][i];
            tmp += (t + 1) * A[1][i];
            if (i < N-2) {
                a -= t * A[0][i];
                a -= (t+1) * A[0][i+1];
                a += (B[0][N] - B[0][i+2]) * 2;
                a -= (2 * N - 1) * A[1][i];
                a -= (2 * N - 2) * A[1][i+1];
                a += (B[1][N] - B[1][i+2]) * 2;
            }
        } else {
            ans = max(ans, tmp + b);
            tmp += t * A[1][i];
            tmp += (t + 1) * A[0][i];
            if (i < N-2) {
                b -= t * A[1][i];
                b -= (t+1) * A[1][i+1];
                b += (B[1][N] - B[1][i+2]) * 2;
                b -= (2 * N - 1) * A[0][i];
                b -= (2 * N - 2) * A[0][i+1];
                b += (B[0][N] - B[0][i+2]) * 2;
            }
        }
        if (i == N-1) ans = max(ans, tmp);
    }

    ans.writeln;
}

import core.bitop;
import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import std.range.interfaces;

void main()
{
    int n;
    readf("%s", &n);
    readln;

    int[][] rws;
    foreach (_; 0 .. 2) rws ~= readln.chomp.split.map!(to!int).array;

    debug { rws.writeln; }

    auto mxsum = new long[][] (2, n+1);
    mxsum[0][$-1] = mxsum[1][$-1] = 0;
    auto dp = new long[][] (2, n+1);
    dp[0][$-1] = dp[1][$-1] = 0;
    auto totsum = 0L, updown = 0L, downup = 0L;
    foreach_reverse (i; 0 .. n) {
        updown += totsum + cast(long)(2*(n-i) - 1) * rws[1][i];
        downup += totsum + cast(long)(2*(n-i) - 1) * rws[0][i];

        dp[0][i] = max(updown, cast(long)rws[1][i] + dp[1][i+1] + 2*totsum);
        dp[1][i] = max(downup, cast(long)rws[0][i] + dp[0][i+1] + 2*totsum);

        totsum += rws[0][i] + rws[1][i];
        debug { writeln(dp[0][i], ' ', dp[1][i]); }
    }

    dp[0][0].writeln;
}

import core.bitop;
import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import std.range.interfaces;

void main()
{
    int n;
    readf("%s", &n);
    readln;

    int[][] rws;
    foreach (_; 0 .. 2) rws ~= readln.chomp.split.map!(to!int).array;

    debug { rws.writeln; }

    auto dp = new long[][] (2, n+1);
    dp[0][$-1] = dp[1][$-1] = 0;
    auto totsum = 0L, updown = 0L, downup = 0L;
    foreach_reverse (i; 0 .. n) {
        updown += totsum + cast(long)(2*(n-i) - 1) * rws[1][i];
        downup += totsum + cast(long)(2*(n-i) - 1) * rws[0][i];

        dp[0][i] = max(updown, cast(long)rws[1][i] + dp[1][i+1] + 2*totsum);
        dp[1][i] = max(downup, cast(long)rws[0][i] + dp[0][i+1] + 2*totsum);

        totsum += rws[0][i] + rws[1][i];

        debug { writeln(dp[0][i], ' ', dp[1][i]); }
    }

    dp[0][0].writeln;
}

void main(){
  import std.stdio, std.string, std.conv, std.algorithm;

  int w; rd(w);
  auto a=new long[][](2, w);
  a[0]=readln.split.to!(long[]);
  a[1]=readln.split.to!(long[]);

  auto s=new long[](w+1);
  auto sr=new long[][](2, w+1);
  auto sl=new long[][](2, w+1);
  foreach(j; 0..w){
    s[j+1]=s[j];
    if(j%2==0) s[j+1]+=a[0][j]*(j*2)+a[1][j]*(j*2+1);
    else s[j+1]+=a[1][j]*(j*2)+a[0][j]*(j*2+1);
  }
  auto sub=new long[][](2, w+1);
  foreach(i; 0..2)foreach(j; 0..w) sub[i][j+1]=sub[i][j]+a[i][j];
  foreach(i; 0..2)for(int j=w-1; j>=0; j--){
    sr[i][j]=sr[i][j+1];
    sr[i][j]-=(sub[i][w]-sub[i][j+1]);
    sr[i][j]+=a[i][j]*(j*2); // ちょっとサボる

    sl[i][j]=sl[i][j+1];
    sl[i][j]-=(sub[i][w]-sub[i][j+1]);
    sl[i][j]+=a[i][j]*(w*2-1);
  }
  long mx=0;
  for(int j=0; j<=w; j++){
    if(j%2==0) mx=max(mx, s[j]+sr[0][j]+sl[1][j]);
    else mx=max(mx, s[j]+sr[1][j]+sl[0][j]);
  }
  writeln(mx);
}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x) e=l[i].to!(typeof(e));
}


import std.stdio, std.string;
import std.algorithm;

long saiki(int idx, int pos, int n, long[][] dp, int[][] a)
{
    if (dp[idx][pos] != -1)
    {
        return dp[idx][pos];
    }
    if (idx == n - 1)
    {
        long val = 0;
        for (int i = 2; i >= 0; -- i)
        {
            val += a[i][n - 1];
            dp[idx][i] = val;
        }
        return dp[idx][pos];
    }
    auto ndp = new long[][](2, 3);
    auto sum = new long[2];
    foreach (i; 0 .. 2)
    {
        sum[i] = 0;
        foreach (j; 0 .. 3)
        {
            sum[i] += a[j][idx + i];
            if (idx + i + 1 < n)
            {
                ndp[i][j] = saiki(idx + i + 1, j, n, dp, a);
            }
        }
    }
    auto res = long.min;
    if (pos == 1)
    {
        res = max(res, ndp[0][pos] + a[pos][idx]);
        res = max(res, ndp[0][0] + a[pos][idx] + a[0][idx]);
        res = max(res, ndp[0][2] + a[pos][idx] + a[2][idx]);
    }
    else
    {
        res = max(res, ndp[0][pos] + a[pos][idx]);
        res = max(res, ndp[0][1] + a[pos][idx] + a[1][idx]);
        if (pos == 0) res = max(res, ndp[0][2] + sum[0]);
        else res = max(res, ndp[0][0] + sum[0]);
        if (idx + 2 < n)
        {
            if (pos == 0) res = max(res, ndp[1][2] + sum[0] + sum[1]);
            else res = max(res, ndp[1][0] + sum[0] + sum[1]);
        }
        if (idx == n - 2 && pos == 0)
        {
            res = max(res, sum[0] + sum[1]);
        }
    }
    dp[idx][pos] = res;
    return res;
}

void solve(int[][] a, int n)
{
    auto dp = new long[][](n, 3);
    foreach (i; 0 .. n)
    {
        fill(dp[i], -1);
    }
    auto ans = saiki(0, 0, n, dp, a);
    writeln(ans);
}

int main(string[] args)
{
    int n;
    while (readf("%d\n", &n) == 1)
    {
        auto a = new int[][](3, n);
        foreach (i; 0 .. 3)
        {
            foreach (j; 0 .. n)
            {
                readf(" %d", &a[i][j]);
            }
            readln;
        }
        solve(a, n);
    }
    return 0;
}

import std.stdio, std.string;
import std.algorithm;

long saiki(int idx, int pos, int n, long[][] dp, int[][] a)
{
    if (dp[idx][pos] != -1)
    {
        return dp[idx][pos];
    }
    if (idx == n - 1)
    {
        long val = 0;
        for (int i = 2; i >= 0; -- i)
        {
            val += a[i][n - 1];
            dp[idx][i] = val;
        }
        return dp[idx][pos];
    }
    long[3][2] ndp;
    long[2] sum;
    foreach (i; 0 .. 2)
    {
        sum[i] = 0;
        foreach (j; 0 .. 3)
        {
            sum[i] += a[j][idx + i];
            if (idx + i + 1 < n)
            {
                ndp[i][j] = saiki(idx + i + 1, j, n, dp, a);
            }
        }
    }
    auto res = long.min;
    if (pos == 1)
    {
        res = max(res, ndp[0][pos] + a[pos][idx]);
        res = max(res, ndp[0][0] + a[pos][idx] + a[0][idx]);
        res = max(res, ndp[0][2] + a[pos][idx] + a[2][idx]);
    }
    else
    {
        res = max(res, ndp[0][pos] + a[pos][idx]);
        res = max(res, ndp[0][1] + a[pos][idx] + a[1][idx]);
        res = max(res, ndp[0][2 - pos] + sum[0]);
        if (idx + 2 < n)
        {
            if (pos == 0) res = max(res, ndp[1][2] + sum[0] + sum[1]);
            else res = max(res, ndp[1][0] + sum[0] + sum[1]);
        }
        if (idx == n - 2 && pos == 0)
        {
            res = max(res, sum[0] + sum[1]);
        }
    }
    dp[idx][pos] = res;
    return res;
}

void solve(int[][] a, int n)
{
    auto dp = new long[][](n, 3);
    foreach (i; 0 .. n)
    {
        fill(dp[i], -1);
    }
    auto ans = saiki(0, 0, n, dp, a);
    writeln(ans);
}

int main(string[] args)
{
    int n;
    while (readf("%d\n", &n) == 1)
    {
        auto a = new int[][](3, n);
        foreach (i; 0 .. 3)
        {
            foreach (j; 0 .. n)
            {
                readf(" %d", &a[i][j]);
            }
            readln;
        }
        solve(a, n);
    }
    return 0;
}


import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main () {
	foreach (test; 0..readln.strip.to !(int))
		readln.strip.to !(int).iota.map !(q{a + 1}).writefln !("%(%s %)");
}

module _template;
import std.stdio;
import std.string;
import std.algorithm;
import std.container;
import std.range;
import std.math;
import std.numeric;
import std.conv;
import std.typecons;
import std.format;

struct IO {
        string read_string() {
                while (tokens.empty) {
                tokens = readln.split;
        }
        auto token = tokens.front;
                tokens.popFront;
                return token;
        }

        int read_int() {
                return read_string.to!int;
        }

        double read_double() {
                return read_string.to!double;
        }

        string[] tokens;
}

void main() {
        IO cin;
        int t = 1;
        t = cin.read_int;
        while (t--) {
                int n = cin.read_int;
                for (int i = 1; i <= n; i++) {
                        write(i, " ");
                }
                writeln();
        }
}
/*  CODED BY:-
 ___________________________________
|                       ___         |
|  /\   /\  \ /  |  |  |___   |__|  |
| /~~\ /~~\  |   |__|   ___|  |  |  |
|___________________________________|

*/

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new int[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;

		foreach (i; 0..n)
		{
			ans[ti] ~= i+1;
		}
	}

	foreach (e; ans)
		e.map!(to!string).join(" ").writeln;
	stdout.flush;
	debug readln;
}