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

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

    auto arr = readln.chomp.split.map!(to!long).array;

    if (arr.maxElement <= arr.minElement * 2) {
        (-1).repeat(n).map!(to!string).join(" ").writeln;
        return;
    }

    auto arr3 = arr.chain(arr).chain(arr).array;

    debug { arr3.writeln; }

    auto ans = new int[] (n);

    int le = 0;
    alias elem = Tuple!(int, int);
    auto mx = make!(DList!elem);
    foreach (i, e; arr3) {
        while (!mx.empty() && e >= mx.back[0]) {
            mx.removeBack();
        }

        mx ~= tuple(e.to!int, i.to!int);

        debug { writeln(i, ' ', e, ' ', mx.front[0]); }

        while (!mx.empty() && e * 2 < mx.front[0]) {
            while (le <= mx.front[1]) {
                ans[le % n] = i.to!int - le;
                ++le;
            }

            debug { writeln(le); }
            mx.removeFront();
        }
    }

    ans.map!(to!string).join(" ").writeln;
}

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

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;

		auto t = new int [limit];
		foreach (i; 0..n * 3 + 1)
		{
			t[i + half] = a[i % n];
		}
		foreach_reverse (i; 1..half)
		{
			t[i] = min (t[i * 2 + 0], t[i * 2 + 1]);
		}

		auto bads = new int [n * 2];
		for (int i = 0; i < n * 2; i++)
		{
			int value = a[i % n];
			int hi = i + half;
			while (true)
			{
				if ((hi & 1) == 0 && t[hi + 1] * 2 < value)
				{
					break;
				}
				hi = hi / 2;
			}
			while (hi < half)
			{
				int p = hi * 2 + 2;
				if (t[p] * 2 < value)
				{
					p -= 1;
				}
				hi = p;
			}
			bads[i] = hi - half;
			debug {writeln (i, ": ", bads[i]);}
		}

		foreach_reverse (i; 0..n * 2 - 1)
		{
			bads[i] = min (bads[i], bads[i + 1]);
		}
		debug {writeln (bads);}

		auto res = new int [n];
		foreach (i; 0..n)
		{
			if (bads[i] == n * 3)
			{
				res[i] = -1;
			}
			else
			{
				res[i] = bads[i] - i + 1;
			}
		}
		writefln ("%(%s %)", res);
	}
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int n = rint;
	long[] as = rlong(n);
	as ~= as ~ as;
	log("as:", as);

	T uplimit(T)(T a, T c, bool delegate(T) f){
		if(f(c)) return c; if(! f(a)) return a - 1;
		while(a + 1 < c){ T b = (a + c) / 2; if(f(b)) a = b; else c = b; }
		return a;
	}

	int[] ans;
	K[] ks = new K[](n * 2);
	int l = 0, r = 0;
	int j0;
	foreach(int i, a; as){
		while(l < r && ks[r - 1].value <= a) r -= 1;
		ks[r] = K(i, a), r += 1;
		log("i:", i, "a:", a, "l:", l, "r:", r, "ks:", ks[0 .. n]);
		int newl = uplimit(l, r - 1, (int x) => (ks[x].value > a * 2));
		if(newl >= 0 && j0 <= ks[newl].pos){
			foreach(j; j0 .. ks[newl].pos + 1) ans ~= i - j;
			j0 = ks[newl].pos + 1;
		}
		log("l:", l, "newl:", newl, "ans:", ans);
	}
	while(ans.length < n) ans ~= -1;
	ans[0 .. n].map!(to!string).array.join(" ").writeln;

}
struct K{
	int pos;
	long value;
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, 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)); }

enum E = 20;

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

      auto mx = new int[][](E, N);
      auto mn = new int[][](E, N);
      auto live = new bool[][](E, N);
      foreach (i; 0 .. N) {
        mx[0][i] = A[i];
        mn[0][i] = A[i];
        live[0][i] = true;
      }
      foreach (e; 0 .. E - 1) {
        foreach (i; 0 .. N) {
          const j = (i + (1 << e)) % N;
          mx[e + 1][i] = max(mx[e][i], mx[e][j]);
          mn[e + 1][i] = min(mn[e][i], mn[e][j]);
          live[e + 1][i] = live[e][i] && live[e][j] && !(mx[e][i] > 2 * mn[e][j]);
        }
      }
      auto ans = new int[N];
      foreach (i; 0 .. N) {
        int m = 0;
        int sum;
        int j = i;
        foreach_reverse (e; 0 .. E) {
          if (live[e][j] && !(m > 2 * mn[e][j])) {
            chmax(m, mx[e][j]);
            sum += 1 << e;
            j = (j + (1 << e)) % N;
          }
        }
        ans[i] = (sum >= 2 * N) ? -1 : sum;
      }

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


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[] tk;
        string readString() {
                while (tk.empty)
                        tk = readln.split;
                auto tkt = tk.front;
                tk.popFront;
                return tkt;
        }
        int readInt() {
                return readString.to!int;
        }
        double readDouble() {
                return readString.to!double;
        }
}

void main() {
        IO cin;
        int t = 1;
        // t = cin.readInt;
        while (t--) {
                int n = cin.readInt;
                int k = cin.readInt;
                int[] d = new int[n * k + 1];
                int[int] map;
                int[][] c = new int[][](k);

                for (int i = 0; i < k; i++) {
                        int input = cin.readInt;
                        d[input] = 1;
                        map[i] = input;
                }

                int offSet = 1;
                for (int i = 0; i < k; i++) {
                        c[i] ~= map[i];
                        int count = 1;
                        for (int j = offSet; count < n;) {
                                if (!d[j]) {
                                        c[i] ~= j;
                                        d[j] = 1;
                                        count++;
                                        j++;
                                        offSet = j;
                                } else {
                                        offSet++;
                                        j++;
                                }
                        }
                }

                for (int i = 0; i < k; i++) {
                        for (int j = 0; j < c[i].length; j++) {
                                write(c[i][j], " ");
                        }
                        writeln();
                }
        }
}

module sigod.codeforces.p244A;

import std.array : split;
import std.conv : to;
import std.stdio;
import std.string : strip;

void main() {
	int n, k;

	read_variables(n, k);

	auto a = read_array!int();
	auto orange = new bool[n * k + 1];

	foreach (ref ai; a) {
		orange[ai] = true;
	}

	int position = 1;

	foreach (ref ai; a) {
		stdout.write(ai);

		int count = 1;
		while (count < n) {
			if (!orange[position]) {
				stdout.write(" ", position);

				orange[position] = true;

				++count;
			}

			++position;
		}

		stdout.writeln();
	}
}

private
void read_variables(S...)(out S args) {
	auto input = stdin.readln().strip().split();

	foreach (index, ref arg; args) {
		arg = input[index].to!(typeof(arg))();
	}
}

private
T[] read_array(T)() {
	auto input = stdin.readln().strip().split();

	T[] result = new T[input.length];

	foreach (index, ref element; input) {
		result[index] = element.to!T();
	}

	return result;
}

module sigod.codeforces.p244A;

import std.array : split;
import std.conv : to;
import std.stdio;
import std.string : strip;

void main() {
	int n, k;

	read_variables(n, k);

	auto a = read_array!int();

	foreach (ai; a) {
		stdout.write(ai);

		foreach (ni; 1 .. n) {
			ai += k;

			if (ai > n * k) ai %= n * k;

			stdout.write(" ", ai);
		}

		stdout.writeln();
	}
}

private
void read_variables(S...)(out S args) {
	auto input = stdin.readln().strip().split();

	foreach (index, ref arg; args) {
		arg = input[index].to!(typeof(arg))();
	}
}

private
T[] read_array(T)() {
	auto input = stdin.readln().strip().split();

	T[] result = new T[input.length];

	foreach (index, ref element; input) {
		result[index] = element.to!T();
	}

	return result;
}


void main() { runSolver(); }

void problem() {
  auto QN = scan!long;

  auto solve() {
    auto subSolve() {
      auto N = scan!int;
      auto A = scan!long(N);

      bool[long] visited;
      foreach(a; A) {
        if (a in visited) visited[-a] = true; else visited[a] = false;
      }

      return visited.length;
    }

    foreach(_; 0..QN) subSolve().writeln;
  }

  outputForAtCoder(&solve);
}

// ----------------------------------------------

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;
T[][] combinations(T)(T[] s, in long m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
Point invert(Point p) { return Point(p.y, p.x); }
long[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }
bool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }
bool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }
string charSort(alias S = "a < b")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }
ulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}
T[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }
string toAnswerString(R)(R r) { return r.map!"a.to!string".joiner(" ").array.to!string; }
struct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x<MD)?x:x-MD;}static auto make(ulong x) {ModInt m; m.v = x; return m;}auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!ulong);}auto opBinary(string op:"^^", T)(T r) const {long x=v;long y=1;while(r){if(r%2==1)y=(y*x)%MD;x=x^^2%MD;r/=2;} return make(y);}auto opBinary(string op:"/")(ModInt r) const {return this*memoize!inv(r);}static ModInt inv(ModInt x) {return x^^(MD-2);}string toString() const {return v.to!string;}auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}}
alias MInt1 = ModInt!(10^^9 + 7);
alias MInt9 = ModInt!(998_244_353);
void outputForAtCoder(T)(T delegate() fn) {
  static if (is(T == float) || is(T == double) || is(T == real)) "%.16f".writefln(fn());
  else static if (is(T == void)) fn();
  else static if (is(T == string)) fn().writeln;
  else static if (isInputRange!T) {
    static if (!is(string == ElementType!T) && isInputRange!(ElementType!T)) foreach(r; fn()) r.toAnswerString.writeln;
    else foreach(r; fn()) r.writeln;
  }
  else fn().writeln;
}
void runSolver() {
  enum BORDER = "==================================";
  debug { BORDER.writeln; while(!stdin.eof) { "<<< Process time: %s >>>".writefln(benchmark!problem(1)); BORDER.writeln; } }
  else problem();
}
enum YESNO = [true: "Yes", false: "No"];

// -----------------------------------------------

K binarySearch(K)(bool delegate(K) cond, K l, K r) { return binarySearch((K k) => k, cond, l, r); }
T binarySearch(T, K)(K delegate(T) fn, bool delegate(K) cond, T l, T r) {
  auto ok = l;
  auto ng = r;
  const T TWO = 2;

  bool again() {
    static if (is(T == float) || is(T == double) || is(T == real)) {
      return !ng.approxEqual(ok, 1e-08, 1e-08);
    } else {
      return abs(ng - ok) > 1;
    }
  }

  while(again()) {
    const half = (ng + ok) / TWO;
    const halfValue = fn(half);

    if (cond(halfValue)) {
      ok = half;
    } else {
      ng = half;
    }
  }

  return ok;
}

import std;

void main () {
    immutable tests = (){ uint t; readf!"%s\n"(t); return t; }();

    foreach (test_index; 0 .. tests) {
        immutable n = (){ uint n; readf!"%s\n"(n); return n; }();

        immutable s =
            readln
            .chomp
            .split(' ')
            .to!(byte[])
            .sort
            .group
            .assocArray;

        immutable ans = s.length +
            s.byKey
            .count!((k) => s[k] >= 2 && -k !in s);

        writeln(ans);
    }
}
// "" ' '

import std.algorithm;
import std.conv;
import std.math;
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 !(int)).array;
		int [int] k;
		foreach (ref c; a)
		{
			k[abs (c)] += 1;
			k[abs (c)] = min (k[abs (c)], 1 + (c != 0));
		}
		k.byValue.sum.writeln;
	}
}

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) {
    int n;
    readf!" %d "(n);
    auto a = readln.splitter.map!(to!int).array;
    int[int] freq;
    foreach (x ; a)
      freq[x]++;
    int ans = (0 in freq) ? 1 : 0;
    foreach (i ; 1 .. 101) {
      int cnt = freq.get(i, 0) + freq.get(-i, 0);
      ans += min(cnt, 2);
    }
    writeln(ans);
  }
}

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 numCases = readInt;
      foreach (caseId; 0 .. numCases) {
        const N = readInt;
        auto A = new int[N];
        foreach (i; 0 .. N) {
          A[i] = readInt;
        }

        int[int] freq;
        foreach (i; 0 .. N) {
          ++freq[abs(A[i])];
        }
        int ans;
        foreach (key, val; freq) {
          ans += min((key == 0) ? 1 : 2, val);
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

import std;

void main () {
    immutable tests = (){ uint t; readf!"%s\n"(t); return t; }();

    foreach (test_index; 0 .. tests) {
        immutable n = (){ uint n; readf!"%s\n"(n); return n; }();

        immutable arr = readln().chomp.split(' ').to!(int[]).assumeUnique;

        int[int] normal;
        foreach (val; arr) ++ normal[val];

        size_t maxi = normal.length;
        foreach (i; 0 .. n) {
            int[int] frec = normal.dup;

            foreach (j; i .. n) {
                if (arr[j]) {
                    -- frec[arr[j]];
                    ++ frec[-arr[j]];
                    if (frec[arr[j]] == 0)
                        frec.remove(arr[j]);
                }
                maxi = max(maxi, frec.length);
            }
        }

        maxi.writeln();
    }
}
// "" ' '

import std;

void main () {
    immutable tests = (){ uint t; readf!"%s\n"(t); return t; }();

    foreach (test_index; 0 .. tests) {
        immutable n = (){ uint n; readf!"%s\n"(n); return n; }();

        immutable arr = readln().chomp.split(' ').to!(int[]).assumeUnique;

        int[int] normal;
        foreach (val; arr) ++ normal[val];

        size_t maxi = normal.length;
        foreach (i; 0 .. n) {
            int[int] frec = normal.dup;
            int[int] inverted;

            size_t cont = frec.length;
            foreach (j; i .. n) {
                if (arr[j] != 0) {
                    cont += (inverted[arr[j]] ++ == 0);
                    cont -= (-- frec[arr[j]] == 0);
                }
                maxi = max(maxi, cont);
            }
        }

        maxi.writeln();
    }
}
// "" ' '


import std.container;
import std.algorithm;
import std.range;
import std.array;
import std.stdio;
import std.utf;
import std.math;
import std.typecons;

alias type = single;
struct TestCase
{
  mixin prettyPrinting;

  long n, m;
  @Dim("n") long[] a;
  @Dim("n - 1") Tuple!(long, long)[] edges;

  void solve(long tc = -1)
  {
    foreach(ref edge; edges)
      edge[0]--, edge[1]--;
    auto alist = makeSlice!(long[])(n);
    auto visited = makeSlice!(bool)(n);
    foreach(edge; edges)
      {
        alist.at(edge[0]) ~= edge[1];
        alist.at(edge[1]) ~= edge[0];
      }
    long cnt = 0;
    void dfs(long node, long cats, bool good)
    {
      visited.at(node) = true;
      bool isLeaf = true;
      cats += a.at(node);
      if (cats > m)
        good = false;
      foreach(w; alist.at(node))
        if (!visited.at(w))
          {
            isLeaf = false;
            if (a.at(node) == 0)
              dfs(w, 0, good);
            else
              dfs(w, cats, good);
          }
      if (isLeaf && good)
        {
          cnt++;
        }
    }
    dfs(0, 0, true);
    writeln(cnt);
  }
}

struct Interval
{
  long l, h;

  bool empty()
  {
    return l > h;
  }
}

Interval intersect(Interval a, Interval b)
{
  return Interval(max(a.l, b.l), min(a.h, b.h));
}

struct If
{
  string condition;
}
enum ignore = If("false");
struct Dim
{
  string dimensions;
  int levels;
  this(string dimensions)
  {
    this.dimensions = dimensions;
    levels = cast(int)dimensions.count(",") + 1;
  }
}

template getArrayLevel(W)
{
  static if (isDynamicArray!W && !is(W == string))
    enum getArrayLevel = 1 + getArrayLevel!(removeArray!(W, 1));
  else
    enum getArrayLevel = 0;
}

static foreach(fieldName; 'a' .. cast(char)('z' + 1))
  {
    static if (fieldName != 'o')
      mixin("enum d", fieldName, " = Dim(\"", fieldName, "\");");
  }

mixin template prettyPrinting()
{
  string toString()
  {
    alias structType = typeof(this);
    auto res = appender!(string)();
    res.put(structType.stringof);
    res.put("(");
    static foreach(fieldName; FieldNameTuple!structType)
      {
        res.put(text(" ", fieldName, ": ", mixin(fieldName)));
      }
    res.put(" )");
    res.put("\n");
    return res[];
  }
}

struct Graph(N)
{
  size_t size;
  this(S)(S size)
  {
    adjacencyList.length = cast(size_t) size;
    this.size = cast(size_t) size;
    this.visited = makeSlice!bool(size);
  }
  N[][] adjacencyList;
  alias ne = neighbours;
  N[] neighbours(N n)
  {
    return adjacencyList.at(n);
  }
  alias bcon = biconnect;
  void biconnect(N a, N b)
  {
    adjacencyList.at(a) ~= b;
    adjacencyList.at(b) ~= a;
  }
  alias con = connect;
  void connect(N a, N b)
  {
    adjacencyList.at(a) ~= b;
  }
  void connect(R)(R edges)
  {
    foreach(ref edge; edges)
      {
        connect(edge[0], edge[1]);
      }
  }
  void biconnect(R)(R edges)
  {
    foreach(ref edge; edges)
      {
        biconnect(edge[0], edge[1]);
      }
  }
  alias deg = degree!long;
  auto degree(T)(N a)
  {
    return cast(T) adjacencyList.at(a).length;
  }

  auto visited = new bool[0];
  void dfs(Visitor)(long startNode)
  {
    auto visitor = Visitor.init;
    void internalDfs(long node)
    {
      visited.set(node, true);
      static if (is(typeof({visitor.n = node;})))
        visitor.n = node;
      static if (is(typeof({visitor.pre;})))
        visitor.pre;
      foreach(w; ne(node))
        if (!visited.at(w))
          {
            static if (is(typeof({visitor.w = w;})))
              visitor.w = w;
            static if (is(typeof(visitor.pren)))
              visitor.pren;
            internalDfs(w);
            static if (is(typeof(visitor.postn)))
              visitor.postn;
          }
      static if (is(typeof(visitor.post)))
        visitor.post;
    }
    internalDfs(startNode);
  }
  void dfs(long startNode)
  {
    struct DefVisitor {}
    dfs!DefVisitor(startNode);
  }
}

DList!string _words;

template removeArray(W, int times)
{
  static if (times == 0)
    alias removeArray = W;
  else static if (times == 1)
    alias removeArray = typeof(function(){W w; return w[0];}());
  else
    {
      static assert(isDynamicArray!W);
      alias removeArray = removeArray!(removeArray!(W, 1), times - 1);
    }
}

template baseType(W)
{
  static if (isDynamicArray!W && !is(W == string))
    alias baseType = baseType!(typeof((delegate() {W w; return w[0];})()));
  else
    alias baseType = W;
}

auto makeSlice(E, W, T...)(W size, T otherSizes)
{
  static if (T.length == 0)
    {
      E[] res;
      res.length = cast(size_t) size;
      return res;
    }
  else
    {
      typeof(makeSlice!E(otherSizes))[] res;
      res.length = cast(size_t) size;
      foreach(ref row; res)
        row = makeSlice!E(otherSizes);
      return res;
    }
}

void read(T...)(ref T t)
{
  void basicTypeRead(W)(ref W w)
  {
    import std.stdio: readln;
    while(_words.empty)
      {
        import std.array: split;
        foreach(word; readln.split)
          {
            _words.insertBack(word);
          }
      }
    import std.conv: to;
    string word = _words.front;
    _words.removeFront;
    w = to!W(word);
  }
  void iterableRead(W)(ref W w)
  {
    foreach(ref element; w)
      read(element);
  }
  void tupleRead(W)(ref W w)
  {
    static foreach(j; 0 .. W.length)
      read(w[j]);
  }
  void structRead(W)(ref W readStruct)
  {
    static foreach(fieldName; FieldNameTuple!W)
      {
        with(readStruct)
          {
            alias fieldSymbol = mixin(W.stringof, ".", fieldName);
            alias typeOfField = typeof(mixin(fieldName));
            static if (hasUDA!(fieldSymbol, If))
              enum condition = getUDAs!(fieldSymbol, If)[0].condition;
            else
              enum condition = "true";

            if (mixin(condition))
              {
                static if (hasUDA!(fieldSymbol, Dim))
                  {
                    static assert(isDynamicArray!(typeOfField)
                                  , text(W.stringof, ".", fieldName, " has Dim UDA but isn't a Dynamic Array."));
                    enum uda = getUDAs!(fieldSymbol, Dim)[0];
                    static assert(getArrayLevel!(typeOfField) == uda.levels,
                                  text(W.stringof, ".", fieldName, " has a Dim UDA with different number of dimensions than it's type."));
                    mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda.dimensions, q{)});
                  }
                else static if (isDynamicArray!typeOfField && !is(typeOfField == string))
                  {
                    pragma(msg, "Warning: You probably want to add dimension to input ", W.stringof, ".", fieldName);
                  }
                read(mixin(fieldName));
              }
          }
      }
  }
  static foreach(i; 0 .. T.length)
    {
      static if (isBasicType!(T[i]) || is(T[i] == string))
        basicTypeRead(t[i]);
      else static if (is(typeof({foreach(ref e; t[i]){}})))
        iterableRead(t[i]);
      else static if (isTuple!(T[i]))
        tupleRead(t[i]);
      else static if (isAggregateType!(T[i]))
        structRead(t[i]);
    }
}

auto next(T)()
{
  T t;
  read(t);
  return t;
}

size_t ind(T)(T t)
{
  return cast(size_t)t;
}

ref auto at(R, I)(ref R r, I i)
{
  return r[cast(size_t)i];
}

void set(R, I, V)(ref R r, I i, V v)
{
  r[cast(size_t)i] = v;
}

import std.conv;
import std.traits;
import std.meta;
int _loglevel = -1;
void _log(string functionName, argSeq...)()
{
  foreach(i; 0 .. _loglevel * 4)
    write(' ');
  write(functionName);
  static foreach(arg; argSeq)
    {
      write(" ", arg.stringof, " = ", arg);
    }
  writeln;
}
enum logCall =
  q{
    debug
    {
     {
     _loglevel++;
     alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, ".").back));
     static if (_parameterTuple.length > 0)
       mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(", ").array, `);`);
     else
       _log!(__FUNCTION__);
      }
     foreach(i; 0 .. _loglevel * 4) write(' ');
     writeln('{');
     scope(exit)
       {
         foreach(i; 0 .. _loglevel * 4) write(' ');
         writeln('}');
         _loglevel--;
       }
    }
};

void logSym(S...)(int line = __LINE__, string file = __FUNCTION__)
{
  debug
    {
      foreach(i; 0 .. _loglevel * 4) write(' ');
      write(file, "(", line, "):");
      static foreach(s; S)
        write(" ", s.stringof, " = ", s);
      writeln;

    }
}

Array!E makeArray(E, S)(S size, lazy E value)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  foreach(ref ai; a)
    ai = value();
  return a;
}
Array!E makeArray(E, S)(S size)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  return a;
}

template mem(alias F)
{
  ReturnType!F[Tuple!(Parameters!F)] table;
  ReturnType!F mem(Parameters!F args)
  {
    return table.require(tuple(args), F(args));
  }
}

string input(string names, string type = "long")
{
  return text(type, " ", names, "; read(", names, ");");
}

auto pmod(T, W)(T t, W w)
{
  return (t%w + w) % w;
}

void rep(S, F)(F f, S times)
{
  static if (is(typeof(f(i))))
    foreach(i; 0 .. times)
      f(i);
  else
    foreach(i; 0 .. times)
      f();
}

void multi()
{
  foreach(tc; 0 .. next!long)
    next!TestCase.solve(tc);
}

void single()
{
  auto tc = next!TestCase;
  tc.solve;
}

void main()
{
  type;
}

// Vicfred
// https://codeforces.com/problemset/problem/580/C
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

int n, m, answer;
int[][] tree = new int[][](10^^5+5);
int[] a;

void dfs(int x, int parent, int cats, bool flag) {
    if(a[x] == 1)
        cats += 1;
    if(cats > m)
        flag = true;
    if(tree[x].length == 1 && x != 1 && !flag)
        answer += 1;
    foreach(item; tree[x])
        if(item != parent)
            dfs(item, x, cats*a[x], flag);
}

void main() {
    readf("%s %s", &n, &m);
    readln;
    a = 0~readln.split.map!(to!int).array;

    foreach(i; 1..n) {
        int x, y;
        readf("%s %s", &x, &y);
        readln;
        tree[x] ~= y;
        tree[y] ~= x;
    }

    dfs(1, 0, 0, 0);
    answer.writeln;
}


import std.stdio;
import std.array;

void main() {
  int n, m, l, r, ones = 0, mones = 0;
  "%d %d\n".readf(&n, &m);

  string[] a = readln.split;

  foreach(string i; a) {
    if(i == "1")
      ones++;
    else if(i == "-1")
      mones++;
  }

  foreach(_; 0 .. m) {
    "%d %d\n".readf(&l, &r);
    if(
      (r-l) % 2 == 1 &&
      (r-l+1) / 2 <= ones &&
      (r-l+1) / 2 <= mones
    )
      1.writeln;
    else
      0.writeln;
  }

}

import std.stdio;
import std.ascii;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.random;
import std.regex;
import std.typecons;

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto xs = readln.chomp.split(" ").map!(to!int).array;
    sort(xs);
    int a, b;
    foreach (x; xs) {
        if (x == -1) {
            a++;
        } else {
            b++;
        }
    }
    foreach (i; 0 .. M) {
        int s, t; scanf("%d %d\n", &s, &t);
        int l = t - s + 1;
        if (l % 2 != 0) {
            writeln(0); continue;
        }
        if (a >= l / 2 && b >= l / 2) {
            writeln(1);
        } else {
            writeln(0);
        }
    }
}

import std.stdio;
import std.array;
import std.string;

void main() {
  int n, m, l, r, ones = 0, mones = 0;
  "%d %d\n".readf(&n, &m);

  string[] a = readln.split.array;

  foreach(string i; a) {
    if(i == "1")
      ones++;
    else if(i == "-1")
      mones++;
  }

  foreach(_; 0 .. m) {
    "%d %d\n".readf(&l, &r);
    if(
      (r-l) % 2 == 1 &&
      (r-l+1) / 2 <= ones &&
      (r-l+1) / 2 <= mones
    )
      1.writeln;
    else
      0.writeln;
  }

}

// unihernandez22
// https://codeforces.com/contest/302/problem/A
// greedy

import std.stdio;
import std.array;

void main() {
  int n, m, l, r, ones = 0, mones = 0;
  "%d %d\n".readf(&n, &m);

  int x;
  for(int i = 0; i < n; i++) {
    "%d ".readf(&x);
    if(x == 1)
      ones++;
    else if(x == -1)
      mones++;
  }

  for(int i = 0; i < m; i++) {
    "%d %d\n".readf(&l, &r);
    if(
      (r-l) % 2 == 1 &&
      (r-l+1) / 2 <= ones &&
      (r-l+1) / 2 <= mones
    )
      1.writeln;
    else
      0.writeln;
  }

}


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;
        }

        string[] tokens;
}

void main() {
        IO cin;
        int t = 1;
        // int t = cin.read_int;
        while (t--) {
                int n = cin.read_int;
                int c = cin.read_int;

                int[] arr = new int[n];

                foreach(ref x; arr) {
                        x = cin.read_int;
                }

                int count = 0;
                for (int i = 0; i < n - 1; i++) {
                        if (arr[i + 1] - arr[i] <= c) {
                                count++;
                        } else count = 0;
                }

                writeln(count + 1);
                //plus one since the first word is always added
        }
}

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

void read(out long n, out long c, out long[] arr) {

	readf(" %s %s\n", &n, &c);

	arr = readln.split.map!(to!long).array;
}

long solv(out long res, in long c, in long[] arr) {

	res = 1;
	foreach (idx, el; arr[0 .. $ - 1]) {
		if (arr[idx + 1] - el > c) {
			res = 1;
		} else {
			++res;
		}
	}

	return res;
}

void main() {

	long n, c;
	long[] arr;

	read(n, c, arr);

	long res;
	writeln(solv(res, c, arr));
}

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

void read(out long n, out long c, out long[] arr) {

	readf(" %s %s\n", &n, &c);

	arr = readln.split.map!(to!long).array;
}

long solv(out long res, in long c, in long[] arr) {

	foreach (idx, el; arr[0 .. $ - 1]) {
		if (arr[idx + 1] - el > c) {
			res = 1;
		} else {
			++res;
		}
	}

	return res;
}

void main() {

	long n, c;
	long[] arr;

	read(n, c, arr);

	long res;
	writeln(solv(res, c, arr));
}


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 int[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		auto a = RDA!int;

		auto cnt = new long[](m);
		foreach (e; a)
			++cnt[e%m];

		foreach (i; 1..(m+1)/2)
		{
			auto x = cnt[i];
			auto y = cnt[m-i];
			auto z = min(x, y);
			x -= z;
			y -= z;
			if (z == 0)
				ans[ti] += max(x, y);
			else
				ans[ti] += max(max(x, y), 1);
			debug writeln("i:", i, " ans:", ans[ti]);
		}
		if (cnt[0] != 0)
			++ans[ti];
		if (m % 2 == 0 && cnt[m/2] != 0)
			++ans[ti];
	}

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

// Vicfred
// https://codeforces.com/contest/1497/problem/B
// modular arithmetic, math, greedy
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

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

while(t--) {
    uint n, m;
    readf("%s %s\n", &n, &m);
    long[] a = readln.split.map!(to!long).map!(x => x % m).array;

    long[] counter = new long[m];
    foreach(number; a) {
        counter[cast(uint)number] += 1L;
    }

    //a.writeln;
    //counter.writeln;

    long ans = 0L;
    if(counter[0L] > 0L)
        ans += 1L;

    for(uint i = 1L; i*2 <= m; i++) {
        if(counter[i] == 0L && counter[m - i] == 0L)
            continue;
        ans += 1L;
        if(2*i == m)
            continue;
        //writefln("ans: %s", ans);

        long take = min(counter[i], counter[m - i]);
        //writefln("counter[%s] = %s", i, counter[i]);
        //writefln("counter[%s] = %s", m - i, counter[m - i]);

        counter[i] -= take;
        counter[m - i] -= take;

        if(counter[i] > 0L)
            counter[i] -= 1L;
        if(counter[m - i] > 0L)
            counter[m - i] -= 1L;
        ans += counter[i] + counter[m - i];
    }
    ans.writeln;
}
}

// Vicfred
// https://codeforces.com/contest/1497/problem/B
// modular arithmetic, math, greedy
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

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

while(t--) {
    uint n, m;
    readf("%s %s\n", &n, &m);
    long[] a = readln.split.map!(to!long).map!(x => x % m).array;

    long[] counter = new long[m];
    foreach(number; a) {
        counter[cast(uint)number] += 1L;
    }

    //a.writeln;
    //counter.writeln;

    long ans = 0L;
    if(counter[0L] > 0L)
        ans += 1L;

    for(uint i = 1L; i*2 < m; i++) {
        if(counter[i] == 0L && counter[m - i] == 0L)
            continue;
        ans += 1L;
        //writefln("ans: %s", ans);

        long take = min(counter[i], counter[m - i]);
        //writefln("counter[%s] = %s", i, counter[i]);
        //writefln("counter[%s] = %s", m - i, counter[m - i]);

        counter[i] -= take;
        counter[m - i] -= take;

        if(counter[i] > 0L)
            counter[i] -= 1L;
        if(counter[m - i] > 0L)
            counter[m - i] -= 1L;
        ans += counter[i] + counter[m - i];
    }
    if(counter[m/2] > 1) {
        ans += 1L;
    }
    ans.writeln;
}
}

// Vicfred
// https://codeforces.com/contest/1497/problem/B
// modular arithmetic, math, greedy
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

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

while(t--) {
    uint n, m;
    readf("%s %s\n", &n, &m);
    long[] a = readln.split.map!(to!long).map!(x => x % m).array;

    long[] counter = new long[m];
    foreach(number; a) {
        counter[cast(uint)number] += 1L;
    }

    //a.writeln;
    //counter.writeln;

    long ans = 0L;
    if(counter[0L] > 0L)
        ans += 1L;

    for(uint i = 1L; i*2 < m; i++) {
        if(counter[i] == 0L && counter[m - i] == 0L)
            continue;
        ans += 1L;
        //writefln("ans: %s", ans);

        long take = min(counter[i], counter[m - i]);
        //writefln("counter[%s] = %s", i, counter[i]);
        //writefln("counter[%s] = %s", m - i, counter[m - i]);

        counter[i] -= take;
        counter[m - i] -= take;

        if(counter[i] > 0L)
            counter[i] -= 1L;
        if(counter[m - i] > 0L)
            counter[m - i] -= 1L;
        ans += counter[i] + counter[m - i];
    }
    if(m%2 == 0 && counter[m/2] > 1) {
        ans += 1L;
    }
    ans.writeln;
}
}

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 int[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		auto a = RDA!int;

		auto cnt = new long[](m);
		foreach (e; a)
			++cnt[e%m];

		foreach (i; 1..(m+1)/2)
		{
			auto x = cnt[i];
			auto y = cnt[m-i];
			auto z = min(x, y);
			++ans[ti];
			x -= z;
			y -= z;
			if (z == 0)
				ans[ti] += max(x, y);
			else
				ans[ti] += max(max(x, y) - 1, 0);
		}
		if (cnt[0] != 0)
			++ans[ti];
		if (m % 2 == 0 && cnt[m/2] != 0)
			++ans[ti];
	}

	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 int[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		auto a = RDA!int;

		auto cnt = new long[](m);
		foreach (e; a)
			++cnt[e%m];

		foreach (i; 1..(m+1)/2)
		{
			auto x = cnt[i];
			auto y = cnt[m-i];
			auto z = min(x, y);
			++ans[ti];
			x -= z;
			y -= z;
			ans[ti] += max(max(x, y) - 1, 0);
		}
		if (cnt[0] != 0)
			++ans[ti];
		if (m % 2 == 0 && cnt[m/2] != 0)
			++ans[ti];
	}

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


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto b = readln.splitter.map !(to !(int)).array;
		sort (a);
		sort (b);
		reverse (b);
		foreach (i; 0..k)
		{
			if (a[i] < b[i])
			{
				swap (a[i], b[i]);
			}
		}
		writeln (sum (a));
	}
}

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) { x.modm(y.modpow(mod - 2)); }

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

		a.sort!"a > b"();
		b.sort!"a > b"();

		foreach (i; 0..n-k)
		{
			ans[ti] += a.front; a.popFront;
		}
		foreach (i; 0..k)
		{
			if (a.front > b.front)
			{
				ans[ti] += a.front; a.popFront;
			}
			else
			{
				ans[ti] += b.front; b.popFront;
			}
		}
	}

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


import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int n = rint;
	long[] as = rlong(n);

	long sum = as.sum;
	long[] ds;
	for(long d = 1; d * d <= sum; d ++) if(sum % d == 0) ds ~= d, ds ~= sum / d;
	log("as:", as, "sum:", sum, "ds:", ds);
	// ※素数に絞っても良いはず

	long ans = as.sum * n;
	foreach(d; ds){
		if(d == 1) continue;
		long leftover;
		long tempans;
		long q;
		foreach(i, a; as){
			q += a, q %= d;
			if(q < d - q) tempans += q;
			else tempans += d - q;
			log("i:", i, "q:", q, "tempans:", tempans);
		}
		ans = min(ans, tempans);
		log("tempans:", tempans, "ans:", ans);
	}
	if(ans >= as.sum * n) ans = -1;
	ans.writeln;
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto total = sum (a, 0L);
		if (total == 1)
		{
			writeln (-1);
			continue;
		}

		long [] p;
		for (long d = 2; d * d <= total; d++)
		{
			if (total % d == 0)
			{
				p ~= d;
				while (total % d == 0)
				{
					total /= d;
				}
			}
		}
		if (total > 1)
		{
			p ~= total;
		}

		long solve (long d)
		{
			long cur = 0;
			long res = 0;
			foreach (i; 0..n - 1)
			{
				cur += a[i];
				cur %= d;
				res += min (cur, d - cur);
			}
			return res;
		}

		long res = total * n;
		foreach (ref c; p)
		{
			res = min (res, solve (c));
		}
		writeln (res);
	}
}

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.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)); }

enum INF = 10L^^18;

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      auto A = new long[N];
      foreach (i; 0 .. N) {
        A[i] = readLong();
      }

      const S = A.sum;
      long[] ps;
      long s = S;
      for (long p = 2; p * p <= s; ++p) {
        if (s % p == 0) {
          do {
            s /= p;
          } while (s % p == 0);
          ps ~= p;
        }
      }
      if (s > 1) {
        ps ~= s;
      }
      debug {
        writeln("ps = ", ps);
      }

      long ans = INF;
      foreach (p; ps) {
        long cost;
        long now;
        foreach (i; 0 .. N) {
          now += A[i];
          now %= p;
          cost += min(now, p - now);
        }
        debug {
          writeln(p, ": ", cost);
        }
        chmin(ans, cost);
      }
      writeln((ans >= INF) ? -1 : ans);
    }
  } catch (EOFException e) {
  }
}


// Vicfred
// https://codeforces.com/contest/1437/problem/A
// math
import std.stdio;

void main() {
    long t;
    readf("%s\n", &t);

    long l, r;
    while(t--) {
        readf("%s %s\n", &l, &r);
        if(2L * l > r)
            "YES".writeln;
        else
            "NO".writeln;
    }
}

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 bool[](t);
	foreach (ti; 0..t)
	{
		auto l = RD;
		auto r = RD;
		auto a = r+1;
		ans[ti] = l*2 >= a;
	}

	foreach (e; ans)
		writeln(e ? "YES" : "NO");
	stdout.flush;
	debug readln;
}

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)
	{
		int lo, hi;
		readf !(" %s %s") (lo, hi);
		auto mod = hi + 1;
		writeln ((lo % mod) * 2 >= mod ? "YES" : "NO");
	}
}

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 bool[](t);
	foreach (ti; 0..t)
	{
		auto l = RD;
		auto r = RD;
		auto a = (l-1)*2 + 1;
		ans[ti] = a > r;
	}

	foreach (e; ans)
		writeln(e ? "YES" : "NO");
	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(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 bool[](t);
	foreach (ti; 0..t)
	{
		auto l = RD;
		auto r = RD;
		auto a = r + 1;
		ans[ti] = l * 2 > a;
	}

	foreach (e; ans)
		writeln(e ? "YES" : "NO");
	stdout.flush;
	debug readln;
}


import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;

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

//	chmin/chmax
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

int N, M;
real[] X;
int[] A, B;
real[] C;

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		M = readInt;
		X = new real[N];
		foreach (u; 0 .. N) {
			X[u] = readReal;
		}
		A = new int[M];
		B = new int[M];
		C = new real[M];
		foreach (i; 0 .. M) {
			A[i] = readInt - 1;
			B[i] = readInt - 1;
			C[i] = readReal;
		}

		real ans = 0.0;
		foreach (i; 0 .. M) {
			if (C[i] > 0) {
				chmax(ans, (X[A[i]] + X[B[i]]) / C[i]);
			}
		}
		writefln("%.10f", ans);

	}
	} catch (EOFException) {}
}

import std.stdio;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

void main() {
    int N, M;
    scanf("%d %d\n", &N, &M);
    int[] X = readln.chomp.split(" ").map!(to!int).array;
    double Ans = 0;
    for (int i = 0; i < M; i++) {
        int a, b, c; scanf("%d %d %d\n", &a, &b, &c);
        a--; b--;
        Ans = max(Ans, cast(double)(X[a] + X[b]) / c);
    }
    writefln("%.12f", Ans);
}


import std.stdio, std.array, std.algorithm : sort, copy;
import std.conv;
import std.string;

int main(string[] argv)
{
	auto input = stdin.readln().chomp().split();
	auto leng = to!int(input[0]);
	auto curCountIceCream = to!long(input[1]);

    string line;
	int notSatisfiedCount = 0;
	int curLen = 0;
    while ((line = readln().chomp()) !is null)
	{
		auto newLine = removechars(line, ['\x20']);
		auto curInput = to!long(newLine);
		if ( curCountIceCream + curInput >= 0 )
			curCountIceCream += curInput;
		else
			notSatisfiedCount++;
		curLen++;
		if ( curLen == leng)
			break;
	}

	writeln( curCountIceCream , " " , notSatisfiedCount);

    return 0;
}

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

class Ice_Cream {

    this(long x) { this.x = x; }

    void setX(long d) { x += d; }

    protected static long x;
}

class Kay : Ice_Cream {

    this() { super(x); }
    void setRest(long d) { Kay.x -= d; }

    @property long rest() { return Kay.x; }
}

class Gerda : Ice_Cream {

    this() { super(x); }

    void setSad() { _sad++; }

    @property long sad() { return _sad; }

    private long _sad;
}

void main() {

    long n, x;

    readf(" %s %s ", &n, &x);

    Ice_Cream ice = new Ice_Cream(x);
    Kay kay = new Kay();
    Gerda gerda = new Gerda();

    long d;
    char child_work;

    foreach (idx; 0 .. n) {

        readf(" %s %s ", &child_work, &d);

        if (child_work == '+') {
            ice.setX(d);
        } else {
            if (ice.x < d) {
                gerda.setSad();
            } else {
                kay.setRest(d);
            }
        }
    }

    writeln(kay.rest, ' ', gerda.sad);
}

import std.stdio, std.array, std.algorithm : sort, copy;
import std.conv;
import std.string;

int main(string[] argv)
{
	auto input = stdin.readln().chomp().split();
	auto leng = to!int(input[0]);
	auto curCountIceCream = to!int(input[1]);

    string line;
	int notSatisfiedCount = 0;
	int curLen = 0;
    while ((line = readln().chomp()) !is null)
	{
		auto newLine = removechars(line, ['\x20']);
		auto curInput = to!int(newLine);
		if ( curCountIceCream + curInput >= 0 )
			curCountIceCream += curInput;
		else
			notSatisfiedCount++;
		curLen++;
		if ( curLen == leng)
			break;
	}

	writeln( curCountIceCream , " " , notSatisfiedCount);

    return 0;
}

import std.stdio, std.array, std.algorithm : sort, copy;
import std.conv;
import std.string;

int main(string[] argv)
{
	auto input = stdin.readln().chomp().split();
	auto leng = to!int(input[0]);
	auto curCountIceCream = to!int(input[1]);

    string line;
	int notSatisfiedCount = 0;
	long curLen = 0;
    while ((line = readln().chomp()) !is null)
	{
		auto newLine = removechars(line, ['\x20']);
		auto curInput = to!long(newLine);
		if ( curCountIceCream + curInput >= 0 )
			curCountIceCream += curInput;
		else
			notSatisfiedCount++;
		curLen++;
		if ( curLen == leng)
			break;
	}

	writeln( curCountIceCream , " " , notSatisfiedCount);

    return 0;
}


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)); }

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

void main() {
  try {
    for (; ; ) {
      const M = readInt();
      const N = readInt();
      auto A = new string[M];
      foreach (x; 0 .. M) {
        A[x] = readToken();
      }

      auto uf = new int[M + N];
      uf[] = -1;
      foreach (x; 0 .. M) foreach (y; 0 .. N) {
        if (A[x][y] == '#') {
          uf.connect(x, M + y);
        }
      }
      auto board = new char[][](M, N);
      foreach (x; 0 .. M) foreach (y; 0 .. N) {
        board[x][y] = (uf.root(x) == uf.root(M + y)) ? '#' : '.';
      }
      bool ans = true;
      foreach (x; 0 .. M) foreach (y; 0 .. N) {
        ans = ans && (A[x][y] == board[x][y]);
      }
      writeln(ans ? "Yes" : "No");
    }
  } catch (EOFException e) {
  }
}

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

void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}

void main()
{
  int n, m; readV(n, m);
  string[] s; readM(n, s);

  s = s.sort().uniq.array;

  auto b = new int[](m);
  foreach (si; s)
    b[] += si.map!(c => c == '#' ? 1 : 0).array[];

  writeln(b.all!"a<=1" ? "Yes" : "No");
}

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 readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}

void main()
{
  int n, m; readV(n, m);
  string[] s; readC(n, s);

  s = s.sort().uniq.array;

  auto b = new int[](m);
  foreach (si; s)
    b[] += si.map!(c => c == '#' ? 1 : 0).array[];

  writeln(b.all!"a<=1" ? "Yes" : "No");
}

/+ dub.sdl:
    name "A"
    dependency "dunkelheit" version=">=0.9.0"
+/

import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;

int main() {
    Scanner sc = new Scanner(stdin);
    scope(exit) assert(!sc.hasNext);

    int h, w;
    sc.read(h, w);

    bool[][] g = new bool[][](h, w);
    foreach (i; 0..h) {
        string s;
        sc.read(s);
        foreach (j; 0..w) {
            g[i][j] = (s[j] == '#');
        }
    }

    bool[][] g2 = new bool[][](h, w);

    foreach (i; 0..h) {
        foreach (j; 0..w) {
            if (!g[i][j]) continue;
            int[] hv, wv;
            foreach (i2; 0..h) {
                if (g[i2][j]) hv ~= i2;
            }
            foreach (j2; 0..w) {
                if (g[i][j2]) wv ~= j2;
            }

            foreach (i2; hv) {
                foreach (j2; wv) {
                    g2[i2][j2] = true;
                }
            }
        }
    }

    foreach (i; 0..h) {
        foreach (j; 0..w) {
            if (g[i][j] != g2[i][j]) {
                writeln("No");
                return 0;
            }
        }
    }
    writeln("Yes");

    // foreach (v; g2) {
    //     writeln(v.map!(x => x ? '#' : '.'));
    // }
    return 0;
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;

struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
    import core.exception : RangeError;

    private T* _data;
    private uint len, cap;

    @property bool empty() const { return len == 0; }
    @property size_t length() const { return len; }
    alias opDollar = length;

    inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }

    ref inout(T) opIndex(size_t i) inout {
        version(assert) if (len <= i) throw new RangeError();
        return _data[i];
    }
    ref inout(T) front() inout { return this[0]; }
    ref inout(T) back() inout { return this[$-1]; }

    void reserve(size_t newCap) {
        import core.memory : GC;
        import core.stdc.string : memcpy;
        import std.conv : to;
        if (newCap <= cap) return;
        void* newData = GC.malloc(newCap * T.sizeof);
        cap = newCap.to!uint;
        if (len) memcpy(newData, _data, len * T.sizeof);
        _data = cast(T*)(newData);
    }
    void free() {
        import core.memory : GC;
        GC.free(_data);
    }

    void clear() {
        len = 0;
    }

    void insertBack(T item) {
        import std.algorithm : max;
        if (len == cap) reserve(max(cap * 2, MINCAP));
        _data[len++] = item;
    }
    alias opOpAssign(string op : "~") = insertBack;
    void removeBack() {
        assert(!empty, "StackPayload.removeBack: Stack is empty");
        len--;
    }
}

/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */

// module dkh.foundation;

static if (__VERSION__ <= 2070) {
    /*
    Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
    Copyright: Andrei Alexandrescu 2008-.
    License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
    */
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }

}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;

// import dkh.container.stackpayload;

class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray;
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succW() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (!line.empty && line.front.isWhite) {
            line.popFront;
        }
        return !line.empty;
    }
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            line = lineBuf[];
            f.readln(line);
            if (!line.length) return false;
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {

                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else static if (isStaticArray!T) {
                foreach (i; 0..T.length) {
                    bool f = succW();
                    assert(f);
                    x[i] = line.parse!E;
                }
            } else {
                StackPayload!E buf;
                while (succW()) {
                    buf ~= line.parse!E;
                }
                x = buf.data;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }

    int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
    void read(Args...)(auto ref Args args) {
        import std.exception;
        static if (args.length != 0) {
            enforce(readSingle(args[0]));
            read(args[1..$]);
        }
    }
    bool hasNext() {
        return succ();
    }
}

/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/


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

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

    auto mx = new int[] (n+1);
    foreach (i; 1 .. n+1) { mx[i] = i; }

    struct UF {
        int[] p, r;

        this (int n) {
            p = new int[] (n+1);
            r = new int[] (n+1);
            foreach (i; 1 .. n+1) {
                p[i] = i;
                r[i] = 1;
            }
        }

        int fnd(int v) {
            if (p[v] != v) {
                p[v] = fnd(p[v]);
            }

            return p[v];
        }

        void uni(int a, int b) {
            a = fnd(a);
            b = fnd(b);

            if (a == b) { return; }

            if (r[a] > r[b]) { swap(a, b); }

            p[a] = b;

            if (r[a] == r[b]) { ++r[b]; }
        }
    }

    auto uf = UF(n);

    foreach (_; 0 .. m) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        mx[u] = max(mx[u], v);
        mx[v] = max(mx[v], u);

        uf.uni(u, v);
    }

    int ans = 0;
    int le = 1;
    while (le <= n) {
        int r = mx[le];
        int p = le+1;
        while (p <= r) {
            if (uf.fnd(le) != uf.fnd(p)) {
                ans += 1;
                uf.uni(le, p);
            }
            r = max(r, mx[p]);
            ++p;
        }

        le = r + 1;
    }

    ans.writeln;
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import 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; }

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998244353;
//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; }

struct UnionFind
{
	void init(int n) { par = new int[](n); foreach (i; 0..n) par[i] = i; cnt = new int[](n); fill(cnt, 1); }
	int root(int i) { return par[i] == i ? i : (par[i] = root(par[i])); }
	bool same(int i, int j) { return root(i) == root(j); }
	void unite(int i, int j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; cnt[j] += cnt[i]; }
	int size(int i) { return cnt[root(i)]; }
	int[] par, cnt;
}

void main()
{
	auto n = RD!int;
	auto m = RD!int;
	UnionFind uf;
	uf.init(n);
	foreach (i; 0..m)
	{
		auto u = RD!int-1;
		auto v = RD!int-1;
		uf.unite(u, v);
	}

	auto r = new int[](n);
	auto lr = new int[][](n, 2);
	foreach (i; 0..n)
		lr[i][0] = int.max;

	foreach (i; 0..n)
	{
		auto p = uf.root(i);
		r[i] = p;
		lr[p][0] = min(lr[p][0], i);
		lr[p][1] = max(lr[p][1], i);
	}

	auto index = lr.MAKE_IDX!"a[0] < b[0]";
	auto rMax = new int[](n+1);
	foreach (i; 0..n)
	{
		rMax[i+1] = max(rMax[i], lr[index[i]][1]);
	}
	long ans;
	foreach (ii, i; index)
	{
		if (lr[i][0] == int.max) break;
		if (lr[i][0] < rMax[ii])
			++ans;
	}

	writeln(ans);
	stdout.flush();
	debug readln();
}

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;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto G = new int[][](N);
    auto uf = new UnionFind(N);

    foreach (i; 0..M) {
        s = readln.split.map!(to!int);
        auto a = s[0] - 1;
        auto b = s[1] - 1;
        G[a] ~= b;
        G[b] ~= a;
        uf.unite(a, b);
    }

    Tuple!(int, int)[] A;
    foreach (i; 0..N) if (uf.table[i] < 0) A ~= tuple(uf.mn[i], uf.mx[i]);

    A.sort!"a[0] < b[0]";
    auto t = A.front;
    int ans = 0;

    foreach (i; 1..A.length.to!int) {
        if (A[i][0] > t[1]) {
            t = A[i];
        } else {
            ans += 1;
            t[1] = max(t[1], A[i][1]);
        }
    }

    ans.writeln;
}

class UnionFind {
    import std.algorithm : swap;

    int n;
    int[] table;
    int[] mn;
    int[] mx;

    this(int n) {
        this.n = n;
        table = new int[](n);
        table[] = -1;
        mn = n.iota.array;
        mx = n.iota.array;
    }

    int find(int x) {
        return table[x] < 0 ? x : (table[x] = find(table[x]));
    }

    void unite(int x, int y) {
        int ox = x;
        int oy = y;
        x = find(x);
        y = find(y);
        mn[x] = min(mn[x], mn[y], ox, oy);
        mx[x] = max(mx[x], mx[y], ox, oy);
        if (x == y) return;
        if (table[x] > table[y]) swap(x, y);
        mn[x] = min(mn[x], mn[y], ox, oy);
        mx[x] = max(mx[x], mx[y], ox, oy);
        table[x] += table[y];
        table[y] = x;
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int n = rint, m = rint;
	Node[] nodes;
	foreach(i; 0 .. n) nodes ~= new Node(i, 0);

	X[] xs;
	foreach(j; 0 .. m){
		int a = rint - 1, b = rint - 1;
		if(a < b) xs ~= X(a, b);
		else xs ~= X(b, a);
	}
	log("xs:", xs);

	int[] us = new int[](n + 9);
	foreach(x; xs) us[x.a] += 1, us[x.b] -= 1;
	log("us:", us);

	int rangecount;
	int sum;
	foreach(i; 0 .. n){
		if(sum == 0 && us[i] > 0) rangecount += 1;
		if(sum > 0 || sum + us[i] > 0) nodes[i].value = 1;
		sum += us[i];
	}
	log("rangecount:", rangecount);

	foreach(x; xs) nodes[x.a].group.eat(nodes[x.b].group);
	int groupcount;
	foreach(nd; nodes){
		if(nd.value > 0 && nd.group.id == nd.id) groupcount += 1;
	}
	log("groupcount:", groupcount);

	int ans = groupcount - rangecount;
	ans.writeln;

}
struct X{
	int a, b;
}
/*

count how many distinct segment unions.
count how many connected subgraphs.

*/

// Union-Find 整理されているもの ※Edgeは使わなくてもよい
// 使い方は nd1.group.eat(nd2.group);
class Edge{
	Node node1;
	Node node2;
	long value;
	this(Node node1, Node node2, long value){
		this.node1 = node1, this.node2 = node2;
		this.value = value;
	}
}

class Node{
	long id;
	long value;
	Group group;
	this(long id, long value){
		this.id = id;
		this.value = value;
		this.group = new Group(this);
	}
}

class Group{
	long value;
	long id;
	Node[] nodes;
	this(Node node){
		this.id = node.id;
		this.nodes = [node];
		this.value = node.value;
	}
	void eat(Group gp){
		if(this.id == gp.id) return;
		if(gp.nodes.length > this.nodes.length) gp.eat(this);
		else{
			foreach(nd; gp.nodes){
				this.nodes ~= nd;
				nd.group = this;
				this.value += nd.value;
			}
		}
	}
}

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;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto G = new int[][](N);
    auto uf = new UnionFind(N);

    foreach (i; 0..M) {
        s = readln.split.map!(to!int);
        auto a = s[0] - 1;
        auto b = s[1] - 1;
        G[a] ~= b;
        G[b] ~= a;
        uf.unite(a, b);
    }

    Tuple!(int, int)[] A;
    foreach (i; 0..N) if (uf.table[i] < 0) A ~= tuple(uf.mn[i], uf.mx[i]);

    A.sort!"a[0] < b[0]";
    auto t = A.front;
    int ans = 0;

    foreach (i; 1..A.length.to!int) {
        if (A[i][0] > t[1]) {
            t = A[i];
        } else {
            ans += 1;
            t[1] = max(t[1], A[i][1]);
        }
    }

    ans.writeln;
}

class UnionFind {
    import std.algorithm : swap;

    int n;
    int[] table;
    int[] mn;
    int[] mx;

    this(int n) {
        this.n = n;
        table = new int[](n);
        table[] = -1;
        mn = n.iota.array;
        mx = n.iota.array;
    }

    int find(int x) {
        return table[x] < 0 ? x : (table[x] = find(table[x]));
    }

    void unite(int x, int y) {
        int ox = x;
        int oy = y;
        x = find(x);
        y = find(y);
        mn[x] = min(mn[x], ox, oy);
        mx[x] = max(mx[x], ox, oy);
        if (x == y) return;
        if (table[x] > table[y]) swap(x, y);
        table[x] += table[y];
        table[y] = x;
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }
}

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;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto G = new int[][](N);
    auto uf = new UnionFind(N);

    foreach (i; 0..M) {
        s = readln.split.map!(to!int);
        auto a = s[0] - 1;
        auto b = s[1] - 1;
        G[a] ~= b;
        G[b] ~= a;
        uf.unite(a, b);
    }

    Tuple!(int, int)[] A;
    foreach (i; 0..N) if (uf.table[i] < 0) A ~= tuple(uf.mn[i], uf.mx[i]);

    A.sort!"a[0] < b[0]";
    auto t = A.front;
    int ans = 0;

    foreach (i; 1..A.length.to!int) {
        if (A[i][0] > t[1]) {
            t = A[i];
        } else {
            ans += 1;
            t[1] = A[i][1];
        }
    }

    ans.writeln;
}

class UnionFind {
    import std.algorithm : swap;

    int n;
    int[] table;
    int[] mn;
    int[] mx;

    this(int n) {
        this.n = n;
        table = new int[](n);
        table[] = -1;
        mn = n.iota.array;
        mx = n.iota.array;
    }

    int find(int x) {
        return table[x] < 0 ? x : (table[x] = find(table[x]));
    }

    void unite(int x, int y) {
        int ox = x;
        int oy = y;
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (table[x] > table[y]) swap(x, y);
        mn[x] = min(mn[x], ox, oy);
        mx[x] = max(mx[x], ox, oy);
        table[x] += table[y];
        table[y] = x;
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }
}

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;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto G = new int[][](N);
    auto uf = new UnionFind(N);

    foreach (i; 0..M) {
        s = readln.split.map!(to!int);
        auto a = s[0] - 1;
        auto b = s[1] - 1;
        G[a] ~= b;
        G[b] ~= a;
        uf.unite(a, b);
    }

    Tuple!(int, int)[] A;
    foreach (i; 0..N) if (uf.table[i] < 0) A ~= tuple(uf.mn[i], uf.mx[i]);

    A.sort!"a[0] < b[0]";
    auto t = A.front;
    int ans = 0;

    foreach (i; 1..A.length.to!int) {
        if (A[i][0] > t[1]) {
            t = A[i];
        } else {
            ans += 1;
            t[1] = max(t[1], A[i][1]);
        }
    }

    ans.writeln;
}

class UnionFind {
    import std.algorithm : swap;

    int n;
    int[] table;
    int[] mn;
    int[] mx;

    this(int n) {
        this.n = n;
        table = new int[](n);
        table[] = -1;
        mn = n.iota.array;
        mx = n.iota.array;
    }

    int find(int x) {
        return table[x] < 0 ? x : (table[x] = find(table[x]));
    }

    void unite(int x, int y) {
        int ox = x;
        int oy = y;
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (table[x] > table[y]) swap(x, y);
        mn[x] = min(mn[x], ox, oy);
        mx[x] = max(mx[x], ox, oy);
        table[x] += table[y];
        table[y] = x;
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }
}

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;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto G = new int[][](N);
    auto uf = new UnionFind(N);

    foreach (i; 0..M) {
        s = readln.split.map!(to!int);
        auto a = s[0] - 1;
        auto b = s[1] - 1;
        G[a] ~= b;
        G[b] ~= a;
        uf.unite(a, b);
    }

    Tuple!(int, int)[] A;
    foreach (i; 0..N) if (uf.table[i] < 0) A ~= tuple(uf.mn[i], uf.mx[i]);

    A.sort!"a[0] < b[0]";
    auto t = A.front;
    int ans = 0;

    foreach (i; 1..A.length.to!int) {
        if (A[i][0] > t[1]) {
            t = A[i];
        } else {
            ans += 1;
            t[1] = max(t[1], A[i][1]);
        }
    }

    ans.writeln;
}

class UnionFind {
    import std.algorithm : swap;

    int n;
    int[] table;
    int[] mn;
    int[] mx;

    this(int n) {
        this.n = n;
        table = new int[](n);
        table[] = -1;
        mn = n.iota.array;
        mx = n.iota.array;
    }

    int find(int x) {
        return table[x] < 0 ? x : (table[x] = find(table[x]));
    }

    void unite(int x, int y) {
        int ox = x;
        int oy = y;
        x = find(x);
        y = find(y);
        mn[x] = min(mn[x], ox, oy);
        mx[x] = max(mx[x], ox, oy);
        if (x == y) return;
        if (table[x] > table[y]) swap(x, y);
        mn[x] = min(mn[x], ox, oy);
        mx[x] = max(mx[x], ox, oy);
        table[x] += table[y];
        table[y] = x;
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int n = rint, m = rint;
	Node[] nodes;
	foreach(i; 0 .. n) nodes ~= new Node(i, 0);

	X[] xs;
	foreach(j; 0 .. m){
		xs ~= X(rint - 1, rint - 1);
	}
	log("xs:", xs);

	foreach(x; xs){
		nodes[x.a].value = 1;
		nodes[x.b].value = 1;
		nodes[x.a].group.eat(nodes[x.b].group);
	}
	int groupcount;
	foreach(nd; nodes){
		if(nd.value > 0 && nd.group.id == nd.id) groupcount += 1;
	}
	log("groupcount:", groupcount);

	int[] us = new int[](n + 9);
	foreach(x; xs) us[x.a] += 1, us[x.b] -= 1;
	log("us:", us);

	int rangecount;
	int sum;
	foreach(i; 0 .. n){
		if(sum == 0 && us[i] > 0) rangecount += 1;
		sum += us[i];
	}
	log("rangecount:", rangecount);

	int ans = groupcount - rangecount;
	ans.writeln;

}
struct X{
	int a, b;
}
/*

count how many distinct segment unions.
count how many connected subgraphs.

*/

// Union-Find 整理されているもの ※Edgeは使わなくてもよい
// 使い方は nd1.group.eat(nd2.group);
class Edge{
	Node node1;
	Node node2;
	long value;
	this(Node node1, Node node2, long value){
		this.node1 = node1, this.node2 = node2;
		this.value = value;
	}
}

class Node{
	long id;
	long value;
	Group group;
	this(long id, long value){
		this.id = id;
		this.value = value;
		this.group = new Group(this);
	}
}

class Group{
	long value;
	long id;
	Node[] nodes;
	this(Node node){
		this.id = node.id;
		this.nodes = [node];
		this.value = node.value;
	}
	void eat(Group gp){
		if(this.id == gp.id) return;
		if(gp.nodes.length > this.nodes.length) gp.eat(this);
		else{
			foreach(nd; gp.nodes){
				this.nodes ~= nd;
				nd.group = this;
				this.value += nd.value;
			}
		}
	}
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int n = rint, m = rint;
	Node[] nodes;
	foreach(i; 0 .. n) nodes ~= new Node(i, 0);

	X[] xs;
	foreach(j; 0 .. m){
		xs ~= X(rint - 1, rint - 1);
	}
	log("xs:", xs);

	int[] us = new int[](n + 9);
	foreach(x; xs) us[x.a] += 1, us[x.b] -= 1;
	log("us:", us);

	int rangecount;
	int sum;
	foreach(i; 0 .. n){
		if(sum == 0 && us[i] > 0) rangecount += 1;
		if(sum > 0 || sum + us[i] > 0) nodes[i].value = 1;
		sum += us[i];
	}
	log("rangecount:", rangecount);

	foreach(x; xs) nodes[x.a].group.eat(nodes[x.b].group);
	int groupcount;
	foreach(nd; nodes){
		if(nd.value > 0 && nd.group.id == nd.id) groupcount += 1;
	}
	log("groupcount:", groupcount);

	int ans = groupcount - rangecount;
	ans.writeln;

}
struct X{
	int a, b;
}
/*

count how many distinct segment unions.
count how many connected subgraphs.

*/

// Union-Find 整理されているもの ※Edgeは使わなくてもよい
// 使い方は nd1.group.eat(nd2.group);
class Edge{
	Node node1;
	Node node2;
	long value;
	this(Node node1, Node node2, long value){
		this.node1 = node1, this.node2 = node2;
		this.value = value;
	}
}

class Node{
	long id;
	long value;
	Group group;
	this(long id, long value){
		this.id = id;
		this.value = value;
		this.group = new Group(this);
	}
}

class Group{
	long value;
	long id;
	Node[] nodes;
	this(Node node){
		this.id = node.id;
		this.nodes = [node];
		this.value = node.value;
	}
	void eat(Group gp){
		if(this.id == gp.id) return;
		if(gp.nodes.length > this.nodes.length) gp.eat(this);
		else{
			foreach(nd; gp.nodes){
				this.nodes ~= nd;
				nd.group = this;
				this.value += nd.value;
			}
		}
	}
}