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

long calc(long n, long k) {
    long d = k / (n-1);
    return k + d + (k % (n-1) == 0 ? -1 : 0);
}

void main() {
    int t; scan(t);
    foreach (_; 0..t) {
        int n, k; scan(n, k);
        writeln(calc(n, k));
    }
}

void scan(T...)(ref T a) {
    string[] ss = readln.split;
    foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T=string)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readints = reads!int;

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

T readNum(T)(){
  return readStr.to!T;
}

T[] readNums(T)(){
  return readStr.split.to!(T[]);
}

string readStr(){
  return readln.chomp;
}

void main(){
  auto t = readNum!int;

  foreach(i; 0 .. t){
    auto nk = readNums!int;
    auto skip = (nk[1]-1) / (nk[0]-1);

    writeln(nk[1] + skip);
  }
}

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;
		auto k = RD;

		long x = k;
		long cnt;
		while (x >= n)
		{
			cnt += x / n;
			x = (x % n) + x / n;
		}
		ans[ti] = k+cnt;
	}

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

void main() {
	auto t = ri;
	foreach(T; 0..t) {
		auto ip = readAs!(long[]), n = ip[0], k = ip[1];

		long l = 0, r = 1L << 60;
		long m;

		while(l + 1 < r) {
			//debug "%d, %d".writefln(l, r);
			m = (l+r) / 2;
			debug writefln("%d - (%d / %d) = %d, %d", m, m, n, m - (m / n), k);
			if(m - (m / n) < k) {
				debug "ok".writeln;
				l = m;
			} else r = m;
		}

		(l+1).writeln;
	}
}

// ===================================

import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;

T readAs(T)() if (isBasicType!T) {
	return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
	return readln.split.to!T;
}

T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		res[i] = readAs!(T[]);
	}
	return res;
}

T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		auto s = rs;
		foreach(j; 0..width) res[i][j] = s[j].to!T;
	}
	return res;
}

int ri() {
	return readAs!int;
}

long rl() {
	return readAs!long;
}

double rd() {
	return readAs!double;
}

string rs() {
	return readln.chomp;
}

import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_t; 0..T) {
        auto nk = readln.split.to!(long[]);
        auto N = nk[0];
        auto K = nk[1];

        if (K < N) {
            writeln(K);
            continue;
        }

        long l = 1, r = K*2+1;
        while (l+1 < r) {
            auto m = (l+r)/2;
            auto e = m/N;
            if (m-e >= K) {
                r = m;
            } else {
                l = m;
            }
        }
        writeln(r);
    }
}

// Author: Ivan Kazmenko (gassa@mail.ru)
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 n, k;
		readf !(" %s %s") (n, k);
		auto res = k * 1L * n / (n - 1) - 5;
		while (res - res / n < k)
		{
			res += 1;
		}
		writeln (res);
	}
}

import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_t; 0..T) {
        auto nk = readln.split.to!(long[]);
        auto N = nk[0];
        auto K = nk[1];

        long l = 1, r = K*2+1;
        while (l+1 < r) {
            auto m = (l+r)/2;
            auto e = m/N;
            if (m-e >= K) {
                r = m;
            } else {
                l = m;
            }
        }
        writeln(r);
    }
}

// Author: Ivan Kazmenko (gassa@mail.ru)
// does not work for some inputs, for example, n = 4 and k = 2
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 n, k;
		readf !(" %s %s") (n, k);
		auto res = k * 1L * n / (n - 1) - 1;
		if (res % n == 0)
		{
			res += 1;
		}
		writeln (res);
	}
}


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

void get(Args...)(ref Args args)
{
    import std.traits, std.meta, std.typecons;

    static if (Args.length == 1) {
        alias Arg = Args[0];

        static if (isArray!Arg) {
          static if (isSomeChar!(ElementType!Arg)) {
            args[0] = readln.chomp.to!Arg;
          } else {
            args[0] = readln.split.to!Arg;
          }
        } else static if (isTuple!Arg) {
            auto input = readln.split;
            static foreach (i; 0..Fields!Arg.length) {
                args[0][i] = input[i].to!(Fields!Arg[i]);
            }
        } else {
            args[0] = readln.chomp.to!Arg;
        }
    } else {
        auto input = readln.split;
        assert(input.length == Args.length);

        static foreach (i; 0..Args.length) {
            args[i] = input[i].to!(Args[i]);
        }
    }
}

void get_lines(Args...)(size_t N, ref Args args)
{
    import std.traits, std.range;

    static foreach (i; 0..Args.length) {
        static assert(isArray!(Args[i]));
        args[i].length = N;
    }

    foreach (i; 0..N) {
        static if (Args.length == 1) {
            get(args[0][i]);
        } else {
            auto input = readln.split;
            static foreach (j; 0..Args.length) {
                args[j][i] = input[j].to!(ElementType!(Args[j]));
            }
        }
    }
}

void main()
{
    int T; get(T);
    while (T--) {
        int x, y; get(x, y);
        char[] s; get(s);
        int max_x, min_x, max_y, min_y;
        foreach (c; s) {
            switch (c) {
                case 'U': ++max_y; break;
                case 'D': --min_y; break;
                case 'R': ++max_x; break;
                case 'L': --min_x; break;
                default:
            }
        }
        writeln(min_x <= x && x <= max_x && min_y <= y && y <= max_y ? "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(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 bool[](t);
	foreach (ti; 0..t)
	{
		auto x = RD;
		auto y = RD;
		auto s = RD!string;

		long u, d, r, l;
		foreach (c; s)
		{
			if (c == 'U')
				++u;
			else if (c == 'D')
				--d;
			else if (c == 'R')
				++r;
			else
				--l;
		}
		ans[ti] = true;
		if (x > r || x < l)
			ans[ti] = false;
		if (y > u || y < d)
			ans[ti] = false;
	}

	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 x, y;
		readf !(" %s %s") (x, y);
		readln;
		auto s = readln.strip;
		bool ok = true;
		ok &= (s.count ('R').to !(int) >= +x);
		ok &= (s.count ('L').to !(int) >= -x);
		ok &= (s.count ('U').to !(int) >= +y);
		ok &= (s.count ('D').to !(int) >= -y);
		writeln (ok ? "YES" : "NO");
	}
}


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

int search(long[] arr, int n, long t)
{
    auto left = 0, right = n - 1;
    auto res = -1;
    while (left <= right)
    {
        auto mid = (left + right) >> 1;
        if (arr[mid] >= t)
        {
            right = mid - 1;
        }
        else
        {
            left = mid + 1;
            res = mid;
        }
    }
    return res + 1;
}

long count(int[] a, int left, int right, long t)
{
    if (left == right)
    {
        if (a[left] < t) return 1;
        return 0;
    }
    long res = 0;
    auto mid = (left + right) >> 1;
    res += count(a, left, mid, t);
    res += count(a, mid + 1, right, t);
    auto arr = new long[mid - left + 1];
    long sum = 0;
    for (int i = mid; i >= left; -- i)
    {
        sum += a[i];
        arr[mid - i] = sum;
    }
    arr.sort;
    sum = 0;
    for (int i = mid + 1; i <= right; ++ i)
    {
        sum += a[i];
        auto ret = search(arr, mid - left + 1, t - sum);
        res += ret;
    }
    return res;
}

void solve(int[] a, int n, long t)
{
    auto ans = count(a, 0, n - 1, t);
    writeln(ans);
}

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

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

const long INF = 1L << 59;

void main() {
    auto s = readln.split;
    auto N = s[0].to!int;
    auto T = s[1].to!long;
    auto A = readln.split.map!(to!long).array;
    auto B = new long[](N+1);
    foreach (i; 0..N) B[i+1] = B[i] + A[i];

    int[long] comp;
    auto C = B.dup.sort().uniq().array;
    auto M = C.length.to!int;
    foreach (i; 0..M) comp[C[i]] = i;

    auto st = new SegmentTree(M);
    foreach (i; 0..N+1) st.add(comp[B[i]], 1);
    long ans = 0;

    foreach (i; 0..N) {
        st.add(comp[B[i]], -1);
        auto lb = C.assumeSorted.lowerBound(T + B[i]).length.to!int;
        ans += st.sum(0, lb - 1);
    }

    ans.writeln;
}

class SegmentTree {
    int[] table;
    int size;

    this(int n) {
        assert(bsr(n) < 29);
        size = 1 << (bsr(n) + 2);
        table = new int[](size);
    }

    void add(int pos, int num) {
        return add(pos, num, 0, 0, size/2-1);
    }

    void add(int pos, int num, int i, int left, int right) {
        table[i] += num;
        if (left == right)
            return;
        auto mid = (left + right) / 2;
        if (pos <= mid)
            add(pos, num, i*2+1, left, mid);
        else
            add(pos, num, i*2+2, mid+1, right);
    }

    int sum(int pl, int pr) {
        if (pr < pl) return 0;
        return sum(pl, pr, 0, 0, size/2-1);
    }

    int sum(int pl, int pr, int i, int left, int right) {
        if (pl > right || pr < left)
            return 0;
        else if (pl <= left && right <= pr)
            return table[i];
        else
            return
                sum(pl, pr, i*2+1, left, (left+right)/2) +
                sum(pl, pr, i*2+2, (left+right)/2+1, right);
    }
}


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

void main ()
{
	int n;
	int m;
	while (readf !(" %s %s") (n, m) > 0)
	{
		readln;
		auto x = readln.split;
		auto y = readln.split;
		auto q = readln.strip.to !(int);
		foreach (r; 0..q)
		{
			auto z = readln.strip.to !(int);
			z -= 1;
			writeln (x[z % n], y[z % m]);
		}
	}
}

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; }
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; }
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 main()
{
	auto n = RD!int;
	auto m = RD!int;
	string[] s, t;
	foreach (i; 0..n)
		s ~= RD!string;
	foreach (i; 0..m)
		t ~= RD!string;
	auto q = RD!int;
	auto ans = new string[](q);
	foreach (qi; 0..q)
	{
		auto y = RD!int-1;
		ans[qi] = s[y%n] ~ t[y%m];
	}

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

import std.stdio;
import std.string : strip, split;

void main()
{
  int n, m, q;
  readf!"%d %d\n"(n, m);

  auto s = readln.strip.split;
  auto t = readln.strip.split;
  readf!"%d\n"(q);

  foreach (i; 0..q) {
    int y;
    readf!"%d\n"(y);
    y--;

    writeln(s[y % s.length] ~ t[y % t.length]);
  }
}

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;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

void solve(){
	int n = rint, m = rint;
	string[] as = rtypes!string(n);
	string[] bs = rtypes!string(m);

	foreach(_; 0 .. rint){
		long x = rlong - 1;
		(as[(x % n).to!int] ~ bs[(x % m).to!int]).writeln;
	}
}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const M = readInt();
      auto S = new string[N];
      foreach (i; 0 .. N) {
        S[i] = readToken();
      }
      auto T = new string[M];
      foreach (j; 0 .. M) {
        T[j] = readToken();
      }
      const Q = readInt();
      auto Y = new int[Q];
      foreach (q; 0 .. Q) {
        Y[q] = readInt();
      }

      foreach (q; 0 .. Q) {
        writeln(S[(Y[q] - 1) % N], T[(Y[q] - 1) % M]);
      }
    }
  } 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 read_string() {
                while (tokens.empty) {
                tokens = readln.split;
        }
        auto token = tokens.front;
                tokens.popFront;
                return token;
        }

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

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

        string[] tokens;
}

void main() {
        IO cin;
        int t = 1;
        // int t = cin.read_int;
        while (t--) {
                int n = cin.read_int;
                string s = cin.read_string;
                string code = cin.read_string;
                int ans = 0;
                for (int i = 0; i < n; i++) {
                        int met1 = min(s[i], code[i]) + 10 - max(s[i], code[i]);
                        int met2 = abs(code[i] - s[i]);
                        ans += min(met1, met2);
                }
                writeln(ans);
        }
}

import std.stdio, std.range, std.conv, std.algorithm, std.array, std.string, std.ascii, std.math;

void main() {

	readln;
	auto a = readln.strip, b = readln.strip;

	uint sum;
	foreach (i, e; a) {
		auto tmpA = e.to!int, tmpB = b[i].to!int;
		auto minA = min(tmpA, tmpB), maxB = max(tmpA, tmpB);
		auto cl = maxB - minA, tl = 10 - maxB + minA;
		if (cl < tl) {
			sum += cl;
		}
		else {
			sum += tl;
		}
	}

	writeln(sum);
}

import std.stdio;
import core.stdc.stdio;
import std.algorithm;
import std.math;

int main(string[] argv)
{
	int n; scanf("%d", &n);
	char[1001] ss;
	char[1001] fs;
	scanf("%s", &ss);
	scanf("%s", &fs);
	int count = 0;
	foreach (int i; 0..n)
	{
		int d1 = ss[i] - fs[i]; d1 = abs(d1);
		int d2 = 10 - d1; d2 = abs(d2);
		count += (d1 < d2 ? d1 : d2);
	}
	write(count);
	return 0;
}


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 T = readln.chomp.to!int;
    while (T--) {
        auto s = readln.split.map!(to!long);
        auto a = s[0];
        auto b = s[1];
        auto c = s[2];
        auto r = s[3];
        if (a > b) swap(a, b);
        auto u = c - r;
        auto v = c + r;
        if (u >= b || v <= a) {
            writeln(b - a);
        } else {
            u = max(u, a);
            v = min(v, b);
            writeln(b - a - (v - u));
        }
    }
}

//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;

class InputReader {
  private:
  ubyte[] p;
  ubyte[] buffer;
  size_t cur;
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
    p = stdin.rawRead (buffer);
  }
  final ubyte skipByte (ubyte lo) {
    while (true) {
      auto a = p[cur .. $];
      auto r = a.find! (c => c >= lo);
      if (!r.empty) {
        cur += a.length - r.length;
        return p[cur++];
      }
      p = stdin.rawRead (buffer);
      cur = 0;
      if (p.empty) return 0;
    }
  }
  final ubyte nextByte () {
    if (cur < p.length) {
       return p[cur++];
    }
    p = stdin.rawRead (buffer);
    if (p.empty) return 0;
    cur = 1;
    return p[0];
  }

  template next(T) if (isSigned!T) {
    final T next ()  {
      T res;
      ubyte b = skipByte (45);
      if (b == 45) {
        while (true) {
          b = nextByte ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    final T next () {
      T res = skipByte (48) - 48;
      while (true) {
        ubyte b = nextByte ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  final T[] nextA(T) (int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

void main() {
  auto r = new InputReader;
  immutable nt = r.next!uint;
  auto res = uninitializedArray!(int[]) (nt);
  foreach (tid; 0 .. nt) {
    auto a = r.next!int;
    auto b = r.next!int;
    if (a > b) swap (a, b);
    immutable c = r.next!int;
    immutable rd = r.next!int;
    immutable u = c - rd, v = c + rd;
    auto x = max (a, u), y = min (b, v);
    auto s = x < y ? (y - x) : 0;
    res[tid] = (b - a) - s;
  }
  writefln ("%(%s\n%)", 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;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

void solve(){
	foreach(_; 0 .. rint){
		long a = rlong, b = rlong, c = rlong, r = rlong;
		long x = c - r, y = c + r;
		if(a > b){ long z = a; a = b, b = z; }
		long ans;
		if(y <= a || b <= x) ans += b - a;
		else{
			if(a < x) ans += x - a;
			if(y < b) ans += b - y;
		}
		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; }
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; }

void main()
{
	auto t = RD!int;
	auto ans = new int[](t);
	foreach (ti; 0..t)
	{
		auto a = RD!int;
		auto b = RD!int;
		auto c = RD!int;
		auto r = RD!int;
		if (a > b)
			swap(a, b);
		auto x = c - r;
		auto y = c + r;
		auto len = b - a;
		if (x < a)
			ans[ti] = max(len - max(0, y - a), 0);
		else if (y >= b)
			ans[ti] = max(len - max(0, b - x), 0);
		else
			ans[ti] = max(len - (y - x), 0);
	}

	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 ()
{
	int tests;
	readf !(" %s") (tests);
	foreach (test; 0..tests)
	{
		long n;
		readf !(" %s") (n);
		n -= 1;
		long triple = n / 3;
		int rem = n % 3;
		int bits = 0;
		while (triple >= 1L << bits)
		{
			triple -= 1L << bits;
			bits += 2;
		}
		auto a = [0L, 0L, 0L];
		a[0] += (1L << bits);
		a[1] += (2L << bits);
		a[2] += (3L << bits);
		for (int bit = 0; bit < bits; bit += 2)
		{
			int cur = triple & 3;
			a[0] += [0L, 1L, 2L, 3L][cur] << bit;
			a[1] += [0L, 2L, 3L, 1L][cur] << bit;
			a[2] += [0L, 3L, 1L, 2L][cur] << bit;
			triple >>= 2;
		}
		writeln (a[rem]);
	}
}

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() {
  debug {
    bool[long] app;
    foreach (i; 0 .. 100) {
      for (int a = 0; ; ++a) {
        if (a !in app) {
          for (int b = 0; ; ++b) {
            const c = a ^ b;
            if ((b !in app) && (c !in app)) {
              writefln("%3d %3d %3d %10b %10b %10b", a, b, c, a, b, c);
              app[a] = app[b] = app[c] = true;
              goto found;
            }
          }
        }
      }
     found:
    }
  }

  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readLong() - 1;
        long n = N / 3;
        for (int k = 0; ; ++k) {
          // a \in [2^(2k), 2^(2k+1))
          if (n < 1L << (2 * k)) {
            debug {
              writefln("k = %s, n = %s", k, n);
            }
            long a = 1L << (2 * k);
            a += n;
            long b = 1L << (2 * k + 1);
            foreach (i; 0 .. k) {
              b |= [0L, 2L, 3L, 1L][cast(int)((a >> (2 * i)) & 3)] << (2 * i);
            }
            const c = a ^ b;
            debug {
              writefln("%064b", a);
              writefln("%064b", b);
              writefln("%064b", c);
            }
            writeln([a, b, c][cast(int)(N % 3)]);
            break;
          } else {
            n -= 1L << (2 * k);
          }
        }
      }
    }
  } catch (EOFException e) {
  }
}

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() {
  debug {
    bool[long] app;
    foreach (i; 0 .. 100) {
      for (int a = 0; ; ++a) {
        if (a !in app) {
          for (int b = 0; ; ++b) {
            const c = a ^ b;
            if ((b !in app) && (c !in app)) {
              writefln("%3d %3d %3d %10b %10b %10b", a, b, c, a, b, c);
              app[a] = app[b] = app[c] = true;
              goto found;
            }
          }
        }
      }
     found:
    }
  }

  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readLong() - 1;
        long n = N / 3;
        for (int k = 0; ; ++k) {
          // a \in [2^(2k), 2^(2k+1))
          if (n < 1L << (2 * k)) {
            long a = 1L << (2 * k);
            a += n;
            long b = 1L << (2 * k + 1);
            foreach (i; 0 .. k) {
              b |= [0, 2, 3, 1][cast(int)((a >> (2 * i)) & 3)] << (2 * i);
            }
            const c = a ^ b;
            writeln([a, b, c][cast(int)(N % 3)]);
            break;
          } else {
            n -= 1L << (2 * k);
          }
        }
      }
    }
  } catch (EOFException e) {
  }
}


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

void main ()
{
	int h, w;
	while (readf (" %s %s", &h, &w) > 0)
	{
		readln;
		string [] a;
		a ~= '#'.repeat (w + 2).array;
		foreach (row; 1..h + 1)
		{
			a ~= '#' ~ readln.strip ~ '#';
		}
		a ~= '#'.repeat (w + 2).array;

		auto frow = new int [] [] (h + 2, w + 2);
		foreach (row; 0..h + 2)
		{
			frow[row][0] = 0;
			foreach (col; 1..w + 2)
			{
				frow[row][col] = frow[row][col - 1] +
				    (a[row][col - 1] == '.' &&
				    a[row][col] == '.');
			}
		}

		auto fcol = new int [] [] (w + 2, h + 2);
		foreach (col; 0..w + 2)
		{
			fcol[col][0] = 0;
			foreach (row; 1..h + 2)
			{
				fcol[col][row] = fcol[col][row - 1] +
				    (a[row - 1][col] == '.' &&
				    a[row][col] == '.');
			}
		}

		int q;
		readf (" %s", &q);
		foreach (z; 0..q)
		{
			int row1, col1, row2, col2;
			readf (" %s %s %s %s", &row1, &col1, &row2, &col2);
			int res = 0;
			foreach (row; row1..row2 + 1)
			{
				res += frow[row][col2] - frow[row][col1];
			}
			foreach (col; col1..col2 + 1)
			{
				res += fcol[col][row2] - fcol[col][row1];
			}
			writeln (res);
		}
	}
}

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

void main() {
    auto s = readln.split.map!(to!int);
    auto H = s[0];
    auto W = s[1];

    auto B = new string[](H);
    foreach (i; 0..H) B[i] = readln.chomp;

    auto yoko = new int[][](H+1, W+1);
    auto tate = new int[][](H+1, W+1);
    foreach (i; 0..H) {
        foreach (j; 0..W) {
            if (j < W-1 && B[i][j] == '.' && B[i][j+1] == '.')
                yoko[i+1][j+1] += 1;
            if (i < H-1 && B[i][j] == '.' && B[i+1][j] == '.')
                tate[i+1][j+1] += 1;
        }
    }

    foreach (i; 0..H) foreach (j; 1..W) {
            yoko[i+1][j+1] += yoko[i+1][j];
            tate[i+1][j+1] += tate[i+1][j];
        }
    foreach (j; 0..W) foreach (i; 1..H) {
            yoko[i+1][j+1] += yoko[i][j+1];
            tate[i+1][j+1] += tate[i][j+1];
        }

    int acm(int a, int b, int c, int d) {
        return
            yoko[c][d-1] - yoko[a-1][d-1] - yoko[c][b-1] + yoko[a-1][b-1] +
            tate[c-1][d] - tate[a-1][d] - tate[c-1][b-1] + tate[a-1][b-1];
    }

    auto M = readln.chomp.to!int;
    foreach (_; 0..M) {
        int ans = 0;
        s = readln.split.map!(to!int);
        writeln(acm(s[0], s[1], s[2], s[3]));
    }
}


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, k;
    readf("%s %s", &n, &k);
    readln;

    auto g = new int[][] (n+1);
    foreach (i; 0 .. n-1) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        g[u] ~= v;
        g[v] ~= u;
    }

    auto depth = new int[] (n+1);
    auto sz = new int[] (n+1);
    auto parent = new int[] (n+1);
    auto isLeaf = new bool[] (n+1);
    auto gain = new int[] (n+1);

    void dfs(int v, int fr) {
        depth[v] = v == 1 ? 0 : depth[fr] + 1;
        sz[v] = 1;
        parent[v] = fr;
        isLeaf[v] = true;

        foreach (u; g[v]) {
            if (u == fr) { continue; }

            dfs(u, v);

            sz[v] += sz[u];
            isLeaf[v] = false;
        }

        gain[v] = depth[v] - (sz[v] - 1);
    }

    dfs(1, 0);

    auto leavesWithGain = (n+1).iota.filter!(i => isLeaf[i]).map!(i => tuple(gain[i], i));
    auto rbt = redBlackTree(leavesWithGain);

    auto ans = 0L;
    while (k--) {
        ans += rbt.back[0];
        auto v = rbt.back[1];
        rbt.removeBack();

        rbt.insert(tuple(gain[parent[v]], parent[v]));
    }

    ans.writeln;
}

//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.container.rbtree;
import std.typecons;

final class InputReader {
  private:
  ubyte[] p, buffer;
  bool eof;
  bool rawRead () {
    if (eof) {
      return false;
    }
    p = stdin.rawRead (buffer);
    if (p.empty) {
      eof = true;
      return false;
    }
    return true;
  }
  ubyte nextByte(bool check) () {
    static if (check) {
      if (p.empty) {
        if (!rawRead ()) {
          return 0;
        }
      }
    }
    auto r = p.front;
    p.popFront ();
    return r;
  }
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
  }
  bool seekByte (in ubyte lo) {
    while (true) {
      p = p.find! (c => c >= lo);
      if (!p.empty) {
        return false;
      }
      if (!rawRead ()) {
        return true;
      }
    }
  }
  template next(T) if (isSigned!T) {
    T next ()  {
      if (seekByte (45)) {
        return 0;
      }
      T res;
      ubyte b = nextByte!false ();
      if (b == 45) {
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    T next () {
      if (seekByte (48)) {
        return 0;
      }
      T res = nextByte!false () - 48;
      while (true) {
        ubyte b = nextByte!true ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  T[] nextA(T) (in int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

class G {
  int[][] e;
  int[] d, parent, ss;
  void go (int i) {
    ss[i] = 1;
    foreach (j; e[i]) {
      if (parent[j] == -2) {
        parent[j] = i;
        d[j] = d[i] + 1;
        go (j);
        ss[i] += ss[j];
      }
    }
  }
  this (int n) {
    e = new int[][n];
    d = new int[n];
    parent = new int[n];
    ss = new int[n];
  }
  void addEdge (int i, int j) {
    e[i] ~= j;
    e[j] ~= i;
  }
  void dfs () {
    parent[] = -2;
    parent[0] = -1;
    go (0);
  }
}

alias T = Tuple!(int, int);

alias RBT = RedBlackTree!(T, "a < b", false);

void main() {
  auto r = new InputReader ();
  const n = r.next!uint;
  const k = r.next!uint;
  auto g = new G (n);
  foreach (eid; 1 .. n) {
    const i = r.next!uint - 1;
    const j = r.next!uint - 1;
    g.addEdge (i, j);
  }
  g.dfs ();
  auto x = new int[n];
  foreach (i; 0 .. n) {
    x[i] = (g.ss[i] - 1) - g.d[i];
  }
  sort (x);
  writeln (sum(x[k .. $], 0L));
}

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 n = RD!int;
	auto k = RD!int;
	auto edges = new int[][](n);
	foreach (i; 0..n-1)
	{
		auto u = RD!int-1;
		auto v = RD!int-1;
		edges[u] ~= v;
		edges[v] ~= u;
	}

	auto cnt = new long[](n);

	long dfs(int pos, int par, int depth)
	{
		cnt[pos] += depth;
		long res = 1;
		foreach (to; edges[pos])
		{
			if (to == par) continue;
			res += dfs(to, pos, depth+1);
		}
		cnt[pos] -= res;
		return res;
	}
	dfs(0, -1, 0);

	auto resort = new bool[](n);
	auto index = cnt.MAKE_IDX;
	foreach (i; index[0..n-k])
	{
		resort[i] = true;
	}

	long ans;
	void dfs2(int pos, int par, int depth)
	{
		if (resort[pos])
		{
			foreach (to; edges[pos])
			{
				if (to == par) continue;
				dfs2(to, pos, depth+1);
			}
		}
		else
		{
			ans += depth;
			foreach (to; edges[pos])
			{
				if (to == par) continue;
				dfs2(to, pos, depth);
			}
		}
	}
	dfs2(0, -1, 0);

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

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, k;
    readf("%s %s", &n, &k);
    readln;

    auto g = new int[][] (n+1);
    foreach (i; 0 .. n-1) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        g[u] ~= v;
        g[v] ~= u;
    }

    auto depth = new int[] (n+1);
    auto sz = new int[] (n+1);
    auto parent = new int[] (n+1);
    auto isLeaf = new bool[] (n+1);
    auto gain = new int[] (n+1);

    void dfs(int v, int fr) {
        depth[v] = v == 1 ? 0 : depth[fr] + 1;
        sz[v] = 1;
        parent[v] = fr;
        isLeaf[v] = true;

        foreach (u; g[v]) {
            if (u == fr) { continue; }

            dfs(u, v);

            sz[v] += sz[u];
            isLeaf[v] = false;
        }

        gain[v] = depth[v] - (sz[v] - 1);
    }

    dfs(1, 0);

    auto leavesWithGain = (n+1).iota.filter!(i => isLeaf[i]).map!(i => tuple(gain[i], i));
    auto rbt = redBlackTree(leavesWithGain);
    foreach (i; 1 .. n+1) {
        if (isLeaf[i]) { rbt.insert(tuple(gain[i], i)); }
    }

    auto ans = 0L;
    while (k--) {
        ans += rbt.back[0];
        auto v = rbt.back[1];
        rbt.removeBack();

        rbt.insert(tuple(gain[parent[v]], parent[v]));
    }

    ans.writeln;
}

/// https://codeforces.com/contest/1336/problem/A
import std.stdio;
import std.array;
import std.algorithm;
import std.container;
import std.range : sequence, take;

void main()
{
  int n, k;
  stdin.readf!"%d %d\n"(n, k);

  int[][] adj;  adj.length = n+1;

  int u, v;
  for (int i=1; i<n; i++) {
    stdin.readf!"%d %d\n"(u, v);
    adj[u] ~= v;
    adj[v] ~= u;
  }

  int[] depth; depth.length = n+1;
  int[] childCount; childCount.length = n+1;
  int dfs(int node, int parent=0, int _depth=0) {
    int children = 0;
    foreach (neighbor; adj[node]) {
      if (neighbor != parent)
        children += dfs(neighbor, node, _depth+1);
    }
    depth[node] = _depth;
    childCount[node] = children;
    return children+1;
  }
  dfs(1);

  int prioriry(int node) { return depth[node] - childCount[node]; }
  bool compare(int a, int b) { return prioriry(a) < prioriry(b); }
  auto nodes = sequence!"n+1"().take(n).array;
  auto minHeap = heapify!compare(nodes);

  long happiness;
  foreach (city; minHeap.take(k))
    happiness += prioriry(city);

  writeln(happiness);
}

import std.stdio      : stdin, writeln;
import std.algorithm  : fold, sum;
import std.container  : heapify;
import std.range      : sequence, take;

void main()
{
  int n, k;
  stdin.readf!"%d %d\n"(n, k);

  int[][] adj;
  long[] prioriry;
  adj.length = prioriry.length = n;

  int u, v;
  for (int i=1; i<n; i++) {
    stdin.readf!"%d %d\n"(u, v);
    u--; v--;
    adj[u] ~= v;
    adj[v] ~= u;
  }

  int dfs(int node, int parent=0, int depth=0) {
    auto children = adj[node].fold!((ac,n) => ac + (n != parent? dfs(n,node,depth+1): 0))(0);
    prioriry[node] = depth - children;
    return children + 1;
  }
  dfs(0);

  writeln(heapify(prioriry).take(k).sum);
}

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

void main ()
{
	int n, k;
	while (readf !(" %s %s") (n, k) > 0)
	{
		auto a = new int [] [n];
		foreach (i; 0..n - 1)
		{
			int u, v;
			readf !(" %s %s") (u, v);
			u -= 1;
			v -= 1;
			a[u] ~= v;
			a[v] ~= u;
		}

		auto f = new int [n];

		int recur (int v, int p, int depth)
		{
			int total = 0;
			foreach (u; a[v])
			{
				if (u != p)
				{
					total += recur (u, v, depth + 1);
				}
			}
			f[v] = depth - total;
			return total + 1;
		}

		recur (0, -1, 0);
		sort !(q{a > b}) (f);
		writeln (sum (f[0..k], 0L));
	}
}

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 N, K;
int[] A, B;

int[][] G;
int[] par, dep, sz;

void dfs(int u, int p) {
  par[u] = p;
  dep[u] = (p == -1) ? 0 : (dep[p] + 1);
  sz[u] = 1;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
      sz[u] += sz[v];
    }
  }
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      K = readInt();
      A = new int[N - 1];
      B = new int[N - 1];
      foreach (i; 0 .. N - 1) {
        A[i] = readInt() - 1;
        B[i] = readInt() - 1;
      }

      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }
      par = new int[N];
      dep = new int[N];
      sz = new int[N];
      dfs(0, -1);

      auto degs = new int[N];
      foreach (u; 1 .. N) {
        ++degs[par[u]];
      }
      alias Entry = Tuple!(int, "score", int, "u");
      BinaryHeap!(Array!Entry) que;
      foreach (u; 1 .. N) {
        if (degs[u] == 0) {
          que.insert(Entry(dep[u], u));
        }
      }

      long ans;
      foreach (k; 0 .. K) {
        const e = que.front;
        que.removeFront;
        debug {
          writeln("e = ", e);
        }
        ans += e.score;
        const p = par[e.u];
        if (p != 0 && --degs[p] == 0) {
          que.insert(Entry(dep[p] - (sz[p] - 1), p));
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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, k;
  @Dim("n - 1") long[2][] edges;

  void solve(long tc = -1)
  {
    auto alist = makeSlice!(long[])(n);
    auto height = makeSlice!(long)(n);
    auto size = makeSlice!(long)(n);
    foreach(ref edge; edges)
      {
        edge[] -= 1;
        alist.at(edge[0]) ~= edge[1];
        alist.at(edge[1]) ~= edge[0];
      }
    void dfs(long node, long parent, long h)
    {
      height.at(node) = h;
      size.at(node) = 1;
      foreach(nei; alist.at(node))
        if (nei != parent)
          {
            dfs(nei, node, h + 1);
            size.at(node) += size.at(nei);
          }
    }
    dfs(0, -1, 0);
    long[] priol;
    foreach(node; 0 .. n)
      priol ~= height.at(node) - size.at(node) + 1;
    sort(priol);
    writeln(priol[$ - cast(size_t) k .. $].sum);
  }
}

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

import std.stdio;
import std.algorithm;
import std.container : DList;

void main()
{
  int n, k;
  stdin.readf!"%d %d\n"(n, k);

  int[][] adj;  adj.length = n+1;
  bool[] visited; visited.length = n+1;
  bool[] tourism; tourism.length = n+1;

  int u, v;
  for (int i=1; i<n; i++) {
    stdin.readf!"%d %d\n"(u, v);
    adj[u] ~= v;
    adj[v] ~= u;
  }

  bool desc(int a, int b) { return adj[a].length > adj[b].length; }
  foreach (a; adj)
    a.sort!desc;

  k = n - k;

  auto q = DList!int(1);
  while (!q.empty() && k > 0) {
    auto city = q.back(); q.removeBack();
    tourism[city] = true;
    k = k - 1;
    if (k == 0) break;

    foreach (neighbor; adj[city])
      if (!visited[neighbor]) {
        visited[neighbor] = true;
        q.insertFront(neighbor);
      }
  }

  int happiness;
  int dfs(int i, int parent) {
    int industrial;

    foreach (child; adj[i])
      if (child == parent)
        continue;
      else
        industrial += dfs(child, i);

    if (tourism[i])
      happiness += industrial;
    else
      industrial += 1;

    return industrial;
  }

  dfs(1, 0);
  writeln(happiness);
}

//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;

final class Heap(T) {
  private:
    T [] a;
    int [] h;
    int [] g;
    int n;
    int size;
  pure nothrow @nogc
  void heapifyFront (int k) {
    immutable he = h[k];
    int i = k;
    int j = i << 1;
    while (j <= size) {
      if (j < size && a[h[j+1]] < a[h[j]]) {
        j++;
      }
      if (a[h[j]] >= a[he]) {
        break;
      }
      h[i] = h[j];
      g[h[i]] = i;
      i = j;
      j = i << 1;
    }
    if (i != k) {
      h[i] = he;
      g[he] = i;
    }
  }
  pure nothrow @nogc
  void heapifyBack (int k) {
    immutable he = h[k];
    int i = k;
    while (i > 1) {
      int j = i >> 1;
      if (a[he] >= a[h[j]]) {
        break;
      }
      h[i] = h[j];
      g[h[i]] = i;
      i = j;
    }
    if (i != k) {
      h[i] = he;
      g[he] = i;
    }
  }
  pure nothrow @nogc
  void insert (int i) {
    h[++size] = i;
    g[i] = size;
    heapifyBack (size);
  }
  public:
  pure nothrow @nogc
  void decreaseKey (int k, T value) in {
    assert (k >= 0 && k < n);
    assert (value < a[k]);
  } body {
    a[k] = value;
    immutable pos = g[k];
    if (!pos) {
      insert (k);
    } else {
      heapifyBack (pos);
    }
  }
  pure nothrow @nogc
  void update (int k, T value) in {
    assert (k >= 0 && k < n);
  } body {
    immutable pos = g[k];
    if (!pos) {
      a[k] = value;
      insert (k);
    } else if (value < a[k]) {
      a[k] = value;
      heapifyBack (pos);
    } else if (value > a[k]) {
      a[k] = value;
      heapifyFront (pos);
    }
  }
  pure nothrow @nogc
  int extractMin () {
    assert (size > 0);
    immutable he = h[1];
    g[he] = 0;
    if (--size) {
      h[1] = h[size+1];
      g[h[1]] = 1;
      heapifyFront (1);
    }
    return he;
  }
  pure nothrow @nogc
  inout(T) opIndex (size_t index) inout {
    return a[index];
  }

  @property pure nothrow @nogc
  bool empty () const { return size == 0; }

  this (int n_, T default_value = T.init) {
    n = n_;
    size = 0;
    a = uninitializedArray! (T[]) (n);
    a[] = default_value;
    h = new int[n+1];
    g = new int[n];
  }
}

final class InputReader {
  private:
  ubyte[] p, buffer;
  bool eof;
  bool rawRead () {
    if (eof) {
      return false;
    }
    p = stdin.rawRead (buffer);
    if (p.empty) {
      eof = true;
      return false;
    }
    return true;
  }
  ubyte nextByte(bool check) () {
    static if (check) {
      if (p.empty) {
        if (!rawRead ()) {
          return 0;
        }
      }
    }
    auto r = p.front;
    p.popFront ();
    return r;
  }
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
  }
  bool seekByte (in ubyte lo) {
    while (true) {
      p = p.find! (c => c >= lo);
      if (!p.empty) {
        return false;
      }
      if (!rawRead ()) {
        return true;
      }
    }
  }
  template next(T) if (isSigned!T) {
    T next ()  {
      if (seekByte (45)) {
        return 0;
      }
      T res;
      ubyte b = nextByte!false ();
      if (b == 45) {
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    T next () {
      if (seekByte (48)) {
        return 0;
      }
      T res = nextByte!false () - 48;
      while (true) {
        ubyte b = nextByte!true ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  T[] nextA(T) (in int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

class G {
  int[][] e;
  int[] d, parent;
  void go (int i) {
    foreach (j; e[i]) {
      if (parent[j] == -2) {
        parent[j] = i;
        d[j] = d[i] + 1;
        go (j);
      }
    }
  }
  this (int n) {
    e = new int[][n];
    d = new int[n];
    parent = new int[n];
  }
  void addEdge (int i, int j) {
    e[i] ~= j;
    e[j] ~= i;
  }
  void dfs () {
    parent[] = -2;
    parent[0] = -1;
    go (0);
  }
}

void main() {
  auto r = new InputReader ();
  const n = r.next!uint;
  const k = r.next!uint;
  auto g = new G (n);
  foreach (eid; 1 .. n) {
    const i = r.next!uint - 1;
    const j = r.next!uint - 1;
    g.addEdge (i, j);
  }
  g.dfs ();
  auto h = new Heap!int (n, int.max / 2);
  long res;
  foreach (i; 0 .. n) {
    h.decreaseKey (i, -g.d[i]);
  }
  foreach (i; 0 .. k) {
    int j = h.extractMin ();
    res -= h[j];
    int o = g.parent[j];
    if (o >= 0) {
      int w = h[o];
      h.update (o, w + 1);
    }
  }
  writeln (res);
}

//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.container.rbtree;
import std.typecons;

final class InputReader {
  private:
  ubyte[] p, buffer;
  bool eof;
  bool rawRead () {
    if (eof) {
      return false;
    }
    p = stdin.rawRead (buffer);
    if (p.empty) {
      eof = true;
      return false;
    }
    return true;
  }
  ubyte nextByte(bool check) () {
    static if (check) {
      if (p.empty) {
        if (!rawRead ()) {
          return 0;
        }
      }
    }
    auto r = p.front;
    p.popFront ();
    return r;
  }
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
  }
  bool seekByte (in ubyte lo) {
    while (true) {
      p = p.find! (c => c >= lo);
      if (!p.empty) {
        return false;
      }
      if (!rawRead ()) {
        return true;
      }
    }
  }
  template next(T) if (isSigned!T) {
    T next ()  {
      if (seekByte (45)) {
        return 0;
      }
      T res;
      ubyte b = nextByte!false ();
      if (b == 45) {
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    T next () {
      if (seekByte (48)) {
        return 0;
      }
      T res = nextByte!false () - 48;
      while (true) {
        ubyte b = nextByte!true ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  T[] nextA(T) (in int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

class G {
  int[][] e;
  int[] d, parent;
  void go (int i) {
    foreach (j; e[i]) {
      if (parent[j] == -2) {
        parent[j] = i;
        d[j] = d[i] + 1;
        go (j);
      }
    }
  }
  this (int n) {
    e = new int[][n];
    d = new int[n];
    parent = new int[n];
  }
  void addEdge (int i, int j) {
    e[i] ~= j;
    e[j] ~= i;
  }
  void dfs () {
    parent[] = -2;
    parent[0] = -1;
    go (0);
  }
}

alias T = Tuple!(int, int);

alias RBT = RedBlackTree!(T, "a < b", false);

void main() {
  auto r = new InputReader ();
  const n = r.next!uint;
  const k = r.next!uint;
  auto g = new G (n);
  foreach (eid; 1 .. n) {
    const i = r.next!uint - 1;
    const j = r.next!uint - 1;
    g.addEdge (i, j);
  }
  g.dfs ();
  auto t = new RBT();
  foreach (i; 1 .. n) if (g.e[i].length == 1) {
    t.insert (T (g.d[i], i));
  }
  long res;
  auto delta = new int[n];
  foreach (qid; 0 .. k) {
    auto q = t.back;
    debug stderr.writeln (q);
    res += q[0];
    const int j = q[1];
    int o = g.parent[j];
    if (o >= 0) {
      delta[o] = delta[j] + 1;
      t.insert (T (g.d[o] - delta[o], o));
    }
    t.removeBack ();
  }
  writeln (res);
}

//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.container.rbtree;
import std.typecons;

final class InputReader {
  private:
  ubyte[] p, buffer;
  bool eof;
  bool rawRead () {
    if (eof) {
      return false;
    }
    p = stdin.rawRead (buffer);
    if (p.empty) {
      eof = true;
      return false;
    }
    return true;
  }
  ubyte nextByte(bool check) () {
    static if (check) {
      if (p.empty) {
        if (!rawRead ()) {
          return 0;
        }
      }
    }
    auto r = p.front;
    p.popFront ();
    return r;
  }
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
  }
  bool seekByte (in ubyte lo) {
    while (true) {
      p = p.find! (c => c >= lo);
      if (!p.empty) {
        return false;
      }
      if (!rawRead ()) {
        return true;
      }
    }
  }
  template next(T) if (isSigned!T) {
    T next ()  {
      if (seekByte (45)) {
        return 0;
      }
      T res;
      ubyte b = nextByte!false ();
      if (b == 45) {
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    T next () {
      if (seekByte (48)) {
        return 0;
      }
      T res = nextByte!false () - 48;
      while (true) {
        ubyte b = nextByte!true ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  T[] nextA(T) (in int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

class G {
  int[][] e;
  int[] d, parent;
  void go (int i) {
    foreach (j; e[i]) {
      if (parent[j] == -2) {
        parent[j] = i;
        d[j] = d[i] + 1;
        go (j);
      }
    }
  }
  this (int n) {
    e = new int[][n];
    d = new int[n];
    parent = new int[n];
  }
  void addEdge (int i, int j) {
    e[i] ~= j;
    e[j] ~= i;
  }
  void dfs () {
    parent[] = -2;
    parent[0] = -1;
    go (0);
  }
}

alias T = Tuple!(int, int);

alias RBT = RedBlackTree!(T, "a < b", false);

void main() {
  auto r = new InputReader ();
  const n = r.next!uint;
  const k = r.next!uint;
  auto g = new G (n);
  foreach (eid; 1 .. n) {
    const i = r.next!uint - 1;
    const j = r.next!uint - 1;
    g.addEdge (i, j);
  }
  g.dfs ();
  auto t = new RBT();
  auto intree = new bool[n];
  foreach (i; 1 .. n) if (g.e[i].length == 1) {
    t.insert (T (g.d[i], i));
    intree[i] = true;
  }
  long res;
  auto delta = new int[n];
  foreach (qid; 0 .. k) {
    auto q = t.back;
    debug stderr.writeln (q);
    res += q[0];
    const int j = q[1];
    int o = g.parent[j];
    t.removeBack ();
    if (o > 0) {
      if (intree[o]) {
        t.removeKey (T (g.d[o] - delta[o], o));
      } else intree[o] = true;
      delta[o] += delta[j] + 1;
      t.insert (T (g.d[o] - delta[o], o));
    }
  }
   writeln (res);
}

//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.container.rbtree;
import std.typecons;

final class InputReader {
  private:
  ubyte[] p, buffer;
  bool eof;
  bool rawRead () {
    if (eof) {
      return false;
    }
    p = stdin.rawRead (buffer);
    if (p.empty) {
      eof = true;
      return false;
    }
    return true;
  }
  ubyte nextByte(bool check) () {
    static if (check) {
      if (p.empty) {
        if (!rawRead ()) {
          return 0;
        }
      }
    }
    auto r = p.front;
    p.popFront ();
    return r;
  }
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
  }
  bool seekByte (in ubyte lo) {
    while (true) {
      p = p.find! (c => c >= lo);
      if (!p.empty) {
        return false;
      }
      if (!rawRead ()) {
        return true;
      }
    }
  }
  template next(T) if (isSigned!T) {
    T next ()  {
      if (seekByte (45)) {
        return 0;
      }
      T res;
      ubyte b = nextByte!false ();
      if (b == 45) {
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    T next () {
      if (seekByte (48)) {
        return 0;
      }
      T res = nextByte!false () - 48;
      while (true) {
        ubyte b = nextByte!true ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  T[] nextA(T) (in int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

class G {
  int[][] e;
  int[] d, parent;
  void go (int i) {
    foreach (j; e[i]) {
      if (parent[j] == -2) {
        parent[j] = i;
        d[j] = d[i] + 1;
        go (j);
      }
    }
  }
  this (int n) {
    e = new int[][n];
    d = new int[n];
    parent = new int[n];
  }
  void addEdge (int i, int j) {
    e[i] ~= j;
    e[j] ~= i;
  }
  void dfs () {
    parent[] = -2;
    parent[0] = -1;
    go (0);
  }
}

alias T = Tuple!(int, int);

alias RBT = RedBlackTree!(T, "a < b", false);

void main() {
  auto r = new InputReader ();
  const n = r.next!uint;
  const k = r.next!uint;
  auto g = new G (n);
  foreach (eid; 1 .. n) {
    const i = r.next!uint - 1;
    const j = r.next!uint - 1;
    g.addEdge (i, j);
  }
  g.dfs ();
  auto t = new RBT();
  foreach (i; 1 .. n) if (g.e[i].length == 1) {
    t.insert (T (g.d[i], i));
  }
  long res;
  auto delta = new int[n];
  foreach (qid; 0 .. k) {
    auto q = t.back;
    debug stderr.writeln (q);
    res += q[0];
    const int j = q[1];
    int o = g.parent[j];
    if (o > 0) {
      delta[o] = delta[j] + 1;
      t.insert (T (g.d[o] - delta[o], o));
    }
    t.removeBack ();
  }
  writeln (res);
}


import std;
void main () {
	foreach (t; 0..readln.strip.to!int) {
		auto n = readln.strip.to!int;
		"21".cycle.drop (n % 3 == 1).take ((n * 2 + 1) / 3).writeln;
	}
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		writeln ("21".cycle.drop (n % 3 == 1).take ((n * 2 + 1) / 3));
	}
}


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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto r = RDA;
		foreach (i; 0..n)
		{
			if (r[i] == 2) continue;
			++ans[ti];
		}
	}

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

import std.stdio, std.string, std.algorithm, std.range;
void main()
{
    int q;
    readf!"%d\n"(q);
    foreach (_; 0 .. q)
    {
        readln();
        writeln(readln.split.filter!(a => a != "2").array.length);
    }
}


import std.stdio;
import std.algorithm;

void solve() {
    int n;
    readf(" %s", n);

    int A, B, C, D;
    A = D = -100000;
    B = C =  100000;
    foreach(i; 0..n) {
        int x, y;
        bool a, b, c, d;
        readf(" %s %s %b %b %b %b", x, y, a, b, c, d);
        // left, up, right, down
        if (!a) A = max(A, x);
        if (!b) B = min(B, y);
        if (!c) C = min(C, x);
        if (!d) D = max(D, y);
    }
    if (!(A <= C && D <= B)) writeln(0); else writeln(1, " ", (A+C)/2, " ", (B+D)/2);
}

void main() {
    int q;
    readf(" %s", q);

    foreach(i; 0..q) {
        solve();
    }
}

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 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; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
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 = 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 main()
{
	auto q = RD!int;
	auto ans = new long[][](q);
	foreach (i; 0..q)
	{
		auto n = RD!int;
		long l = -(10^^5), t = -(10^^5), r = 10^^5, b = 10^^5;
		foreach (j; 0..n)
		{
			auto a = RDA;
			if (a[2] == 0)
				l = max(l, a[0]);
			if (a[3] == 0)
				b = min(b, a[1]);
			if (a[4] == 0)
				r = min(r, a[0]);
			if (a[5] == 0)
				t = max(t, a[1]);
		}
		if (l > r || t > b)
		{
			ans[i] = [0];
		}
		else
		{
			ans[i] = [1, l, t];
		}
	}

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

import std.stdio;
import std.algorithm;

struct Point {
    int x;
    int y;
}

struct Action {
    bool up;
    bool down;
    bool left;
    bool right;
}

struct Box {
    int top;
    int bottom;
    int left;
    int right;

    bool valid() {
        return top >= bottom && left <= right;
    }
}

void solve() {
    int n;
    readf(" %s", n);

    auto p = new Point[n];
    auto a = new Action[n];
    foreach(i; 0..n) {
        readf(" %s %s", p[i].x, p[i].y);
        readf(" %b %b %b %b", a[i].left, a[i].up, a[i].right, a[i].down);
    }

    auto box = Box();
    box.top = int.max;
    box.bottom = int.min;
    box.left = int.min;
    box.right = int.max;
    auto inf = box;

    foreach(i; 0..n) {
        if (!a[i].up) {
            box.top = min(box.top, p[i].y);
        }
        if (!a[i].down) {
            box.bottom = max(box.bottom, p[i].y);
        }
        if (!a[i].left) {
            box.left = max(box.left, p[i].x);
        }
        if (!a[i].right) {
            box.right = min(box.right, p[i].x);
        }
        if (!box.valid()) {
            writeln(0);
            return;
        }
    }
    int x;
    if (box.left != inf.left) {
        x = box.left;
    } else if (box.right != inf.right) {
        x = box.right;
    } else {
        x = 0;
    }

    int y;
    if (box.top != inf.top) {
        y = box.top;
    } else if (box.bottom != inf.bottom){
        y = box.bottom;
    } else {
        y = 0;
    }
    writeln(1, " ", x, " ", y);
}

void main() {
    int q;
    readf(" %s", q);

    foreach(i; 0..q) {
        solve();
    }
}

import std.stdio;
import std.algorithm;

void main(){
	const int LOWER = -100000, HIGHER = 100000;
	int q;
	readf(" %s", &q);

	for(int i = 0; i < q; i++){
		int n = 0;
		readf(" %s", &n);

		int leftBound = LOWER;
		int rightBound = HIGHER;
		int bottomBound = LOWER;
		int topBound = HIGHER;

		for(int j = 0; j < n; j++){
			int x = 0, y = 0;
			readf(" %s %s", &x, &y);

			int downI = 0, upI = 0, rightI = 0, leftI = 0;
			readf(" %s %s %s %s", &leftI, &upI, &rightI, &downI);
			if(downI == 0){
				bottomBound = max(bottomBound, y);
			}
			if(upI == 0){
				topBound = min(topBound, y);
			}
			if(leftI == 0){
				leftBound = max(leftBound, x);
			}
			if(rightI == 0){
				rightBound = min(rightBound, x);
			}
		}
		if(rightBound < leftBound || topBound < bottomBound){
			writef("0\n");
		}else{
			writef("1 %d %d\n", leftBound, bottomBound);
		}
	}

}

import std.stdio;
import std.algorithm;

void main(){
	const int LOWER = -100000, HIGHER = 100000;
	int q;
	readf(" %s", &q);

	for(int i = 0; i < q; i++){
		int n = 0;
		readf(" %s", &n);

		int leftBound = LOWER;
		int rightBound = HIGHER;
		int bottomBound = LOWER;
		int topBound = HIGHER;

		for(int j = 0; j < n; j++){
			int x = 0, y = 0;
			readf(" %s %s", &x, &y);

			int downI = 0, upI = 0, rightI = 0, leftI = 0;
			readf(" %s %s %s %s", &downI, &upI, &rightI, &leftI);
			if(downI == 0){
				bottomBound = max(bottomBound, y);
			}
			if(upI == 0){
				topBound = min(topBound, y);
			}
			if(leftI == 0){
				leftBound = max(leftBound, x);
			}
			if(rightI == 0){
				rightBound = min(rightBound, x);
			}

			if(rightBound < leftBound || topBound < bottomBound){
				write("0\n");
			}else{
				write("1 %d, %d\n", leftBound, bottomBound);
			}
		}
	}

}

import std.stdio;
import std.algorithm;

void main(){
	const int LOWER = -100000, HIGHER = 100000;
	int q;
	readf(" %s", &q);

	for(int i = 0; i < q; i++){
		int n = 0;
		readf(" %s", &n);

		int leftBound = LOWER;
		int rightBound = HIGHER;
		int bottomBound = LOWER;
		int topBound = HIGHER;

		for(int j = 0; j < n; j++){
			int x = 0, y = 0;
			readf(" %s %s", &x, &y);

			int downI = 0, upI = 0, rightI = 0, leftI = 0;
			readf(" %s %s %s %s", &downI, &upI, &rightI, &leftI);
			if(downI == 0){
				bottomBound = max(bottomBound, y);
			}
			if(upI == 0){
				topBound = min(topBound, y);
			}
			if(leftI == 0){
				leftBound = max(leftBound, x);
			}
			if(rightI == 0){
				rightBound = min(rightBound, x);
			}
		}
		if(rightBound < leftBound || topBound < bottomBound){
			writef("0\n");
		}else{
			writef("1 %d %d\n", leftBound, bottomBound);
		}
	}

}


import std;

const int N = 1_000_101;

int n, c;
int[N] a, ex, s;

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    outer: foreach(_; 0..t)
    {
        scanf("%d %d\n", &n, &c);
        for(int i = 1;i <= c;i++) ex[i] = false;
        auto tmp = readln.split.to!(int[]);
        for(int i = 1;i <= n;i++) a[i] = tmp[i - 1], ex[a[i]] = true;
        for(int i = 1;i <= c;i++) s[i] = s[i - 1] + ex[i];
        for(int k = 1;k <= c;k++) if(!ex[k]) {
        for(int y = 1;k * y <= c;y++) if(ex[y]) {
            int l = k * y, r = min(c,(k + 1) * y - 1);
                if(s[r] - s[l - 1]) {
                    printf("No\n");
                    continue outer;
                }
            }
        }
        printf("Yes\n");
    }
}

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

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

struct BIT(Num, Num zero) {
    int N;
    Num[] dat; // dat is 1-indexed
    this(int N) {
        this.N = N;
        dat = new Num[N + 1];
        dat[] = zero;
    }
    // add x to i-th (0-indexed) element
    void add(int i, Num x) {
        i++; // make 1-indexed
        while (i <= N) {
            dat[i] += x;
            i += i & -i;
        }
    }
    void add(long i, Num x) { add(cast(int)i, x); }
    // return the sum of [0, i) (0-indexed) element
    Num query(int i) {
        Num s = zero;
        while (i > 0) {
            s += dat[i];
            i -= i & -i;
        }
        return s;
    }
    Num query(long i) { return query(cast(int)i); }
}

void solve() {
    int N, C; readf("%d %d\n", &N, &C);
    auto A = readarray!int;
    A = A.sort.uniq.array;
    N = A.length;

    bool f() {
        if (A[0] != 1) return false;
        auto M = new bool[C+1];
        foreach (a; A) M[a] = true;
        auto R = iota(1, C+1, 1).filter!(i => !M[i]).array;
        auto bit = BIT!(long, 0)(C+1);
        foreach (long r; R) {
            foreach (long a; A) {
                if (r * a > C) break;
                bit.add(r*a, 1);
                bit.add(r*a + a, -1);
            }
        }
        foreach (a; A) {
            if (bit.query(a+1) >= 1) return false;
        }
        return true;
    }
    writeln(f() ? "Yes" : "No");
}

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

import std;

const int N = 1_000_101;

int n, c;
int[N] a, ex, s;

int read() {
    int x = 0, f = 1;
    auto ch = getchar();
    while(ch > '9' || ch < '0') { if(ch == '-') f = -1; ch = getchar(); }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar();
    return x * f;
}

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    outer: foreach(_; 0..t)
    {
        scanf("%d %d\n", &n, &c);
        for(int i = 1;i <= c;i++) ex[i] = false;
        for(int i = 1;i <= n;i++) a[i] = read(), ex[a[i]] = true;
        for(int i = 1;i <= c;i++) s[i] = s[i - 1] + ex[i];
        for(int k = 1;k <= c;k++) if(!ex[k]) {
        for(int y = 1;k * y <= c;y++) if(ex[y]) {
            int l = k * y, r = min(c,(k + 1) * y - 1);
                if(s[r] - s[l - 1]) {
                    printf("No\n");
                    continue outer;
                }
            }
        }
        printf("Yes\n");
    }
}

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

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

struct SegmentTree(T, T unit, alias binop) {
    int n;
    T[] dat;
    this(int n_) {
        n = 1;
        while (n < n_) n *= 2;
        dat = new T[2*n+2];
        dat[] = unit;
    }
    void update(int k, in T a) {
        k += n - 1;
        dat[k] = a;
        while (k > 0) {
            k = (k - 1) / 2;
            dat[k] = binop(dat[k * 2 + 1], dat[k * 2 + 2]);
        }
    }
    T query(int a, int b) const {
        return query(a, b, 0, 0, n);
    }
    T query(int a, int b, int k, int l, int r) const {
        if (r <= a || b <= l) return unit;
        if (a <= l && r <= b) return dat[k];
        T vl = query(a, b, k * 2 + 1, l, (l + r) / 2),
          vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
        return binop(vl, vr);
    }
}
alias RSQ = SegmentTree!(long, 0, (a, b) => a + b);

struct BIT(Num, Num zero) {
    int N;
    Num[] dat; // dat is 1-indexed
    this(int N) {
        this.N = N;
        dat = new Num[N + 1];
        dat[] = zero;
    }
    // add x to i-th (0-indexed) element
    void add(int i, Num x) {
        i++; // make 1-indexed
        while (i <= N) {
            dat[i] += x;
            i += i & -i;
        }
    }
    void add(long i, Num x) { add(cast(int)i, x); }
    // return the sum of [0, i) (0-indexed) element
    Num query(int i) {
        Num s = zero;
        while (i > 0) {
            s += dat[i];
            i -= i & -i;
        }
        return s;
    }
    Num query(long i) { return query(cast(int)i); }
}

/*
void solve() {
    int N, C; readf("%d %d\n", &N, &C);
    auto A = readarray!int;
    A = A.sort.uniq.array;
    N = A.length;
    bool f() {
        if (A[0] != 1) return false;
        if (N == 1) return true;
        auto P = RSQ(C+2);
        P.update(A[1], 1);
        P.update(A[1]+A[1], -1);
        P.update(min(C+1, A[1] * A[1]), 1);
        P.update(min(C+1, A[1] * A[1] + A[1]), -1);
        for (int i = 2; i < N; i++) {
            if (P.query(0, A[i]+1) <= 0) return false;
            for (int j = 0; j <= i; j++) {
                int L = A[i] * A[j];
                if (L > C) break;
                int R = min(C + 1, L + A[j]);
                P.update(L, 1);
                P.update(R, -1);
            }
        }
        return true;
    }
    writeln(f() ? "Yes" : "No");
}
*/

void solve() {
    int N, C; readf("%d %d\n", &N, &C);
    auto A = readarray!long;
    A = A.sort.uniq.array;
    N = A.length;

    bool f() {
        if (A[0] != 1) return false;
        if (N == 1) return true;
        auto P = BIT!(long, 0)(C+2);
        P.add(A[1], 1);
        P.add(A[1]+A[1], -1);
        P.add(min(C+1, A[1] * A[1]), 1);
        P.add(min(C+1, A[1] * A[1] + A[1]), -1);
        for (int i = 2; i < N; i++) {
            if (P.query(cast(int)(A[i]+1)) <= 0) return false;
            for (int j = 0; j <= i; j++) {
                long L = A[i] * A[j];
                if (L > C) break;
                long R = min(C + 1, L + A[j]);
                P.add(L, 1);
                P.add(R, -1);
            }
        }
        return true;
    }
    writeln(f() ? "Yes" : "No");
}

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

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

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

struct SegmentTree(T, T unit, alias binop) {
    int n;
    T[] dat;
    this(int n_) {
        n = 1;
        while (n < n_) n *= 2;
        dat = new T[2*n+2];
        dat[] = unit;
    }
    void update(int k, in T a) {
        k += n - 1;
        dat[k] = a;
        while (k > 0) {
            k = (k - 1) / 2;
            dat[k] = binop(dat[k * 2 + 1], dat[k * 2 + 2]);
        }
    }
    T query(int a, int b) const {
        return query(a, b, 0, 0, n);
    }
    T query(int a, int b, int k, int l, int r) const {
        if (r <= a || b <= l) return unit;
        if (a <= l && r <= b) return dat[k];
        T vl = query(a, b, k * 2 + 1, l, (l + r) / 2),
          vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
        return binop(vl, vr);
    }
}
alias RSQ = SegmentTree!(long, 0, (a, b) => a + b);

struct BIT(Num, Num zero) {
    int N;
    Num[] dat; // dat is 1-indexed
    this(int N) {
        this.N = N;
        dat = new Num[N + 1];
        dat[] = zero;
    }
    // add x to i-th (0-indexed) element
    void add(int i, Num x) {
        i++; // make 1-indexed
        while (i <= N) {
            dat[i] += x;
            i += i & -i;
        }
    }
    // return the sum of [0, i) (0-indexed) element
    Num query(int i) {
        Num s = zero;
        while (i > 0) {
            s += dat[i];
            i -= i & -i;
        }
        return s;
    }
}

/*
void solve() {
    int N, C; readf("%d %d\n", &N, &C);
    auto A = readarray!int;
    A = A.sort.uniq.array;
    N = A.length;
    bool f() {
        if (A[0] != 1) return false;
        if (N == 1) return true;
        auto P = RSQ(C+2);
        P.update(A[1], 1);
        P.update(A[1]+A[1], -1);
        P.update(min(C+1, A[1] * A[1]), 1);
        P.update(min(C+1, A[1] * A[1] + A[1]), -1);
        for (int i = 2; i < N; i++) {
            if (P.query(0, A[i]+1) <= 0) return false;
            for (int j = 0; j <= i; j++) {
                int L = A[i] * A[j];
                if (L > C) break;
                int R = min(C + 1, L + A[j]);
                P.update(L, 1);
                P.update(R, -1);
            }
        }
        return true;
    }
    writeln(f() ? "Yes" : "No");
}
*/

void solve() {
    int N, C; readf("%d %d\n", &N, &C);
    auto A = readarray!int;
    A = A.sort.uniq.array;
    N = A.length;

    bool f() {
        if (A[0] != 1) return false;
        if (N == 1) return true;
        auto P = BIT!(int, 0)(C+2);
        P.add(A[1], 1);
        P.add(A[1]+A[1], -1);
        P.add(min(C+1, A[1] * A[1]), 1);
        P.add(min(C+1, A[1] * A[1] + A[1]), -1);
        for (int i = 2; i < N; i++) {
            if (P.query(A[i]+1) <= 0) return false;
            for (int j = 0; j <= i; j++) {
                int L = A[i] * A[j];
                if (L > C) break;
                int R = min(C + 1, L + A[j]);
                P.add(L, 1);
                P.add(R, -1);
            }
        }
        return true;
    }
    writeln(f() ? "Yes" : "No");
}

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