#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}

int main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%d %d", &n, &m);
    ll A = 1, b = 1;
    ll c = 1, d = 1;
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%d", &a);
        A *= a;
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%d", &a);
        b *= a;
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vi rn, rd;
    _set <int> ok;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    printf("%u %u\n", sz(rn), sz(rd));
    for_each(it(rn), [&](int i) { c *= i, printf("%d ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { d *= i, printf("%d ", i); });
    putchar('\n');
    //eprintf("%lld == %lld\n", c / d, A / b);
    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}

#define int ll

int_fast16_t main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    ll A = 1, b = 1;
    ll c = 1, d = 1;
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        A *= a;
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        b *= a;
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vll rn, rd;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    cout << sz(rn) << ' ' << sz(rd) << endl;
    for_each(it(rn), [&](int i) { c *= i, printf("%lld ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { d *= i, printf("%lld ", i); });
    putchar('\n');
    //eprintf("%lld == %lld\n", c / d, A / b);
    return 0;
}


//Name         : Shinchan Nohara
//Age          : 5 years
//Organisation : Kasukabe Defense Force

#include <iostream>
#include <ctime>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <cassert>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iterator>
#include <fstream>
using namespace std;

typedef long long 		int64;
typedef vector<int> 		vi;
typedef string 			ST;
typedef stringstream 		SS;
typedef vector< vector<int> > 	vvi;
typedef pair<int,int> 		ii;
typedef vector<string> 		vs;
/*
#ifdef __cplusplus
	#undef __cplusplus
	#define __cplusplus 199712L
#endif
#if __cplusplus > 199711L	// for g++0x, value of __cplusplus must be greater thana 199711L.
	#define tr(i, c)	for(auto i = begin(c); i != end(c); i++)
#else
	#define tr(i, c)	for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#endif
*/
#define DEBUG(x)	cout << #x << " = " << x << "\n"
#define endl		("\n")
#define tr(i, c)	for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

#define PI		M_PI
#define E 		M_E
#define	ep		1e-9

#define	Sf		scanf
#define	Pf		printf

#define forn(i, n)	for(int i = 0, lets_stop_here = (int)n; i <  lets_stop_here; i++)
#define forab(i, a, b)	for(int i = a, lets_stop_here = (int)b; i <= lets_stop_here; i++)
#define rep(i, a, b)	for(int i = a, lets_stop_here = (int)b; i >= lets_stop_here; i--)

#define	all(c)		(c).begin(), (c).end()
#define	CL(a, b)	memset(a, b, sizeof(a))
#define mp		make_pair

#define pb		push_back

#define	present(x, c)	((c).find(x) != (c).end())	//map & set//
#define	cpresent(x, c)	(find( (c).begin(), (c).end(), x) != (c).end())	//vector & list//

#define read(n)		scanf("%d", &n)
#define write(n)	printf("%d ", n)
#define writeln(n)	printf("%d\n", n)

const int sz = 1e7 + 100;
int par[sz];
int N, M;
int A[sz], B[sz];
int comA[sz], comB[sz];

void factorize(int sz, int AA[], int count[]) {
	forn(i, sz) {
		int x = AA[i];
		while(x != par[x]) {
			count[par[x]]++;
			x /= par[x];
		}
		if(x != 1)
			count[x]++;
	}
}

void Reduce(int sz, int Arr[], int count[]) {
	forn(i, sz) {
		int x = Arr[i];

		while(x > 1) {
			if(count[par[x]]) {
				count[par[x]]--;
				Arr[i] /= par[x];
			}
			x /= par[x];
		}
	}
}

int main()
{
	forn(i, sz)
		par[i] = i;
	for(int i = 2; i*i < sz; i ++)
		if(par[i] == i)
			for(int j = i+i; j < sz; j+=i)
				par[j] = i;

	read(N);
	read(M);
#ifdef DEBUG
	#undef DEBUG
	#define DEBUG
#endif

	DEBUG(N);
	DEBUG(M);
	forn(i, N)
		read(A[i]);
	DEBUG(A[0]);
	forn(i, M)
		read(B[i]);
	DEBUG(B[0]);

	factorize(N, A, comA);
	DEBUG("fact(A)");
	factorize(M, B, comB);
	forn(i, sz)
		comA[i] = comB[i] = min(comA[i], comB[i]);

	Reduce(N, A, comA);
	Reduce(M, B, comB);

	cout << N << " " << M << endl;
	forn(i, N)
		write(A[i]);
	cout << endl;
	forn(i, M)
		write(B[i]);
	cout << endl;

	return 0;
}


//Name         : Shinchan Nohara
//Age          : 5 years
//Organisation : Kasukabe Defense Force

#include <iostream>
#include <ctime>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <cassert>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iterator>
#include <fstream>
using namespace std;

typedef long long 		int64;
typedef vector<int> 		vi;
typedef string 			ST;
typedef stringstream 		SS;
typedef vector< vector<int> > 	vvi;
typedef pair<int,int> 		ii;
typedef vector<string> 		vs;
/*
#ifdef __cplusplus
	#undef __cplusplus
	#define __cplusplus 199712L
#endif
#if __cplusplus > 199711L	// for g++0x, value of __cplusplus must be greater thana 199711L.
	#define tr(i, c)	for(auto i = begin(c); i != end(c); i++)
#else
	#define tr(i, c)	for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#endif
*/
#define DEBUG(x)	cout << #x << " = " << x << "\n"
#define endl		("\n")
#define tr(i, c)	for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

#define PI		M_PI
#define E 		M_E
#define	ep		1e-9

#define	Sf		scanf
#define	Pf		printf

#define forn(i, n)	for(int i = 0, lets_stop_here = (int)n; i <  lets_stop_here; i++)
#define forab(i, a, b)	for(int i = a, lets_stop_here = (int)b; i <= lets_stop_here; i++)
#define rep(i, a, b)	for(int i = a, lets_stop_here = (int)b; i >= lets_stop_here; i--)

#define	all(c)		(c).begin(), (c).end()
#define	CL(a, b)	memset(a, b, sizeof(a))
#define mp		make_pair

#define pb		push_back

#define	present(x, c)	((c).find(x) != (c).end())	//map & set//
#define	cpresent(x, c)	(find( (c).begin(), (c).end(), x) != (c).end())	//vector & list//

#define read(n)		scanf("%d", &n)
#define write(n)	printf("%d ", n)
#define writeln(n)	printf("%d\n", n)

const int sz = 1e7 + 100;

int par[sz];

void init() {
	forn(i, sz)
		par[i] = i;

	for(int i = 2; i*i < sz; i++)	if(par[i] == i) {
		for(int j = 2*i; j < sz; j+=i)
			par[j] = i;
	}
}
#ifdef DEBUG
	#undef DEBUG
#endif
#define DEBUG	;

int main()
{
	init();
	int N, M;
	cin >> N >> M;
	map <int, int> num, den;

	DEBUG("#1");
	forn(i, N) {
		int x;
		read(x);
//		cout << x << ": ";

		while(par[x] != x) {
//			cout << par[x] << " ";
			num[par[x]]++;
			x /= par[x];
		}
//		cout << x << endl;
		num[x]++;
	}
//	cout << endl;

	DEBUG("#2");
	forn(i, M) {
		int x;
		read(x);
//		cout << x << ": ";

		while(par[x] != x) {
//			cout << par[x] << " ";
			den[par[x]]++;
			x /= par[x];
		}
//		cout << x << endl;
		den[x]++;
	}

	DEBUG("#3");
	map <int, int> com ;
	for(auto it1 = num.begin(), it2 = den.begin(); it1 != num.end() && it2 != den.end(); ) {
		if(it1->first == it2->first)  {
			com[it1->first] = min(it1->second, it2->second);
			it1++;
			it2++;
		}
		else if (it1->first > it2->first)
			it2++;
		else
			it1++;
	}
	DEBUG("#4");
/*
	cout << "com\n";
	tr(it, com)
		cout << it->first << ": " << it->second << endl;
	cout << endl;

	cout << "num\n";
	tr(it, num)
		cout << it->first << ": " << it->second << endl;
	cout << endl;

	cout << "den\n";
	tr(it, den)
		cout << it->first << ": " << it->second << endl;
	cout << endl;
*/

	for(auto it = num.begin(); it != num.end(); ) {
		it->second -= com[it->first];
		if(it->second == 0)
			it = num.erase(it);
		else
			it++;
	}
	DEBUG("#5");
	for(auto it = den.begin(); it != den.end(); ) {
		it->second -= com[it->first];
		if(it->second == 0)
			it = den.erase(it);
		else
			it++;
	}

	vector <int> n, d;
	DEBUG("#6");

	for(auto it = num.begin(); it != num.end(); ) {
		int64 tmp = 1;
		while(it->second > 0 && tmp * it->first <= 1e7) {
			tmp *= it->first;
			it->second--;
		}
		if(tmp != 1)
			n.pb(tmp);
		if(it->second == 0)
			it++;
	}

	for(auto it = den.begin(); it != den.end(); ) {
		int64 tmp = 1;
		while(it->second > 0 && tmp * it->first <= 1e7) {
			tmp *= it->first;
			it->second--;
		}
		if(tmp != 1)
			d.pb(tmp);
		if(it->second == 0)
			it++;
	}

	if(n.size() == 0)
		n.pb(1);
	if(d.size() == 0)
		d.pb(1);

	cout << n.size() << " " << d.size() << endl;
	tr(it, n)
		write(*it);
	cout <<endl;
	tr(it, d)
		write(*it);

	return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;

const int N = int(1e7)+1;
int pr[N];
vector<int> primes;
map<int,int> myMap;
set<int> prim;
vector<int> verh, niz;

int main() {
   	for(int i = 2; i <= N; i++){
      	if(pr[i] == 0){
         	primes.push_back(i);
         	pr[i] = i;
      	}
      	for(int j = 0; j < primes.size() && i*primes[j] <= N && pr[i] >= primes[j]; j++)
         	pr[i*primes[j]] = primes[j];
   	}
   	int n, m; cin >> n >> m;
	for(int i = 0; i < n; i++){
		int a; cin >> a;
		while(a!=1){
			myMap[pr[a]]++;
			prim.insert(pr[a]);
			a /= pr[a];
		}
	}
	for(int i = 0; i < m; i++){
		int a; cin >> a;
		while(a!=1){
			myMap[pr[a]]--;
			prim.insert(pr[a]);
			a /= pr[a];
		}
	}
	for(auto i = prim.begin(); i != prim.end(); i++)
		if(myMap[*i] > 0){
			ull a = 1;
			while(myMap[*i]){
				a *= (*i);
				myMap[*i]--;
				if(a > int(1e7) || myMap[*i] == 0){
					if(a > int(1e7)){
						a /= (*i);
						myMap[*i]++;
					}
					verh.push_back(a);
					a = 1;
				}
			}
		}else if(myMap[*i] < 0){
			ull a = 1;
			while(myMap[*i]){
				a *= (*i);
				myMap[*i]++;
				if(a > int(1e7) || myMap[*i] == 0){
					if(a > int(1e7)){
						a /= (*i);
						myMap[*i]--;
					}
					niz.push_back(a);
					a = 1;
				}
			}
		}
	cout << max(int(verh.size()),1) << ' ' << max(int(niz.size()),1) << endl;
	if(verh.size()) for(auto i : verh) cout << i << ' ';
	else cout << 1;
	cout << endl;
	if(niz.size()) for(auto i : niz) cout << i << ' ';
	else cout << 1;
	cout << endl;
   	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;

const int N = int(1e7);
int pr[N + 1]; //the least prime divisor

//#define DEBUG

//план Б: если ассоциативные массивы работают слишком медленно
/*int nom[N + 1];
int denom[N + 1];

int new_nom[N + 1];
int new_denom[N + 1];*/

int a[int(1e5)];
int b[int(1e5)];

int main()
{
    //freopen("in", "r", stdin);
    ios::sync_with_stdio(false);
    //линейное решето Эратосфена
    vector<int> primes;
    for(size_t i = 2; i <= N; i++){
        if(!pr[i]) pr[i] = i, primes.push_back(i);
        for(size_t j = 0; j < primes.size() && i * primes[j] <= N && pr[i] >= primes[j]; j++)
            pr[primes[j] * i] = pr[i];
    }

    int n, m; cin >> n >> m;
    //факторизация числителя
    map<int, int> nominator;
    for(int i = 0; i < n; i++){
        cin >> a[i]; int x = a[i];
        while(x != 1){
            nominator[pr[x]]++;
            x /= pr[x];
        }
    }
    //факторизация знаменателя
    map<int, int> denominator;
    for(int i = 0; i < m; i++){
        cin >> b[i]; int x = b[i];
        while(x != 1){
            denominator[pr[x]]++;
            x /= pr[x];
        }
    }
    //факторизация числителя после сокращения
    map<int, int> new_nominator;
    for(auto prime = nominator.begin(); prime != nominator.end(); prime++)
        new_nominator[prime->first] = prime->second - min(prime->second, denominator[prime->first]);
    //факторизация знаменателя после сокращения
    map<int, int> new_denominator;
    for(auto prime = denominator.begin(); prime != denominator.end(); prime++)
        new_denominator[prime->first] = prime->second - min(prime->second, nominator[prime->first]);

    #ifdef DEBUG
    auto print = [](map<int, int> some_map){
        for(auto it = some_map.begin(); it != some_map.end(); it++)
            cout << "(" << (it->first) << " " << (it->second) << ")" << endl;
        cout << endl;
    };

    cout << "NOMINATOR" << endl;
    print(nominator);

    cout << "DENOMINATOR" << endl;
    print(denominator);

    cout << "NEW NOMINATOR" << endl;
    print(new_nominator);

    cout << "NEW DENOMINATOR" << endl;
    print(new_denominator);
    #endif

    auto print = [](map<int, int> some_map){
        for(auto it = some_map.begin(); it != some_map.end(); it++)
            cout << "(" << (it->first) << " " << (it->second) << ")" << endl;
        cout << endl;
    };

    for(int i = 0; i < n; i++)
        while(nominator[pr[a[i]]] > new_nominator[pr[a[i]]]){
            nominator[pr[a[i]]]--;
            a[i] /= pr[a[i]];
            if(a[i] == 1) break;
        }

    for(int i = 0; i < m; i++){
        while(denominator[pr[b[i]]] != new_denominator[pr[b[i]]]){
            denominator[pr[b[i]]]--;
            b[i] /= pr[b[i]];
            if(b[i] == 1) break;
        }
    }
    cout << n << " " << m << endl;
    for(int i = 0; i < n; i++)
        cout << a[i] << " ";
    cout << endl;
    for(int i = 0; i < m; i++)
        cout << b[i] << " ";

    return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

const int N = int(1e7)+1;
int pr[N];
vector<int> primes;
map<int,int> myMap;
set<int> prim;
vector<int> verh, niz;

int main() {
   	for(int i = 2; i <= N; i++){
      	if(pr[i] == 0){
         	primes.push_back(i);
         	pr[i] = i;
      	}
      	for(int j = 0; j < primes.size() && i*primes[j] <= N && pr[i] >= primes[j]; j++)
         	pr[i*primes[j]] = primes[j];
   	}
   	int n, m; cin >> n >> m;
	for(int i = 0; i < n; i++){
		int a; cin >> a;
		while(a!=1){
			myMap[pr[a]]++;
			prim.insert(pr[a]);
			a /= pr[a];
		}
	}
	for(int i = 0; i < m; i++){
		int a; cin >> a;
		while(a!=1){
			myMap[pr[a]]--;
			prim.insert(pr[a]);
			a /= pr[a];
		}
	}
	for(auto i = prim.begin(); i != prim.end(); i++){
		if(myMap[*i] > 0){
			int a = 1;
			for(int j = 0; j < myMap[*i]; j++)
				a*=(*i);
			verh.push_back(a);
		}else if(myMap[*i] < 0){
			int a = 1;
			for(int j = 0; j < abs(myMap[*i]); j++)
				a*=(*i);
			niz.push_back(*i);
		}
	}
	cout << max(int(verh.size()),1) << ' ' << max(int(niz.size()),1) << endl;
	if(verh.size()) for(auto i : verh) cout << i << ' ';
	else cout << 1;
	cout << endl;
	if(niz.size()) for(auto i : niz) cout << i << ' ';
	else cout << 1;
	cout << endl;
   	return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;

const int N = int(1e7)+1;
int pr[N];
vector<int> primes;
map<int,int> myMap;
set<int> prim;
vector<int> verh, niz;

int main() {
   	for(int i = 2; i <= N; i++){
      	if(pr[i] == 0){
         	primes.push_back(i);
         	pr[i] = i;
      	}
      	for(int j = 0; j < primes.size() && i*primes[j] <= N && pr[i] >= primes[j]; j++)
         	pr[i*primes[j]] = primes[j];
   	}
   	int n, m; cin >> n >> m;
	for(int i = 0; i < n; i++){
		int a; cin >> a;
		while(a!=1){
			myMap[pr[a]]++;
			prim.insert(pr[a]);
			a /= pr[a];
		}
	}
	for(int i = 0; i < m; i++){
		int a; cin >> a;
		while(a!=1){
			myMap[pr[a]]--;
			prim.insert(pr[a]);
			a /= pr[a];
		}
	}
	for(auto i = prim.begin(); i != prim.end(); i++)
		if(myMap[*i] > 0){
			ull a = 1;
			while(myMap[*i]){
				a *= (*i);
				myMap[*i]--;
				if(a > int(1e6) || myMap[*i] == 0){
					verh.push_back(a);
					a = 1;
				}
			}
		}else if(myMap[*i] < 0){
			ull a = 1;
			while(myMap[*i]){
				a *= (*i);
				myMap[*i]++;
				if(a > int(1e6) || myMap[*i] == 0){
					niz.push_back(a);
					a = 1;
				}
			}
		}
	cout << max(int(verh.size()),1) << ' ' << max(int(niz.size()),1) << endl;
	if(verh.size()) for(auto i : verh) cout << i << ' ';
	else cout << 1;
	cout << endl;
	if(niz.size()) for(auto i : niz) cout << i << ' ';
	else cout << 1;
	cout << endl;
   	return 0;
}


#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;

const int N = int(1e7);
int pr[N + 1]; //the least prime divisor

//#define DEBUG

//план Б: если ассоциативные массивы работают слишком медленно
/*int nom[N + 1];
int denom[N + 1];

int new_nom[N + 1];
int new_denom[N + 1];*/

int a[int(1e5)];
int b[int(1e5)];

int main()
{
    //freopen("in", "r", stdin);
    ios::sync_with_stdio(false);
    //линейное решето Эратосфена
    vector<int> primes;
    for(size_t i = 2; i <= N; i++){
        if(!pr[i]) pr[i] = i, primes.push_back(i);
        for(size_t j = 0; j < primes.size() && i * primes[j] <= N && pr[i] >= primes[j]; j++)
            pr[primes[j] * i] = pr[i];
    }

    int n, m; cin >> n >> m;
    //факторизация числителя
    map<int, int> nominator;
    for(int i = 0; i < n; i++){
        cin >> a[i]; int x = a[i];
        while(x != 1){
            nominator[pr[x]]++;
            x /= pr[x];
        }
    }
    //факторизация знаменателя
    map<int, int> denominator;
    for(int i = 0; i < m; i++){
        cin >> b[i]; int x = b[i];
        while(x != 1){
            denominator[pr[x]]++;
            x /= pr[x];
        }
    }
    //факторизация числителя после сокращения
    map<int, int> new_nominator;
    for(auto prime = nominator.begin(); prime != nominator.end(); prime++)
        new_nominator[prime->first] = prime->second - min(prime->second, denominator[prime->first]);
    //факторизация знаменателя после сокращения
    map<int, int> new_denominator;
    for(auto prime = denominator.begin(); prime != denominator.end(); prime++)
        new_denominator[prime->first] = prime->second - min(prime->second, nominator[prime->first]);

    #ifdef DEBUG
    auto print = [](map<int, int> some_map){
        for(auto it = some_map.begin(); it != some_map.end(); it++)
            cout << "(" << (it->first) << " " << (it->second) << ")" << endl;
        cout << endl;
    };

    auto print = [](map<int, int> some_map){
        for(auto it = some_map.begin(); it != some_map.end(); it++)
            cout << "(" << (it->first) << " " << (it->second) << ")" << endl;
        cout << endl;
    };

    cout << "NOMINATOR" << endl;
    print(nominator);

    cout << "DENOMINATOR" << endl;
    print(denominator);

    cout << "NEW NOMINATOR" << endl;
    print(new_nominator);

    cout << "NEW DENOMINATOR" << endl;
    print(new_denominator);
    #endif

    for(int i = 0; i < n; i++)
        while(nominator[pr[a[i]]] > new_nominator[pr[a[i]]]){
            nominator[pr[a[i]]]--;
            a[i] /= pr[a[i]];
            if(a[i] == 1) break;
        }

    for(int i = 0; i < m; i++){
        while(denominator[pr[b[i]]] != new_denominator[pr[b[i]]]){
            denominator[pr[b[i]]]--;
            b[i] /= pr[b[i]];
            if(b[i] == 1) break;
        }
    }

    sort(a, a + n, [](int x, int y){return x > y;});
    sort(b, b + m, [](int x, int y){return x > y;});

    int top = 0, bottom = 0;
    for(int i = 0; i < n; i++){
        if(a[i] == 1 && i > 0) break;
        top++;
    }
    for(int i = 0; i < m; i++){
        if(b[i] == 1 && i > 0) break;
        bottom++;
    }
    cout << top << " " << bottom << endl;
    for(int i = 0; i < n; i++){
        if(b[i] == 1 && i > 0) break;
        cout << a[i] << " ";
    }
    cout << endl;
    for(int i = 0; i < m; i++){
        if(b[i] == 1 && i > 0) break;
        cout << b[i] << " ";
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;

const int N = int(1e7);
int pr[N + 1]; //the least prime divisor

//#define DEBUG

//план Б: если ассоциативные массивы работают слишком медленно
/*int nom[N + 1];
int denom[N + 1];

int new_nom[N + 1];
int new_denom[N + 1];*/

int a[int(1e5)];
int b[int(1e5)];

int main()
{
    //freopen("in", "r", stdin);
    ios::sync_with_stdio(false);
    //линейное решето Эратосфена
    vector<int> primes;
    for(size_t i = 2; i <= N; i++){
        if(!pr[i]) pr[i] = i, primes.push_back(i);
        for(size_t j = 0; j < primes.size() && i * primes[j] <= N && pr[i] >= primes[j]; j++)
            pr[primes[j] * i] = pr[i];
    }

    int n, m; cin >> n >> m;
    //факторизация числителя
    map<int, int> nominator;
    for(int i = 0; i < n; i++){
        cin >> a[i]; int x = a[i];
        while(x != 1){
            nominator[pr[x]]++;
            x /= pr[x];
        }
    }
    //факторизация знаменателя
    map<int, int> denominator;
    for(int i = 0; i < m; i++){
        cin >> b[i]; int x = b[i];
        while(x != 1){
            denominator[pr[x]]++;
            x /= pr[x];
        }
    }
    //факторизация числителя после сокращения
    map<int, int> new_nominator;
    for(auto prime = nominator.begin(); prime != nominator.end(); prime++)
        new_nominator[prime->first] = prime->second - min(prime->second, denominator[prime->first]);
    //факторизация знаменателя после сокращения
    map<int, int> new_denominator;
    for(auto prime = denominator.begin(); prime != denominator.end(); prime++)
        new_denominator[prime->first] = prime->second - min(prime->second, nominator[prime->first]);

    #ifdef DEBUG
    auto print = [](map<int, int> some_map){
        for(auto it = some_map.begin(); it != some_map.end(); it++)
            cout << "(" << (it->first) << " " << (it->second) << ")" << endl;
        cout << endl;
    };

    cout << "NOMINATOR" << endl;
    print(nominator);

    cout << "DENOMINATOR" << endl;
    print(denominator);

    cout << "NEW NOMINATOR" << endl;
    print(new_nominator);

    cout << "NEW DENOMINATOR" << endl;
    print(new_denominator);
    #endif

    auto print = [](map<int, int> some_map){
        for(auto it = some_map.begin(); it != some_map.end(); it++)
            cout << "(" << (it->first) << " " << (it->second) << ")" << endl;
        cout << endl;
    };

    for(int i = 0; i < n; i++)
        while(nominator[pr[a[i]]] > new_nominator[pr[a[i]]]){
            nominator[pr[a[i]]]--;
            a[i] /= pr[a[i]];
            if(a[i] == 1) break;
        }

    for(int i = 0; i < m; i++){
        while(denominator[pr[b[i]]] != new_denominator[pr[b[i]]]){
            denominator[pr[b[i]]]--;
            b[i] /= pr[b[i]];
            if(b[i] == 1) break;
        }
    }

    for(int i = 0; i < n; i++)
        cout << a[i] << " ";
    cout << endl;
    for(int i = 0; i < m; i++)
        cout << b[i] << " ";

    return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
using namespace std;

const int N = int(1e7)+1;
int pr[N];
vector<int> primes;
map<int,int> myMap;
set<int> prim;
vector<int> verh, niz;

int main() {
   	for(int i = 2; i <= N; i++){
      	if(pr[i] == 0){
         	primes.push_back(i);
         	pr[i] = i;
      	}
      	for(int j = 0; j < primes.size() && i*primes[j] <= N && pr[i] >= primes[j]; j++)
         	pr[i*primes[j]] = primes[j];
   	}
   	int n, m; cin >> n >> m;
	for(int i = 0; i < n; i++){
		int a; cin >> a;
		while(a!=1){
			myMap[pr[a]]++;
			prim.insert(pr[a]);
			a /= pr[a];
		}
	}
	for(int i = 0; i < m; i++){
		int a; cin >> a;
		while(a!=1){
			myMap[pr[a]]--;
			prim.insert(pr[a]);
			a /= pr[a];
		}
	}
	for(auto i = prim.begin(); i != prim.end(); i++){
		if(myMap[*i] > 0) verh.push_back(*i);
		else if(myMap[*i] < 0) niz.push_back(*i);
	}
	cout << verh.size() << ' ' << niz.size() << endl;
	for(auto i: verh) cout << i << ' '; cout << endl;
	for(auto i: niz ) cout << i << ' '; cout << endl;
   	return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
typedef unsigned long long ull

const int N = int(1e7)+1;
int pr[N];
vector<int> primes;
map<int,int> myMap;
set<int> prim;
vector<int> verh, niz;

int main() {
   	for(int i = 2; i <= N; i++){
      	if(pr[i] == 0){
         	primes.push_back(i);
         	pr[i] = i;
      	}
      	for(int j = 0; j < primes.size() && i*primes[j] <= N && pr[i] >= primes[j]; j++)
         	pr[i*primes[j]] = primes[j];
   	}
   	int n, m; cin >> n >> m;
	for(int i = 0; i < n; i++){
		int a; cin >> a;
		while(a!=1){
			myMap[pr[a]]++;
			prim.insert(pr[a]);
			a /= pr[a];
		}
	}
	for(int i = 0; i < m; i++){
		int a; cin >> a;
		while(a!=1){
			myMap[pr[a]]--;
			prim.insert(pr[a]);
			a /= pr[a];
		}
	}
	for(auto i = prim.begin(); i != prim.end(); i++)
		if(myMap[*i] > 0){
			ull a = 1;
			while(myMap[*i]){
				a *= (*i);
				myMap[*i]--;
				if(a > int(1e6) || myMap[*i] == 0){
					verh.push_back(a);
					a = 1;
				}
			}
		}else if(myMap[*i] < 0){
			ull a = 1;
			while(myMap[*i]){
				a *= (*i);
				myMap[*i]++;
				if(a > int(1e6) || myMap[*i] == 0){
					niz.push_back(a);
					a = 1;
				}
			}
		}
	cout << max(int(verh.size()),1) << ' ' << max(int(niz.size()),1) << endl;
	if(verh.size()) for(auto i : verh) cout << i << ' ';
	else cout << 1;
	cout << endl;
	if(niz.size()) for(auto i : niz) cout << i << ' ';
	else cout << 1;
	cout << endl;
   	return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

const int N = int(1e7)+1;
int pr[N];
vector<int> primes;
map<int,int> myMap;
set<int> prim;
vector<int> verh, niz;

int main() {
   	for(int i = 2; i <= N; i++){
      	if(pr[i] == 0){
         	primes.push_back(i);
         	pr[i] = i;
      	}
      	for(int j = 0; j < primes.size() && i*primes[j] <= N && pr[i] >= primes[j]; j++)
         	pr[i*primes[j]] = primes[j];
   	}
   	int n, m; cin >> n >> m;
	for(int i = 0; i < n; i++){
		int a; cin >> a;
		while(a!=1){
			myMap[pr[a]]++;
			prim.insert(pr[a]);
			a /= pr[a];
		}
	}
	for(int i = 0; i < m; i++){
		int a; cin >> a;
		while(a!=1){
			myMap[pr[a]]--;
			prim.insert(pr[a]);
			a /= pr[a];
		}
	}
	for(auto i = prim.begin(); i != prim.end(); i++){
		if(myMap[*i] > 0) verh.push_back(*i);
		else if(myMap[*i] < 0) niz.push_back(*i);
	}
	cout << max(int(verh.size()),1) << ' ' << max(int(niz.size()),1) << endl;
	if(verh.size()) for(auto i : verh) cout << i << ' ';
	else cout << 1;
	cout << endl;
	if(niz.size()) for(auto i : niz) cout << i << ' ';
	else cout << 1;
	cout << endl;
   	return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

const int N = int(1e7)+1;
int pr[N];
vector<int> primes;
map<int,int> myMap;
set<int> prim;
vector<int> verh, niz;

int main() {
   	for(int i = 2; i <= N; i++){
      	if(pr[i] == 0){
         	primes.push_back(i);
         	pr[i] = i;
      	}
      	for(int j = 0; j < primes.size() && i*primes[j] <= N && pr[i] >= primes[j]; j++)
         	pr[i*primes[j]] = primes[j];
   	}
   	int n, m; cin >> n >> m;
	for(int i = 0; i < n; i++){
		int a; cin >> a;
		while(a!=1){
			myMap[pr[a]]++;
			prim.insert(pr[a]);
			a /= pr[a];
		}
	}
	for(int i = 0; i < m; i++){
		int a; cin >> a;
		while(a!=1){
			myMap[pr[a]]--;
			prim.insert(pr[a]);
			a /= pr[a];
		}
	}
	for(auto i = prim.begin(); i != prim.end(); i++){
		if(myMap[*i] > 0){
			int a = 1;
			for(int j = 0; j < myMap[*i]; j++)
				a*=(*i);
			verh.push_back(a);
		}else if(myMap[*i] < 0){
			int a = 1;
			for(int j = 0; j < abs(myMap[*i]); j++)
				a*=(*i);
			niz.push_back(a);
		}
	}
	cout << max(int(verh.size()),1) << ' ' << max(int(niz.size()),1) << endl;
	if(verh.size()) for(auto i : verh) cout << i << ' ';
	else cout << 1;
	cout << endl;
	if(niz.size()) for(auto i : niz) cout << i << ' ';
	else cout << 1;
	cout << endl;
   	return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;

const int N = int(1e7)+1;
int pr[N];
vector<int> primes;
map<int,int> myMap;
set<int> prim;
vector<int> verh, niz;

int main() {
   	for(int i = 2; i <= N; i++){
      	if(pr[i] == 0){
         	primes.push_back(i);
         	pr[i] = i;
      	}
      	for(int j = 0; j < primes.size() && i*primes[j] <= N && pr[i] >= primes[j]; j++)
         	pr[i*primes[j]] = primes[j];
   	}
   	int n, m; cin >> n >> m;
	for(int i = 0; i < n; i++){
		int a; cin >> a;
		while(a!=1){
			myMap[pr[a]]++;
			prim.insert(pr[a]);
			a /= pr[a];
		}
	}
	for(int i = 0; i < m; i++){
		int a; cin >> a;
		while(a!=1){
			myMap[pr[a]]--;
			prim.insert(pr[a]);
			a /= pr[a];
		}
	}
	for(auto i = prim.begin(); i != prim.end(); i++)
		if(myMap[*i] > 0){
			ull a = 1;
			while(myMap[*i]){
				a *= (*i);
				myMap[*i]--;
				if(a > int(1e6) || myMap[*i] == 0){
					if(a > int(1e7)){
						a /= (*i);
						myMap[*i]++;
					}
					verh.push_back(a);
					a = 1;
				}
			}
		}else if(myMap[*i] < 0){
			ull a = 1;
			while(myMap[*i]){
				a *= (*i);
				myMap[*i]++;
				if(a > int(1e6) || myMap[*i] == 0){
					if(a > int(1e7)){
						a /= (*i);
						myMap[*i]--;
					}
					niz.push_back(a);
					a = 1;
				}
			}
		}
	cout << max(int(verh.size()),1) << ' ' << max(int(niz.size()),1) << endl;
	if(verh.size()) for(auto i : verh) cout << i << ' ';
	else cout << 1;
	cout << endl;
	if(niz.size()) for(auto i : niz) cout << i << ' ';
	else cout << 1;
	cout << endl;
   	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;

const int N = int(1e7);
int pr[N + 1]; //the least prime divisor

//#define DEBUG

//план Б: если ассоциативные массивы работают слишком медленно
/*int nom[N + 1];
int denom[N + 1];

int new_nom[N + 1];
int new_denom[N + 1];*/

int a[int(1e5)];
int b[int(1e5)];

int main()
{
    //freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
    ios::sync_with_stdio(false);
    //линейное решето Эратосфена
    vector<int> primes;
    for(size_t i = 2; i <= N; i++){
        if(!pr[i]) pr[i] = i, primes.push_back(i);
        for(size_t j = 0; j < primes.size() && i * primes[j] <= N && pr[i] >= primes[j]; j++)
            pr[primes[j] * i] = pr[i];
    }

    int n, m; cin >> n >> m;
    //факторизация числителя
    map<int, int> nominator;
    for(int i = 0; i < n; i++){
        cin >> a[i]; int x = a[i];
        while(x != 1){
            nominator[pr[x]]++;
            x /= pr[x];
        }
    }
    //факторизация знаменателя
    map<int, int> denominator;
    for(int i = 0; i < m; i++){
        cin >> b[i]; int x = b[i];
        while(x != 1){
            denominator[pr[x]]++;
            x /= pr[x];
        }
    }
    //факторизация числителя после сокращения
    map<int, int> new_nominator;
    for(auto prime = nominator.begin(); prime != nominator.end(); prime++)
        new_nominator[prime->first] = prime->second - min(prime->second, denominator[prime->first]);
    //факторизация знаменателя после сокращения
    map<int, int> new_denominator;
    for(auto prime = denominator.begin(); prime != denominator.end(); prime++)
        new_denominator[prime->first] = prime->second - min(prime->second, nominator[prime->first]);

    #ifdef DEBUG
    auto print = [](map<int, int> some_map){
        for(auto it = some_map.begin(); it != some_map.end(); it++)
            cout << "(" << (it->first) << " " << (it->second) << ")" << endl;
        cout << endl;
    };

    cout << "NOMINATOR" << endl;
    print(nominator);

    cout << "DENOMINATOR" << endl;
    print(denominator);

    cout << "NEW NOMINATOR" << endl;
    print(new_nominator);

    cout << "NEW DENOMINATOR" << endl;
    print(new_denominator);
    #endif

    for(int i = 0; i < n; i++)
        while(nominator[pr[a[i]]] > new_nominator[pr[a[i]]]){
            if(a[i] == 1) break;
            nominator[pr[a[i]]]--;
            a[i] /= pr[a[i]];
            if(a[i] == 1) break;
        }

    for(int i = 0; i < m; i++){
        while(denominator[pr[b[i]]] != new_denominator[pr[b[i]]]){
            if(b[i] == 1) break;
            denominator[pr[b[i]]]--;
            b[i] /= pr[b[i]];
            if(b[i] == 1) break;
        }
    }

    cout << n << " " << m << endl;
    for(int i = 0; i < n; i++)
        cout << a[i] << " ";
    cout << endl;
    for(int i = 0; i < m; i++)
        cout << b[i] << " ";

    return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;

const int N = int(1e7)+1;
int pr[N];
vector<int> primes;
int nr[int(1e5)+1];
int dr[int(1e5)+1];
map <int,int> myMap;

int main() {
	ios::sync_with_stdio(false);
	//НАЧАЛО
   	for(int i = 2; i <= N; i++){
      	if(pr[i] == 0){
         	primes.push_back(i);
         	pr[i] = i;
      	}
      	for(int j = 0; j < primes.size() && i*primes[j] <= N && pr[i] >= primes[j]; j++)
         	pr[i*primes[j]] = primes[j];
   	}
   	//КОНЕЦ
   	int n, m; cin >> n >> m;
	for(int i = 1; i <= n; i++){
		int a; cin >> a;
		nr[i] = a;
		while(a!=1){
			myMap[pr[a]]++;
			a /= pr[a];
		}
	}
	for(int i = 1; i <= m; i++){
		int a; cin >> a;
		dr[i] = a;
		while(a!=1){
			myMap[pr[a]]--;
			a /= pr[a];
		}
	}
	cout << n << ' ' << m << endl;
	for(int i = 1; i <= n; i++){
		int answer = 1;
		int a = nr[i];
		while(a!=1){
			if(myMap[pr[a]] > 0){
				myMap[pr[a]]--;
				answer *= pr[a];
			}
			a /= pr[a];
		}
		cout << answer << ' ';
	}
	cout << endl;
	for(int i = 1; i <= m; i++){
		int answer = 1;
		int a = dr[i];
		while(a!=1){
			if(myMap[pr[a]] < 0){
				myMap[pr[a]]++;
				answer *= pr[a];
			}
			a /= pr[a];
		}
		cout << answer << ' ';
	}
   	return 0;
}


#include <iostream>
#include <conio.h>
using namespace std;

int getdigit(int x)
{
    return x%10;
}

int main()
{
    int n,k,i,tmp,x,res=0;
    cin >> n >> k;
    for(i=1;i<=n;i++)
        {
            cin >> x;
            tmp=0;
            while (x>0)
            {
                if (getdigit(x)!=4||getdigit(x)!=7)
                    tmp++;
                 x/=10;
            }
            if (tmp<=k)
                res++;
        }
    cout << res;
    return 0;

}


#include <iostream>
#include <conio.h>
using namespace std;

int getdigit(int x)
{
    return x%10;
}

int main()
{
    int n,k,i,tmp,x,res=0;
    cin >> n >> k;
    for(i=1;i<=n;i++)
        {
            cin >> x;
            tmp=0;
            while (x>0)
            {
                if (getdigit(x)==4||getdigit(x)==7)
                    tmp++;
                 x/=10;
            }
            if (tmp<=k)
                res++;
        }
    cout << res;
    return 0;

}


#include <iostream>
#include <conio.h>
using namespace std;

int getdigit(int x)
{
    return x%10;
}

int main()
{
    int n,k,i,tmp,x,res=0;
    cin >> n >> k;
    for(i=1;i<=n;i++)
        {
            cin >> x;
            tmp=0;
            while (x>0)
            {
                if (getdigit(x)==4||getdigit(x)==7)
                    tmp++;
                 x/=10;
            }
            if (tmp<=k)
                res++;
        }
    cout << res;
    getch();
    return 0;

}


#include<iostream>
#include<cstdio>
using namespace std;
int t,tedad,n,k;
int main(){
	string x;
	cin>>n>>k;
	for(int i=0;i<n;i++){
		t=0;
		cin>>x;
		for(int j=0;j<x.size();j++){
			if(x[i]=='7'|| x[i]=='4')
				t++;
		}
		if(t<=k){
			tedad++;
		}
	}
	cout<<tedad;
}


#include<iostream>
#include<cstdio>
using namespace std;
int t,tedad,n,k;
int main(){
	string x;
	cin>>n>>k;
	for(int i=0;i<n;i++){
		t=0;
		cin>>x;
		for(int j=0;j<x.size();j++){
			if(x[j]=='7'|| x[j]=='4')
				t++;
		}
		if(t<=k){
			tedad++;
		}
	}
	cout<<tedad;
}


#include<iostream>
#include<cstdio>
using namespace std;
int t,tedad,n,k;
int main(){
	string x;
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>x;
		for(int j=0;j<x.size();j++){
			if(x[i]=='7'|| x[i]=='4')
				t++;
		}
		if(t<=k)
			tedad++;
		t=0;
	}
	cout<<tedad;
}


#include<iostream>
#include<cstdio>
using namespace std;
int t,tedad,n,k;
int main(){
	string x;
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>x;
		for(int j=0;j<x.size();j++){
			if(x[i]=='7'|| x[i]=='4')
				t++;
		}
		if(t<=k){
			tedad++;
		}
		t=0;
	}
	cout<<tedad;
}


#include <iostream>
#include<math.h>
#include<bits/stdc++.h>
#include<algorithm>
#include<cstring>
using namespace std;

int main()
{
int n,m;
cin>>n>>m;
int c=0;

for (int i=0;i<n;i++){
    string x;
    cin>>x;
    int l=0;
    for (int j=0;j<x.size();j++){
        if (x[j]=='7' || x[j]== '4'){
            l++;
        }
    }
    if (l<=m){
        c++;
    }
}

  cout<<c<<endl;
  return 0;
}


#include <iostream>
#include<math.h>
#include<bits/stdc++.h>
#include<algorithm>
#include<cstring>
using namespace std;

int main()
{
int n,m;
cin>>n>>m;
int c=0;

for (int i=0;i<n;i++){
    string x;
    cin>>x;
    int l=0;
    for (int j=0;j<x.size();j++){
        if (x[i]=='7' || x[i]== '4'){
            l++;
        }
    }
    if (l<=m){
        c++;
    }
}

  cout<<c<<endl;
  return 0;
}


#include <iostream>
#include<math.h>
#include<bits/stdc++.h>
#include<algorithm>
#include<cstring>
using namespace std;
int power(int x){
int c=1;
for (int i=0;i<x;i++){
    c*=10;
}
return c;
}

int main()
{
  int n,x,m,c=0;
  cin>>n>>x;
  int v=power(x);

  for (int i=0;i<n;i++){
    cin>>m;
    if (m<v){
        c++;
    }
  }
  cout<<c<<endl;
  return 0;
}


#include <iostream>
#include<math.h>
#include<bits/stdc++.h>
#include<algorithm>
#include<cstring>
using namespace std;
int power(int x){
int c=1;
for (int i=0;i<x;i++){
    c*10;
}

}

int main()
{
  int n,x,m,c=0;
  cin>>n>>x;
  int v=power(x);
  for (int i=0;i<n;i++){
    cin>>m;
    if (m<v){
        c++;
    }
  }
  cout<<c<<endl;
  return 0;
}


#include <iostream>
#include<math.h>
#include<bits/stdc++.h>
#include<algorithm>
#include<cstring>
using namespace std;
int power(int x){
int c=1;
for (int i=0;i<x;i++){
    c*10;
}
return c;
}

int main()
{
  int n,x,m,c=0;
  cin>>n>>x;
  int v=power(x);

  for (int i=0;i<n;i++){
    cin>>m;
    if (m<v){
        c++;
    }
  }
  cout<<c<<endl;
  return 0;
}


 #include <iostream>
#include<math.h>
#include<bits/stdc++.h>
#include<algorithm>
#include<cstring>
using namespace std;
long long  power(int x){
long long  c=1;
for (int i=0;i<x;i++){
    c*=10;
}
return c;
}

int main()
{
  long long  n,x,m,c=0;
  cin>>n>>x;
  long long  v=power(x);

  for (int i=0;i<n;i++){
    cin>>m;
    if (m<v){
        c++;
    }
  }
  cout<<c<<endl;
  return 0;
}


#include <iostream>
#include<math.h>
#include<bits/stdc++.h>
#include<algorithm>
#include<cstring>
using namespace std;
int power(int x){
int c=1;
for (int i=0;i<x;i++){
    c*=10;
}
return c;
}

int main()
{
  int n,x,m,c=0;
  cin>>n>>x;
  int v=power(x);
cout<<v;
  for (int i=0;i<n;i++){
    cin>>m;
    if (m<v){
        c++;
    }
  }
  cout<<c<<endl;
  return 0;
}


#include <iostream>
#include<math.h>
#include<bits/stdc++.h>
#include<algorithm>
#include<cstring>
using namespace std;
long long  power(int x){
long long  c=1;
for (int i=0;i<x;i++){
    c*=10;
}
return c;
}

int main()
{
  long long  n,x,m,c=0;
  cin>>n>>x;
  long long  v=power(x);
cout<<v;
  for (int i=0;i<n;i++){
    cin>>m;
    if (m<v){
        c++;
    }
  }
  cout<<c<<endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,i,cnt=0,d=0;
    string s;
    cin>>a>>b;
    for( i=1; i<=a; i++ ){
        cin>>s;
        for( int j=0; j<s.size(); j++ ){
            if( s[j]=='4' || s[j]=='7' ){
                d++;
            }
        }
        if( d<=b ){
            cnt++;
        }
        d=0;
    }
    cout<<cnt;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,i,cnt=0;
    string s;
    cin>>a>>b;
    for( i=1; i<=a; i++ ){
        cin>>s;
        if( s.size()<=b ){
            cnt++;
        }
    }
    cout<<cnt;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,i,cnt=0,d=0,f=0;
    string s;
    cin>>a>>b;
    for( i=1; i<=a; i++ ){
        cin>>s;
        for( int j=0; j<s.size(); j++ ){
            if( s[i]=='4' ){
                d++;
            }
            if( s[i]=='7' ){
                f++;
            }
        }
        if( d<=b || f<=b ){
            cnt++;
        }
        f=0;
        d=0;
    }
    cout<<cnt;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,i,cnt=0,d=0;
    string s;
    cin>>a>>b;
    for( i=1; i<=a; i++ ){
        cin>>s;
        for( int j=0; j<s.size(); j++ ){
            if( s[i]=='4' || s[i]=='7' ){
                d++;
            }
        }
        if( d<=b ){
            cnt++;
        }
        d=0;
    }
    cout<<cnt;
    return 0;
}


#include<iostream>
using namespace std;
int main(){
     int n,k,ans;
     char s[20];
     while(cin>>n>>k)
     {
         ans=n;
         k++;
         while(n--)
         {
             cin>>s;
             int j,i=0;
             for(j=k;s[i];i++)
             if((s[i]=='4')||(s[i]=='7'))
             j--;
             if(!j) ans--;
         }
         cout<<ans<<endl;
     }
}


#include<iostream>
using namespace std;
int main(){
     int n,k,ans;
     char s[20];
     while(cin>>n>>k)
     {
         ans=n;
         k++;
         while(n--)
         {
             cin>>s;
             int j,i=0;
             for(j=k;s[i];i++)
             if(!j)
             {
                 ans--;
                 break;
             }
             else
             if((s[i]=='4')||(s[i]=='7'))
             j--;
         }
         cout<<ans<<endl;
     }
}


#include<iostream>
using namespace std;
int main(){
     int n,k,ans;
     char s[20];
     while(cin>>n>>k)
     {
         ans=n;
         k++;
         while(n--)
         {
             cin>>s;
             int j,i=0;
             for(j=k;s[i];i++)
             if(!j)
             {
                 ans--;
                 break;
             }
             else
             if((s[i]=='4')||(s[i]=='7'))
             j--;
         }
         cout<<ans<<endl;
     }

}


#include<iostream>
using namespace std;
int main(){
     int n,k,ans;
     char s[20];
     while(cin>>n>>k)
     {
         ans=n;
         while(n--)
         {
             cin>>s;
             int j,i=0;
             for(j=k;s[i];i++)
             if((s[i]=='4')||(s[i]=='7'))
             j--;
             if(j<0) ans--;
         }
         cout<<ans<<endl;
     }
}


#include<iostream>
using namespace std;
int main(){
     int n,k,ans;
     char s[20];
     while(cin>>n>>k)
     {
         ans=n;
         while(n--)
         {
             cin>>s;
             int j,i=0;
             for(j=k;s[i];i++)
             if(!j)
             {
                 ans--;
                 break;
             }
             else
             if((s[i]=='4')||(s[i]=='7'))
             j--;
         }
         cout<<ans<<endl;
     }

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int i,j,n,k,b=0;
    string c;
    cin>>n>>k;
    for(i=1;i<=n;i++){
        cin>>c;
        int tt=0;
        for(j=0;j<=c.size();j++){
            if(c[j]=='4'||c[j]=='7') tt++;
        }
        if(tt<=k) b++;
    }
    cout<<b<<endl;
    return 0;
}


#include <iostream>

using namespace std;
int test(int n)
{
    int t,a;
    while(n!=0)
    {
        t=n%10;
        if(t==4 || t==7)
            a++;
        n=n/10;
    }
    return a;
}

int main()
{
    int n,k;
    cin>>n>>k;
    int a,i=0;
    while(n--)
    {
        cin>>a;
        if(test(a)<=k)
            i++;
    }
    cout<<i;
    return 0;
}


#include <iostream>

using namespace std;
int test(int n)
{
    int t,a;
    while(n!=0)
    {
        t=n%10;
        if(t==4 || t==7)
            a++;
        n=n/10;
    }
    return a;
}

int main()
{
    int  n,k;
    cin>>n>>k;
    int a,i=0;
    while(n--)
    {
        cin>>a;
        if(test(a)<=k)
            i++;
    }
    cout<<i;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int i,j,n,k,b=0;
    string c;
    cin>>n>>k;
    for(i=1;i<=n;i++){
        cin>>c;
        int tt=0;
        for(j=0;j<=c.size();j++){
            if(c[j]=='4'||c[j]=='7') tt++;
        }
        if(tt<=k) ans++;
    }
    cout<<ans<<endl;
}


#include <iostream>

using namespace std;
int test(int n)
{
    int t,a;
    while(n!=0)
    {
        t=n%10;
        if(t==4 || t==7)
            a++;
        n=n/10;
    }
    return a;
}

int main()
{
    long n,k;
    cin>>n>>k;
    int a,i=0;
    while(n--)
    {
        cin>>a;
        if(test(a)<=k)
            i++;
    }
    cout<<i;
    return 0;
}


#include <iostream>

using namespace std;
int test(int n)
{
    int t,a;
    while(n!=0)
    {
        t=n%10;
        if(t==4 || t==7)
            a++;
        n=n/10;
    }
    return a;
}

int main()
{
    long  n,k;
    cin>>n>>k;
    int a,i=0;
    while(n--)
    {
        cin>>a;
        if(test(a)<=k)
            i++;
    }
    cout<<i;
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main(){
    char numero[13];
    int i,j,qtidade,maximo,contador=0,resultado=0;
    scanf("%d%d",&qtidade,&maximo);
    for(i=0;i<qtidade;i++){
        contador=0;
        fflush(stdin);
        gets(numero);

        for(j=0;j<13;j++){
            if(numero[j]=='4'||numero[j]=='7'){
                contador++;

            }

        }
        if(contador<=maximo)resultado++;

        for(j=0;j<13;j++){
            numero[j]=0;
        }
    }
    printf("%d\n",resultado);

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main(){
    char numero[11];
    int i,j,qtidade,maximo,contador,resultado=0;
    scanf("%d%d",&qtidade,&maximo);
    for(i=0;i<qtidade;i++){
        contador=0;
        fflush(stdin);
        gets(numero);

        for(j=0;j<11;j++){
            if(numero[j]=='4'||numero[j]=='7'){
                contador++;

            }

        }
        if(contador<=maximo)resultado++;

        for(j=0;j<11;j++){
            numero[j]=0;
        }
    }
    printf("%d",resultado);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main(){
    while(1){
    char numero[12];
    int i,j,qtidade,maximo,contador=0,resultado=0;
    scanf("%d%d",&qtidade,&maximo);
    for(i=0;i<qtidade;i++){
        contador=0;
        fflush(stdin);
        gets(numero);

        for(j=0;j<12;j++){
            if(numero[j]=='4'||numero[j]=='7'){
                contador++;

            }

        }
        if(contador<=maximo)resultado++;

        for(j=0;j<12;j++){
            numero[j]=0;
        }
    }
    printf("%d\n",resultado);
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main(){
    char numero[13];
    int i,j,qtidade,maximo,contador=0,resultado=0;
    scanf("%d%d",&qtidade,&maximo);
    for(i=0;i<qtidade;i++){
        contador=0;
        scanf("%s",numero);

        for(j=0;j<13;j++){
            if(numero[j]=='4'||numero[j]=='7'){
                contador++;

            }

        }
        if(contador<=maximo)resultado++;

        for(j=0;j<13;j++){
            numero[j]=0;
        }
    }
    printf("%d\n",resultado);

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main(){
    char numero[12];
    int i,j,qtidade,maximo,contador=0,resultado=0;
    scanf("%d%d",&qtidade,&maximo);
    for(i=0;i<qtidade;i++){
        contador=0;
        fflush(stdin);
        gets(numero);

        for(j=0;j<12;j++){
            if(numero[j]=='4'||numero[j]=='7'){
                contador++;

            }

        }
        if(contador<=maximo)resultado++;

        for(j=0;j<12;j++){
            numero[j]=0;
        }
    }
    printf("%d\n",resultado);

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main(){
    char numero[12];
    int i,j,qtidade,maximo,contador,resultado=0;
    scanf("%d%d",&qtidade,&maximo);
    for(i=0;i<qtidade;i++){
        contador=0;
        fflush(stdin);
        gets(numero);

        for(j=0;j<12;j++){
            if(numero[j]=='4'||numero[j]=='7'){
                contador++;

            }

        }
        if(contador<=maximo)resultado++;

        for(j=0;j<12;j++){
            numero[j]=0;
        }
    }
    printf("%d",resultado);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main(){
    char numero[13];
    int i,j,qtidade,maximo,contador=0,resultado=0;
    scanf("%d%d",&qtidade,&maximo);
    for(i=0;i<qtidade;i++){
        contador=0;
        gets(numero);

        for(j=0;j<13;j++){
            if(numero[j]=='4'||numero[j]=='7'){
                contador++;

            }

        }
        if(contador<=maximo)resultado++;

        for(j=0;j<13;j++){
            numero[j]=0;
        }
    }
    printf("%d\n",resultado);

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 100005
#define INF 1010580540
#define C 105
using namespace std;
int f[N],pre[N],c[C],x[N],y[N],z[N],map[C][C],F[C];
int sum,n,m,p,i,j,k,now,u,v;
int get(int u){return f[u]==u?u:f[u]=get(f[u]);}
inline void Union(int x,int y)
{
  int u=get(x),v=get(y);
  f[v]=u;
}
int main()
{
  scanf("%d%d%d",&n,&m,&p);
  for (i=1;i<=p;i++) scanf("%d",&c[i]);
  for (i=1;i<=n;i++)
  {
    if (sum==0) sum=c[++now];
    f[i]=i;pre[i]=now;sum--;
  }
  for (i=1;i<=m;i++)
  {
    scanf("%d%d%d",&x[i],&y[i],&z[i]);
    if (pre[x[i]]==pre[y[i]]&&!z[i]) Union(x[i],y[i]);
  }
  for (i=1;i<=n;i++)
  {
    f[i]=get(f[i]);
    if (!F[pre[i]]) F[pre[i]]=f[i];
    if (f[i]!=F[pre[i]]) {puts("No");return 0;}
  }
  memset(map,60,sizeof(map));
  for (i=1;i<=m;i++)
    if ((u=pre[x[i]])!=(v=pre[y[i]])) map[u][v]=map[v][u]=min(map[v][u],z[i]);
  for (k=1;k<=p;k++)
    for (i=1;i<=p;i++)
      for (j=1;j<=p;j++)
        if (i!=j&&i!=k&&j!=k)
          if (map[i][k]<INF&&map[k][j]<INF)
            map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
  puts("Yes");
  for (i=1;i<=p;i++)
  {
    for (j=1;j<=p;j++)
      printf("%d ",(i==j)?0:((map[i][j]==INF)?-1:map[i][j]));
    puts("");
  }
  return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 100005
#define INF 1010580540
#define C 505
using namespace std;
int f[N],pre[N],c[C],x[N],y[N],z[N],map[C][C],F[C];
int sum,n,m,p,i,j,k,now,u,v;
int get(int u){return f[u]==u?u:f[u]=get(f[u]);}
inline void Union(int x,int y)
{
  int u=get(x),v=get(y);
  f[v]=u;
}
int main()
{
  scanf("%d%d%d",&n,&m,&p);
  for (i=1;i<=p;i++) scanf("%d",&c[i]);
  for (i=1;i<=n;i++)
  {
    if (sum==0) sum=c[++now];
    f[i]=i;pre[i]=now;sum--;
  }
  for (i=1;i<=m;i++)
  {
    scanf("%d%d%d",&x[i],&y[i],&z[i]);
    if (pre[x[i]]==pre[y[i]]&&!z[i]) Union(x[i],y[i]);
  }
  for (i=1;i<=n;i++)
  {
    f[i]=get(f[i]);
    if (!F[pre[i]]) F[pre[i]]=f[i];
    if (f[i]!=F[pre[i]]) {puts("No");return 0;}
  }
  memset(map,60,sizeof(map));
  for (i=1;i<=m;i++)
    if ((u=pre[x[i]])!=(v=pre[y[i]])) map[u][v]=map[v][u]=min(map[v][u],z[i]);
  for (k=1;k<=p;k++)
    for (i=1;i<=p;i++)
      for (j=1;j<=p;j++)
        if (i!=j&&i!=k&&j!=k)
          if (map[i][k]<INF&&map[k][j]<INF)
            map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
  puts("Yes");
  for (i=1;i<=p;i++)
  {
    for (j=1;j<=p;j++)
      printf("%d ",(i==j)?0:((map[i][j]==INF)?-1:map[i][j]));
    puts("");
  }
  return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 100005
#define INF 1010580540
#define C 505
using namespace std;
int f[N],pre[N],c[C],x[N],y[N],z[N],map[C][C],F[C];
int sum,n,m,p,i,j,k,now,u,v;
int get(int u){return f[u]==u?u:f[u]=get(f[u]);}
inline void Union(int x,int y)
{
  int u=get(x),v=get(y);
  f[v]=u;
}
int main()
{
  scanf("%d%d%d",&n,&m,&p);
  for (i=1;i<=p;i++) scanf("%d",&c[i]);
  for (i=1;i<=n;i++)
  {
    if (sum==0) sum=c[++now];
    f[i]=i;pre[i]=now;sum--;
  }
  for (i=1;i<=m;i++)
  {
    scanf("%d%d%d",&x[i],&y[i],&z[i]);
    if (!z[i]) Union(x[i],y[i]);
  }
  for (i=1;i<=n;i++)
  {
    f[i]=get(f[i]);
    if (!F[pre[i]]) F[pre[i]]=f[i];
    if (f[i]!=F[pre[i]]) {puts("No");return 0;}
  }
  memset(map,60,sizeof(map));
  for (i=1;i<=m;i++)
    if ((u=pre[x[i]])!=(v=pre[y[i]])) map[u][v]=map[v][u]=min(map[v][u],z[i]);
  for (k=1;k<=p;k++)
    for (i=1;i<=p;i++)
      for (j=1;j<=p;j++)
        if (i!=j&&i!=k&&j!=k)
          if (map[i][k]<INF&&map[k][j]<INF)
            map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
  puts("Yes");
  for (i=1;i<=p;i++)
  {
    for (j=1;j<=p;j++)
      printf("%d ",(i==j)?0:((map[i][j]==INF)?-1:map[i][j]));
    puts("");
  }
  return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 100005
#define C 105
using namespace std;
int f[N],pre[N],c[C],x[N],y[N],z[N],map[C][C],F[C];
int sum,n,m,p,i,j,k,now,u,v;
int get(int u){return f[u]==u?u:f[u]=get(f[u]);}
inline void Union(int x,int y)
{
  int u=get(x),v=get(y);
  f[v]=u;
}
int main()
{
  scanf("%d%d%d",&n,&m,&p);
  for (i=1;i<=p;i++) scanf("%d",&c[i]);
  for (i=1;i<=n;i++)
  {
    if (sum==0) sum=c[++now];
    f[i]=i;pre[i]=now;sum--;
  }
  for (i=1;i<=m;i++)
  {
    scanf("%d%d%d",&x[i],&y[i],&z[i]);
    if (pre[x[i]]==pre[y[i]]&&!z[i]) Union(x[i],y[i]);
  }
  for (i=1;i<=n;i++)
  {
    f[i]=get(f[i]);
    if (!F[pre[i]]) F[pre[i]]=f[i];
    if (f[i]!=F[pre[i]]) {puts("No");return 0;}
  }
  memset(map,60,sizeof(map));
  for (i=1;i<=m;i++)
    if ((u=pre[x[i]])!=(v=pre[y[i]])) map[u][v]=map[v][u]=min(map[v][u],z[i]);
  for (k=1;k<=p;k++)
    for (i=1;i<=p;i++)
      for (j=1;j<=p;j++)
        if (i!=j&&i!=k&&j!=k)
          map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
  puts("Yes");
  for (i=1;i<=p;i++)
  {
    for (j=1;j<=p;j++)
      printf("%d ",(i==j)?0:((map[i][j]==1010580540)?-1:map[i][j]));
    puts("");
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cmath>
#include <map>
#include <set>

using namespace std;
int n, m, k, c[600], d[600], g[600][600];
vector <vector <int> > gg (150010);
bool used[150010];
int find(int a){
    int l = 0;
    int r = k;
    while (true){
        if (a > d[(l+r+1)/2]) l = (l + r + 1)/2;
        else if (a <= d[(l+r+1)/2-1]) r = (l + r + 1)/2;
        else break;
    }
    return (l+r+1)/2;
}
void dfs(int v){
    used[v] = true;
    for (int i = 0; i < gg[v].size(); i++)
        if (used[gg[v][i]] == false)
            dfs(gg[v][i]);
}

int main(){
    cin >> n >> m >> k;
    d[0] = 0;
    for (int i = 1; i <= k; i++){
        cin >> c[i];
        d[i] = d[i-1] + c[i];
    }

    for (int i = 0; i < 510; i++)
    for (int j = 0; j < 510; j++){
        if (i == j) g[i][j] = 0;
        else g[i][j] = 1000000000;
    }

        for (int i = 0; i < m; i++){
            int u, v, x;
            cin >> u >> v >> x;
            int uu = find(u);
            int vv = find(v);

            if (uu == vv && x == 0) {
                gg[u].push_back(v);
                gg[v].push_back(u);
            }

            if (uu != vv) {
                if (g[uu][vv] == 1000000000){
                    g[uu][vv] = x;
                    g[vv][uu] = x;
                    }
                    else {
                    g[uu][vv] = min(x, g[uu][vv]);
                    g[vv][uu] = min(x, g[vv][uu]);
                    }

            }
        }
        for (int i = 0; i < k; i++)
            dfs(d[i] + 1);

        for (int i = 1; i <= n; i++)
            if (used[i] == false) {cout <<"No"; return 0;}

        cout << "Yes" << endl;

    for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++)
        for (int l = 1; l <= k; l++)
                g[i][j] = min(g[i][j], g[i][l] + g[l][j]);
        for (int i = 1; i <= k; i++){
            for (int j = 1; j <= k; j++) {
                if (g[i][j] != 1000000000)
                    cout << g[i][j] << " ";
                else cout << -1 << " " ;
                }
            cout << endl;
        }
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cmath>
#include <map>
#include <set>

using namespace std;
int n, m, k, c[510], d[510], g[510][510];
vector <vector <int> > gg (100010);
bool used[100010];
int find(int a){
    int l = 0;
    int r = k;
    while (true){
        if (a > d[(l+r+1)/2]) l = (l + r + 1)/2;
        else if (a <= d[(l+r+1)/2-1]) r = (l + r + 1)/2;
        else break;
    }
    return (l+r+1)/2;
}
void dfs(int v){
    used[v] = true;
    for (int i = 0; i < gg[v].size(); i++)
        if (used[gg[v][i]] == false)
            dfs(gg[v][i]);
}

int main(){
    cin >> n >> m >> k;
    d[0] = 0;
    for (int i = 1; i <= k; i++){
        cin >> c[i];
        d[i] = d[i-1] + c[i];
    }

    for (int i = 0; i < 501; i++)
    for (int j = 0; j < 501; j++){
        if (i == j) g[i][j] = 0;
        else g[i][j] = -1;
    }

        for (int i = 0; i < m; i++){
            int u, v, x;
            cin >> u >> v >> x;
            int uu = find(u);
            int vv = find(v);
            if (uu == vv && x == 0) {gg[u].push_back(v); gg[v].push_back(u);}

            else { g[uu][vv] = x; g[vv][uu] = x;}
        }
        for (int i = 0; i < k; i++)
            dfs(d[i] + 1);
        for (int i = 1; i <= n; i++)
            if(used[i] == false) {cout <<"No"; return 0;}
        cout << "Yes" << endl;
        for (int i = 1; i <= k; i++){
            for (int j = 1; j <= k; j++)
                cout << g[i][j] << " ";
            cout << endl;
        }

    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cmath>
#include <map>
#include <set>

using namespace std;
int n, m, k, c[600], d[600], g[600][600];
vector <vector <int> > gg (150010);
bool used[150010];
int find(int a){
    int l = 0;
    int r = k;
    while (true){
        if (a > d[(l+r+1)/2]) l = (l + r + 1)/2;
        else if (a <= d[(l+r+1)/2-1]) r = (l + r + 1)/2;
        else break;
    }
    return (l+r+1)/2;
}
void dfs(int v){
    used[v] = true;
    for (int i = 0; i < gg[v].size(); i++)
        if (used[gg[v][i]] == false)
            dfs(gg[v][i]);
}

int main(){
    cin >> n >> m >> k;
    d[0] = 0;
    for (int i = 1; i <= k; i++){
        cin >> c[i];
        d[i] = d[i-1] + c[i];
    }

    for (int i = 0; i < 510; i++)
    for (int j = 0; j < 510; j++){
        if (i == j) g[i][j] = 0;
        else g[i][j] = 1000000000;
    }

        for (int i = 0; i < m; i++){
            int u, v, x;
            cin >> u >> v >> x;
            int uu = find(u);
            int vv = find(v);

            if (uu == vv && x == 0) {
                gg[u].push_back(v);
                gg[v].push_back(u);
            }

            if (uu != vv) {
                if (g[uu][vv] == -1){
                    g[uu][vv] = x;
                    g[vv][uu] = x;
                    }
                    else {
                    g[uu][vv] = min(x, g[uu][vv]);
                    g[vv][uu] = min(x, g[vv][uu]);
                    }

            }
        }
        for (int i = 0; i < k; i++)
            dfs(d[i] + 1);

        for (int i = 1; i <= n; i++)
            if (used[i] == false) {cout <<"No"; return 0;}

        cout << "Yes" << endl;

    for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++)
        for (int l = 1; l <= k; l++)
                g[i][j] = min(g[i][j], g[i][l] + g[l][j]);
        for (int i = 1; i <= k; i++){
            for (int j = 1; j <= k; j++) {
                if (g[i][j] != 1000000000)
                    cout << g[i][j] << " ";
                else cout << -1 << " " ;
                }
            cout << endl;
        }
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cmath>
#include <map>
#include <set>

using namespace std;
int n, m, k, c[600], d[600], g[600][600];
vector <vector <int> > gg (150010);
bool used[150010];
int find(int a){
    int l = 0;
    int r = k;
    while (true){
        if (a > d[(l+r+1)/2]) l = (l + r + 1)/2;
        else if (a <= d[(l+r+1)/2-1]) r = (l + r + 1)/2;
        else break;
    }
    return (l+r+1)/2;
}
void dfs(int v){
    used[v] = true;
    for (int i = 0; i < gg[v].size(); i++)
        if (used[gg[v][i]] == false)
            dfs(gg[v][i]);
}

int main(){
    cin >> n >> m >> k;
    d[0] = 0;
    for (int i = 1; i <= k; i++){
        cin >> c[i];
        d[i] = d[i-1] + c[i];
    }

    for (int i = 0; i < 510; i++)
    for (int j = 0; j < 510; j++){
        if (i == j) g[i][j] = 0;
        else g[i][j] = 1000000000;
    }

        for (int i = 0; i < m; i++){
            int u, v, x;
            cin >> u >> v >> x;
            int uu = find(u);
            int vv = find(v);

            if (x == 0) {
                gg[u].push_back(v);
                gg[v].push_back(u);
            }

            if (uu != vv) {
            g[uu][vv] = min(x, g[uu][vv]);
                g[vv][uu] = min(x, g[vv][uu]);
                }
        }
        for (int i = 0; i < k; i++)
            dfs(d[i] + 1);

        for (int i = 1; i <= n; i++)
            if (used[i] == false) {cout <<"No"; return 0;}

        cout << "Yes" << endl;

    for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++)
        for (int l = 1; l <= k; l++)
                g[j][l] = min(g[j][l], g[j][i] + g[i][l]);

        for (int i = 1; i <= k; i++){
            for (int j = 1; j <= k; j++) {
                if (g[i][j] != 1000000000)
                    cout << g[i][j] << " ";
                else cout << -1 << " " ;
                }
            cout << endl;
        }
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cmath>
#include <map>
#include <set>

using namespace std;
int n, m, k, c[510], d[510], g[510][510];
vector <vector <int> > gg (100010);
bool used[100010];
int find(int a){
    int l = 0;
    int r = k;
    while (true){
        if (a > d[(l+r+1)/2]) l = (l + r + 1)/2;
        else if (a <= d[(l+r+1)/2-1]) r = (l + r + 1)/2;
        else break;
    }
    return (l+r+1)/2;
}
void dfs(int v){
    used[v] = true;
    for (int i = 0; i < gg[v].size(); i++)
        if (used[gg[v][i]] == false)
            dfs(gg[v][i]);
}

int main(){
    cin >> n >> m >> k;
    d[0] = 0;
    for (int i = 1; i <= k; i++){
        cin >> c[i];
        d[i] = d[i-1] + c[i];
    }

    for (int i = 0; i < 501; i++)
    for (int j = 0; j < 501; j++){
        if (i == j) g[i][j] = 0;
        else g[i][j] = -1;
    }

        for (int i = 0; i < m; i++){
            int u, v, x;
            cin >> u >> v >> x;
            int uu = find(u);
            int vv = find(v);
            if (uu == vv && x == 0) {gg[u].push_back(v); gg[v].push_back(u);}

            else if (uu != vv) { g[uu][vv] = x; g[vv][uu] = x;}
        }
        for (int i = 0; i < k; i++)
            dfs(d[i] + 1);
        for (int i = 1; i <= n; i++)
            if(used[i] == false) {cout <<"No"; return 0;}
        cout << "Yes" << endl;
        for (int i = 1; i <= k; i++){
            for (int j = 1; j <= k; j++)
                cout << g[i][j] << " ";
            cout << endl;
        }

    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cmath>
#include <map>
#include <set>

using namespace std;
int n, m, k, c[600], d[600], g[600][600];
vector <vector <int> > gg (150010);
bool used[150010];
int find(int a){
    int l = 0;
    int r = k;
    while (true){
        if (a > d[(l+r+1)/2]) l = (l + r + 1)/2;
        else if (a <= d[(l+r+1)/2-1]) r = (l + r + 1)/2;
        else break;
    }
    return (l+r+1)/2;
}
void dfs(int v){
    used[v] = true;
    for (int i = 0; i < gg[v].size(); i++)
        if (used[gg[v][i]] == false)
            dfs(gg[v][i]);
}

int main(){
    cin >> n >> m >> k;
    d[0] = 0;
    for (int i = 1; i <= k; i++){
        cin >> c[i];
        d[i] = d[i-1] + c[i];
    }

    for (int i = 0; i < 510; i++)
    for (int j = 0; j < 510; j++){
        if (i == j) g[i][j] = 0;
        else g[i][j] = -1;
    }

        for (int i = 0; i < m; i++){
            int u, v, x;
            cin >> u >> v >> x;
            int uu = find(u);
            int vv = find(v);

            if (uu == vv && x == 0) {
                gg[u].push_back(v);
                gg[v].push_back(u);
            }

            if (uu != vv) {
                if (g[uu][vv] == -1){
                    g[uu][vv] = x;
                    g[vv][uu] = x;
                    }
                    else {
                    g[uu][vv] = min(x, g[uu][vv]);
                    g[vv][uu] = min(x, g[vv][uu]);
                    }

            }
        }
        for (int i = 0; i < k; i++)
            dfs(d[i] + 1);

        for (int i = 1; i <= n; i++)
            if (used[i] == false) {cout <<"No"; return 0;}

        cout << "Yes" << endl;
    for (int i=1; i<=k; i++)
        for (int j=1; j<=k; j++)
            for (int l=1; l<=k; l++){
                if (g[i][l] > -1 && g[l][j] > -1){
                    if (g[i][j] > -1)
                        g[i][j] = min (g[i][j], g[i][l] + g[l][j]);
                    else g[i][j] = g[i][l] + g[l][j];
                }
                }
        for (int i = 1; i <= k; i++){
            for (int j = 1; j <= k; j++)
                cout << g[i][j] << " ";
            cout << endl;
        }

    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cmath>
#include <map>
#include <set>

using namespace std;
int n, m, k, c[600], d[600], g[600][600];
vector <vector <int> > gg (150010);
bool used[150010];
int find(int a){
    int l = 0;
    int r = k;
    while (true){
        if (a > d[(l+r+1)/2]) l = (l + r + 1)/2;
        else if (a <= d[(l+r+1)/2-1]) r = (l + r + 1)/2;
        else break;
    }
    return (l+r+1)/2;
}
void dfs(int v){
    used[v] = true;
    for (int i = 0; i < gg[v].size(); i++)
        if (used[gg[v][i]] == false)
            dfs(gg[v][i]);
}

int main(){
    cin >> n >> m >> k;
    d[0] = 0;
    for (int i = 1; i <= k; i++){
        cin >> c[i];
        d[i] = d[i-1] + c[i];
    }

    for (int i = 0; i < 510; i++)
    for (int j = 0; j < 510; j++){
        if (i == j) g[i][j] = 0;
        else g[i][j] = -1;
    }

        for (int i = 0; i < m; i++){
            int u, v, x;
            cin >> u >> v >> x;
            int uu = find(u);
            int vv = find(v);

            if (uu == vv && x == 0) {
                gg[u].push_back(v);
                gg[v].push_back(u);
            }

            if (uu != vv) {
                if (g[uu][vv] == -1){
                    g[uu][vv] = x;
                    g[vv][uu] = x;
                    }
                    else {
                    g[uu][vv] = min(x, g[uu][vv]);
                    g[vv][uu] = min(x, g[vv][uu]);
                    }

            }
        }
        for (int i = 0; i < k; i++)
            dfs(d[i] + 1);

        for (int i = 1; i <= n; i++)
            if (used[i] == false) {cout <<"No"; return 0;}

        cout << "Yes" << endl;

    for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++)
        for (int l = 1; l <= k; l++){
            if (i != j && g[i][l] > -1 && g[l][j] > -1){
                if (g[i][j] > -1)
                    g[i][j] = min(g[i][j], g[i][l] + g[l][j]);
                else g[i][j] = g[i][l] + g[l][j];
            }
        }
        for (int i = 1; i <= k; i++){
            for (int j = 1; j <= k; j++)
                cout << g[i][j] << " ";
            cout << endl;
        }

    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cmath>
#include <map>
#include <set>

using namespace std;
int n, m, k, c[600], d[600], g[600][600];
vector <vector <int> > gg (150010);
bool used[150010];
int find(int a){
    int l = 0;
    int r = k;
    while (true){
        if (a > d[(l+r+1)/2]) l = (l + r + 1)/2;
        else if (a <= d[(l+r+1)/2-1]) r = (l + r + 1)/2;
        else break;
    }
    return (l+r+1)/2;
}
void dfs(int v){
    used[v] = true;
    for (int i = 0; i < gg[v].size(); i++)
        if (used[gg[v][i]] == false)
            dfs(gg[v][i]);
}

int main(){
    cin >> n >> m >> k;
    d[0] = 0;
    for (int i = 1; i <= k; i++){
        cin >> c[i];
        d[i] = d[i-1] + c[i];
    }

    for (int i = 0; i < 510; i++)
    for (int j = 0; j < 510; j++){
        if (i == j) g[i][j] = 0;
        else g[i][j] = 1000000000;
    }

        for (int i = 0; i < m; i++){
            int u, v, x;
            cin >> u >> v >> x;
            int uu = find(u);
            int vv = find(v);

            if (uu == vv && x == 0) {
                gg[u].push_back(v);
                gg[v].push_back(u);
            }

            if (uu != vv) {
            g[uu][vv] = min(x, g[uu][vv]);
                g[vv][uu] = min(x, g[vv][uu]);
                }
        }
        for (int i = 0; i < k; i++)
            dfs(d[i] + 1);

        for (int i = 1; i <= n; i++)
            if (used[i] == false) {cout <<"No"; return 0;}

        cout << "Yes" << endl;

    for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++)
        for (int l = 1; l <= k; l++)
                g[j][l] = min(g[j][l], g[j][i] + g[i][l]);

        for (int i = 1; i <= k; i++){
            for (int j = 1; j <= k; j++) {
                if (g[i][j] != 1000000000)
                    cout << g[i][j] << " ";
                else cout << -1 << " " ;
                }
            cout << endl;
        }
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cmath>
#include <map>
#include <set>

using namespace std;
int n, m, k, c[600], d[600], g[600][600];
vector <vector <int> > gg (150010);
bool used[150010];
int find(int a){
    int l = 0;
    int r = k;
    while (true){
        if (a > d[(l+r+1)/2]) l = (l + r + 1)/2;
        else if (a <= d[(l+r+1)/2-1]) r = (l + r + 1)/2;
        else break;
    }
    return (l+r+1)/2;
}
void dfs(int v){
    used[v] = true;
    for (int i = 0; i < gg[v].size(); i++)
        if (used[gg[v][i]] == false)
            dfs(gg[v][i]);
}

int main(){
    cin >> n >> m >> k;
    d[0] = 0;
    for (int i = 1; i <= k; i++){
        cin >> c[i];
        d[i] = d[i-1] + c[i];
    }

    for (int i = 0; i < 501; i++)
    for (int j = 0; j < 501; j++){
        if (i == j) g[i][j] = 0;
        else g[i][j] = -1;
    }

        for (int i = 0; i < m; i++){
            int u, v, x;
            cin >> u >> v >> x;
            int uu = find(u);
            int vv = find(v);
            if (uu == vv && x == 0) {gg[u].push_back(v); gg[v].push_back(u);}

            else if (uu != vv) {
                if (g[uu][vv] == -1){
                    g[uu][vv] = x;
                    g[vv][uu] = x;
                    }
                    else {
                    g[uu][vv] = min(x, g[uu][vv]);
                    g[vv][uu] = min(x, g[vv][uu]);
                    }

            }
        }
        for (int i = 0; i < k; i++)
            dfs(d[i] + 1);

        for (int i = 1; i <= n; i++)
            if(used[i] == false) {cout <<"No"; return 0;}

        cout << "Yes" << endl;
        for (int i = 1; i <= k; i++){
            for (int j = 1; j <= k; j++)
                cout << g[i][j] << " ";
            cout << endl;
        }

    return 0;
}


/*
    Day 7, Week 18 : March 7, 2014
    Time : 6:13 PM -
*/

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#define     inf     1000000009
#define     Z       100005
using namespace std;
int K;
int d[505][505];
int grp[Z];

void FLOYD()
{
    int i,j,k;

    for(k = 1; k <= K; k++){
        for(i = 1; i <=K; i++){
            for(j = 1; j <=K; j++){
                d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
            }
        }
    }

    return;
}
bool vis[Z],mark[505];
vector <int> v[Z];

void DFS(int at){
    int i;

    vis[at]  = true;

    for(i = 0; i < v[at].size(); i++){
        if(vis[v[at][i]]==true) continue;
        DFS(v[at][i]);
    }
    return;
}
int main()
{
    int N,M,i,c,now,j;

    cin >> N >> M >> K;

    for(i = 1; i <= N; i++){
        v[i].clear();
    }

    now = 1;
    for(i = 0; i < K; i++){
        cin >> c;
        for(j = 0; j < c; j++){
            grp[now] = i+1;
            now++;
        }
    }

    for(i = 1; i <=K; i++){
        for(j = 1; j <=K; j++){
            d[i][j] = inf;
        }
        d[i][i] = 0;
    }

    int a,b,x;

    for(i = 0; i < M; i++){
        cin >> a>>b >> x;
        if(x==0){
            v[a].push_back(b);
            v[b].push_back(a);
        }
        a = grp[a];
        b = grp[b];
        d[a][b] = min(d[a][b],x);
        d[b][a] = d[a][b];

    }

    memset(vis, false, sizeof(vis));
    memset(mark,false, sizeof(mark));

   int flag = 1;
    for(i = 1 ; i <= N; i++){
        if(vis[i]==true) continue;
        if(mark[grp[i]]==true) flag = 0;
        mark[grp[i]] = true;
        DFS(grp[i]);
    }

    if(flag==0) printf("No\n");
    else{
        printf("Yes\n");
        FLOYD();

        for(i = 1; i <=K; i++){
            for(j = 1; j <=K; j++){
                if(j != 1) printf(" ");
                if(d[i][j] == inf) d[i][j] = -1;
                printf("%d",d[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}


/*
    Day 7, Week 18 : March 7, 2014
    Time : 6:13 PM - 6:34 PM
*/

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#define     inf     1000000009
#define     Z       100005
using namespace std;
int K;
int d[505][505];
int grp[Z];

void FLiOYD()
{
    int i,j,k;

    for(k = 1; k <= K; k++){
        for(i = 1; i <=K; i++){
            for(j = 1; j <=K; j++){
                d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
            }
        }
    }

    return;
}
bool vis[Z],mark[505];
vector <int> v[Z];

void DFS(int at){
    int i;

    vis[at]  = true;

    for(i = 0; i < v[at].size(); i++){
        if(vis[v[at][i]]==true) continue;
        DFS(v[at][i]);
    }
    return;
}
int main()
{
    int N,M,i,c,now,j;

    cin >> N >> M >> K;

    for(i = 1; i <= N; i++){
        v[i].clear();
    }

    now = 1;
    for(i = 0; i < K; i++){
        cin >> c;
        for(j = 0; j < c; j++){
            grp[now] = i+1;
            now++;
        }
    }

    for(i = 1; i <=K; i++){
        for(j = 1; j <=K; j++){
            d[i][j] = inf;
        }
        d[i][i] = 0;
    }

    int a,b,x;

    for(i = 0; i < M; i++){
        cin >> a>>b >> x;
        if(x==0){
            v[a].push_back(b);
            v[b].push_back(a);
        }
        a = grp[a];
        b = grp[b];
        d[a][b] = min(d[a][b],x);
        d[b][a] = d[a][b];

    }

    memset(vis, false, sizeof(vis));
    memset(mark,false, sizeof(mark));

   int flag = 1;
    for(i = 1 ; i <= N; i++){
        if(vis[i]==true) continue;
        if(mark[grp[i]]==true) flag = 0;
        mark[grp[i]] = true;
        DFS(i);
    }

    if(flag==0) printf("No\n");
    else{
        printf("Yes\n");
        FLiOYD();

        for(i = 1; i <=K; i++){
            for(j = 1; j <=K; j++){
                if(j != 1) printf(" ");
                if(d[i][j] == inf) d[i][j] = -1;
                printf("%d",d[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}


/*
    Day 7, Week 18 : March 7, 2014
    Time : 6:13 PM -
*/

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#define     inf     1000000009
#define     Z       100005
using namespace std;
int K;
int d[505][505];
int grp[Z];

void FLOYD()
{
    int i,j,k;

    for(k = 1; k <= K; k++){
        for(i = 1; i <=K; i++){
            for(j = 1; j <=K; j++){
                d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
            }
        }
    }

    return;
}
bool vis[Z],mark[505];
vector <int> v[Z];

void DFS(int at){
    int i;

    vis[at]  = true;

    for(i = 0; i < v[at].size(); i++){
        if(vis[v[at][i]]==true) continue;
        DFS(v[at][i]);
    }
    return;
}
int main()
{
    int N,M,i,c,now,j;

    cin >> N >> M >> K;

    for(i = 1; i <= N; i++){
        v[i].clear();
    }

    now = 1;
    for(i = 0; i < K; i++){
        cin >> c;
        for(j = 0; j < c; j++){
            grp[now] = i+1;
            now++;
        }
    }

    for(i = 1; i <=K; i++){
        for(j = 1; j <=K; j++){
            d[i][j] = inf;
        }
        d[i][i] = 0;
    }

    int a,b,x;

    for(i = 0; i < M; i++){
        cin >> a>>b >> x;
        if(x==0){
            v[a].push_back(b);
            v[b].push_back(a);
        }
        a = grp[a];
        b = grp[b];
        d[a][b] = min(d[a][b],x);
        d[b][a] = d[a][b];

    }

    memset(vis, false, sizeof(vis));
    memset(mark,false, sizeof(mark));

   int flag = 1;
    for(i = 1 ; i <= N; i++){
        if(vis[i]==true) continue;
        if(mark[grp[i]]==true) flag = 0;
        mark[grp[i]] = true;
        DFS(i);
    }

    if(flag==0) printf("No\n");
    else{
        printf("Yes\n");
        FLOYD();

        for(i = 1; i <=K; i++){
            for(j = 1; j <=K; j++){
                if(j != 1) printf(" ");
                if(d[i][j] == inf) d[i][j] = -1;
                printf("%d",d[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}


/*
    Day 6, Week 18 : 6 March, 2014
    Time : 12:37 AM -
*/
#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
#define     pb  push_back
#define     Z   505
using namespace std;
int c[Z],grp[100005],d[Z][Z];
int mark[Z],vis[100005];

vector<int> g[100005];

void DFS(int at){
    vis[at] = 1;
    for(int i = 0;  i < g[at].size(); i++){
        if(vis[g[at][i]]) continue;
        DFS(vis[g[at][i]]);
    }
    return;
}
int main()
{
    int N,M,K,now,i,j,flag,u,v,x;

    cin >> N >> M >> K;

    now = 1;
    for(i = 1; i <= K; i++){
        cin >> c[i];
        for(j = 0; j < c[i]; j++){
            grp[now] = i;
            now++;
        }
        for(j = 1; j <=K; j++)
            d[i][j]= -1;
        d[i][i] = 0;

    }

    for(i = 0; i < M ; i++){
        cin >> u >> v>> x;
        if(x==0){
            g[u].pb(v);
            g[v].pb(u);
        }
        u = grp[u]; v =grp[v];
        if(d[u][v]== -1) d[u][v] = x;
        d[u][v] = min(d[u][v],x);
        d[v][u] =d[u][v];
    }

    int w;

    for(i = 1; i <=K ;i++){
        for(j = 1; j <=K ; j++){
            for(w = 1; w<=K; w++){
                if(d[i][w] == -1 || d[w][j] == -1) continue;

                d[i][j] = min(d[i][j], d[i][w]+d[w][j]);
            }
        }
    }

    for(i = 1; i <= N; i++){
        if(vis[i]) continue;
        if(mark[grp[i]]==1) flag = 0;
        mark[grp[i]] = 1;
        DFS(i);
    }
   if(flag==0) printf("No\n");
   else{
   printf("Yes\n");

   for(i = 1; i <=K; i++){
        for(j = 1; j<=K; j++){
            if(j!=1) printf(" ");
            printf("%d",d[i][j]);
        }
        printf("\n");
   }
   }

    return 0;
}


/*
    Day 7, Week 18 : March 7, 2014
    Time : 6:13 PM -
*/

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#define     inf     1000000009
#define     Z       100005
using namespace std;
int K;
int d[505][505];
int grp[Z];

void FLOYD()
{
    int i,j,k;

    for(k = 1; k <= K; k++){
        for(i = 1; i <=K; i++){
            for(j = 1; j <=K; j++){
                d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
            }
        }
    }

    return;
}
bool vis[Z],mark[505];
vector <int> v[Z];

void DFS(int at){
    int i;

    vis[at]  = true;

    for(i = 0; i < v[at].size(); i++){
        if(vis[v[at][i]]==true) continue;
        DFS(v[at][i]);
    }
    return;
}
int main()
{
    int N,M,i,c,now,j;

    cin >> N >> M >> K;

    for(i = 1; i <= N; i++){
        v[i].clear();
    }

    now = 1;
    for(i = 0; i < K; i++){
        cin >> c;
        for(j = 0; j < c; j++){
            grp[now] = i+1;
            now++;
        }
    }

    for(i = 1; i <=K; i++){
        for(j = 1; j <=K; j++){
            d[i][j] = inf;
        }
        d[i][i] = 0;
    }

    int a,b,x;

    for(i = 0; i < M; i++){
        cin >> a>>b >> x;
        if(x==0){
            v[a].push_back(b);
            v[b].push_back(a);
        }
        a = grp[a];
        b = grp[b];
        d[a][b] = min(d[a][b],x);
        d[b][a] = d[a][b];

    }

    memset(vis, false, sizeof(vis));
    memset(mark,false, sizeof(mark));

   int flag = 1;
    for(i = 1 ; i <= N; i++){
        if(vis[i]==true) continue;
        if(mark[grp[i]]==true) flag = 0;
        mark[grp[i]] = true;
        DFS(grp[i]);
    }

    if(flag==0) printf("No\n");
    else{
        printf("Yes\n");
        FLOYD();

        for(i = 1; i <=K; i++){
            for(j = 1; j <=K; j++){
                if(j != 1) printf(" ");
                printf("%d",d[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}


//Dipta Das CUET CSE-11

#include <cstdio>
#include <cstring>
#include <cmath>
#include <limits>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>

#define LL long long
#define Pi acos(-1.0)
#define EPS 1e-11
#define Mod 100000007
#define mset(a, s) memset(a, s, sizeof (a))
#define NN 100005
#define Inf 1000000000

using namespace std;

int par[NN],adj[505][505],cnt;

int main()
{
    int n,m,u,v,x,k,k1[505],k2=1,k3=0;

    scanf("%d%d%d",&n,&m,&k);

    for(int i=1; i<=k; i++)
    {
        scanf("%d",&k1[i]);
        k3++;
        for(int j=0; j<k1[i]; j++)
        {
            par[k2]=k3;
            k2++;
        }
    }

    for(int i=1; i<=k; i++)
    {
        for(int j=1; j<=k; j++) adj[i][j]=Inf;
        adj[i][i]=0;
    }

    bool flg=0;

    for(int i=0; i<m; i++)
    {
        scanf("%d%d%d",&u,&v,&x);
        adj[par[u]][par[v]]=adj[par[v]][par[u]]=min(adj[par[u]][par[v]],x);
        if(par[u]!=par[v] && x==0) flg=1;
    }

    if(flg)
    {
        printf("No");
        return 0;
    }
    for(int l=1; l<=k; l++)
    {
        for(int i=1; i<=k; i++)
        {
            for(int j=1; j<=k; j++)
            {
                adj[i][j]=min(adj[i][j],adj[i][l]+adj[l][j]);
            }
        }
    }

    printf("Yes\n");
    for(int i=1; i<=k; i++)
    {
        for(int j=1; j<=k; j++)
        {
            if(j!=1) printf(" ");
            if(adj[i][j]==Inf) adj[i][j]=-1;
            printf("%d",adj[i][j]);
        }
        puts("");
    }

    return 0;
}


//Dipta Das CUET CSE-11

#include <cstdio>
#include <cstring>
#include <cmath>
#include <limits>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>

#define LL long long
#define Pi acos(-1.0)
#define EPS 1e-11
#define Mod 100000007
#define mset(a, s) memset(a, s, sizeof (a))
#define NN 100005
#define Inf 1000000000

using namespace std;

int par[NN],adj[505][505],cnt;
bool vis[100005];

vector<int> e[100005];

void dfs(int n)
{
    cnt++;
    vis[n]=1;
    for(int i=0; i<e[n].size(); i++)
    {
        if(vis[e[n][i]]==0) dfs(e[n][i]);
    }
}

int main()
{
    int n,m,u,v,x,k,k1[505],k2=1,k3=0;

    scanf("%d%d%d",&n,&m,&k);

    for(int i=1; i<=k; i++)
    {
        scanf("%d",&k1[i]);
        k3++;
        for(int j=0; j<k1[i]; j++)
        {
            par[k2]=k3;
            k2++;
        }
    }

    for(int i=1; i<=k; i++)
    {
        for(int j=1; j<=k; j++) adj[i][j]=Inf;
        adj[i][i]=0;
    }

    for(int i=0; i<m; i++)
    {
        scanf("%d%d%d",&u,&v,&x);
        adj[par[u]][par[v]]=adj[par[v]][par[u]]=min(adj[par[u]][par[v]],x);
        if(par[u]==par[v])
        {
            e[u].push_back(v);
            e[v].push_back(u);
        }
    }

    int z=1;
    k1[0]=0;

    for(int i=1; i<=k; i++)
    {
        z=z+k1[i-1];
        cnt=0;
        dfs(z);
        if(cnt!=k1[i])
        {
            printf("No");
            return 0;
        }
    }

    for(int l=1; l<=k; l++)
    {
        for(int i=1; i<=k; i++)
        {
            for(int j=1; j<=k; j++)
            {
                adj[i][j]=min(adj[i][j],adj[i][l]+adj[l][j]);
            }
        }
    }

    printf("Yes\n");
    for(int i=1; i<=k; i++)
    {
        for(int j=1; j<=k; j++)
        {
            if(j!=1) printf(" ");
            if(adj[i][j]==Inf) adj[i][j]=-1;
            printf("%d",adj[i][j]);
        }
        puts("");
    }

    return 0;
}


//Dipta Das CUET CSE-11

#include <cstdio>
#include <cstring>
#include <cmath>
#include <limits>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>

#define LL long long
#define Pi acos(-1.0)
#define EPS 1e-11
#define Mod 100000007
#define mset(a, s) memset(a, s, sizeof (a))
#define NN 100005
#define Inf 1000000000

using namespace std;

int par[NN],adj[505][505],cnt;

int main()
{
    int n,m,u,v,x,k,k1[505],k2=1,k3=0;

    scanf("%d%d%d",&n,&m,&k);

    for(int i=1; i<=k; i++)
    {
        scanf("%d",&k1[i]);
        k3++;
        for(int j=0; j<k1[i]; j++)
        {
            par[k2]=k3;
            k2++;
        }
    }

    for(int i=1; i<=k; i++)
    {
        for(int j=1; j<=k; j++) adj[i][j]=Inf;
        adj[i][i]=0;
    }

    bool flg=0;

    for(int i=0; i<m; i++)
    {
        scanf("%d%d%d",&u,&v,&x);
        adj[par[u]][par[v]]=adj[par[v]][par[u]]=min(adj[par[u]][par[v]],x);
        if(par[u]!=par[v] && x==0) flg=1;
    }

    if(flg || m==0)
    {
        printf("No");
        return 0;
    }
    for(int l=1; l<=k; l++)
    {
        for(int i=1; i<=k; i++)
        {
            for(int j=1; j<=k; j++)
            {
                adj[i][j]=min(adj[i][j],adj[i][l]+adj[l][j]);
            }
        }
    }

    printf("Yes\n");
    for(int i=1; i<=k; i++)
    {
        for(int j=1; j<=k; j++)
        {
            if(j!=1) printf(" ");
            if(adj[i][j]==Inf) adj[i][j]=-1;
            printf("%d",adj[i][j]);
        }
        puts("");
    }

    return 0;
}


//Dipta Das CUET CSE-11

#include <cstdio>
#include <cstring>
#include <cmath>
#include <limits>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>

#define LL long long
#define Pi acos(-1.0)
#define EPS 1e-11
#define Mod 100000007
#define mset(a, s) memset(a, s, sizeof (a))
#define NN 100005
#define Inf 1000000000

using namespace std;

int par[NN],adj[505][505],cnt;
bool vis[100005];

vector<int> e[100005];

void dfs(int n)
{
    cnt++;
    vis[n]=1;
    for(int i=0; i<e[n].size(); i++)
    {
        if(vis[e[n][i]]==0) dfs(e[n][i]);
    }
}

int main()
{
    int n,m,u,v,x,k,k1[505],k2=1,k3=0;

    scanf("%d%d%d",&n,&m,&k);

    for(int i=1; i<=k; i++)
    {
        scanf("%d",&k1[i]);
        k3++;
        for(int j=0; j<k1[i]; j++)
        {
            par[k2]=k3;
            k2++;
        }
    }

    for(int i=1; i<=k; i++)
    {
        for(int j=1; j<=k; j++) adj[i][j]=Inf;
        adj[i][i]=0;
    }

    for(int i=0; i<m; i++)
    {
        scanf("%d%d%d",&u,&v,&x);
        adj[par[u]][par[v]]=adj[par[v]][par[u]]=min(adj[par[u]][par[v]],x);
        if(par[u]==par[v])
        {
            e[u].push_back(v);
            e[v].push_back(u);
        }
    }

    int z=1;
    k1[0]=0;

    for(int i=1; i<=k; i++)
    {
        z=z+k1[i-1];
        cnt=0;
        dfs(z);
        if(cnt!=k1[i])
        {
            printf("No");
            return 0;
        }
    }

    for(int l=1; l<=k; l++)
    {
        for(int i=1; i<=k; i++)
        {
            for(int j=1; j<=k; j++)
            {
                adj[i][j]=min(adj[i][j],adj[i][k]+adj[k][j]);
            }
        }
    }

    printf("Yes\n");
    for(int i=1; i<=k; i++)
    {
        for(int j=1; j<=k; j++)
        {
            if(j!=1) printf(" ");
            if(adj[i][j]==Inf) adj[i][j]=-1;
            printf("%d",adj[i][j]);
        }
        puts("");
    }

    return 0;
}


//Dipta Das CUET CSE-11

#include <cstdio>
#include <cstring>
#include <cmath>
#include <limits>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>

#define LL long long
#define Pi acos(-1.0)
#define EPS 1e-11
#define Mod 100000007
#define mset(a, s) memset(a, s, sizeof (a))
#define NN 100005
#define Inf 1000000000

using namespace std;

int par[NN],adj[505][505],cnt;
bool vis[100005];

vector<int> e[100005];

void dfs(int n)
{
    cnt++;
    vis[n]=1;
    for(int i=0; i<e[n].size(); i++)
    {
        if(vis[e[n][i]]==0) dfs(e[n][i]);
    }
}

int main()
{
    int n,m,u,v,x,k,k1[505],k2=1,k3=0;

    scanf("%d%d%d",&n,&m,&k);

    for(int i=1; i<=k; i++)
    {
        scanf("%d",&k1[i]);
        k3++;
        for(int j=0; j<k1[i]; j++)
        {
            par[k2]=k3;
            k2++;
        }
    }

    for(int i=1; i<=k; i++)
    {
        for(int j=1; j<=k; j++) adj[i][j]=Inf;
        adj[i][i]=0;
    }

    bool flg=0;

    for(int i=0; i<m; i++)
    {
        scanf("%d%d%d",&u,&v,&x);
        adj[par[u]][par[v]]=adj[par[v]][par[u]]=min(adj[par[u]][par[v]],x);
        if(par[u]==par[v] && x==0)
        {
            e[u].push_back(v);
            e[v].push_back(u);
        }
        else if(x==0) flg=1;
    }

    if(flg)
    {
        printf("No");
        return 0;
    }

    int z=1;
    k1[0]=0;

    for(int i=1; i<=k; i++)
    {
        z=z+k1[i-1];
        cnt=0;
        dfs(z);
        if(cnt!=k1[i])
        {
            printf("No");
            return 0;
        }
    }

    for(int l=1; l<=k; l++)
    {
        for(int i=1; i<=k; i++)
        {
            for(int j=1; j<=k; j++)
            {
                adj[i][j]=min(adj[i][j],adj[i][l]+adj[l][j]);
            }
        }
    }

    printf("Yes\n");
    for(int i=1; i<=k; i++)
    {
        for(int j=1; j<=k; j++)
        {
            if(j!=1) printf(" ");
            if(adj[i][j]==Inf) adj[i][j]=-1;
            printf("%d",adj[i][j]);
        }
        puts("");
    }

    return 0;
}


//Dipta Das CUET CSE-11

#include <cstdio>
#include <cstring>
#include <cmath>
#include <limits>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>

#define LL long long
#define Pi acos(-1.0)
#define EPS 1e-11
#define Mod 100000007
#define mset(a, s) memset(a, s, sizeof (a))
#define NN 100005
#define Inf 1000000000

using namespace std;

int par[NN],adj[505][505];

int main()
{
    int n,m,u,v,x,k,k1,k2=1,k3=0;

    scanf("%d%d%d",&n,&m,&k);

    for(int i=1; i<=k; i++)
    {
        scanf("%d",&k1);
        k3++;
        for(int j=0; j<k1; j++)
        {
            par[k2]=k3;
            k2++;
        }
    }

    for(int i=1; i<=k; i++)
    {
        for(int j=1; j<=k; j++) adj[i][j]=Inf;
        adj[i][i]=0;
    }

    for(int i=0; i<m; i++)
    {
        scanf("%d%d%d",&u,&v,&x);
        adj[par[u]][par[v]]=adj[par[v]][par[u]]=min(adj[par[u]][par[v]],x);
    }

    if(m==0) printf("No");

    else
    {
        for(int l=1; l<=k; l++)
        {
            for(int i=1; i<=k; i++)
            {
                for(int j=1; j<=k; j++)
                {
                    adj[i][j]=min(adj[i][j],adj[i][k]+adj[k][j]);
                }
            }
        }

        printf("Yes\n");
        for(int i=1; i<=k; i++)
        {
            for(int j=1; j<=k; j++)
            {
                if(j!=1) printf(" ");
                if(adj[i][j]==Inf) adj[i][j]=-1;
                printf("%d",adj[i][j]);
            }
            puts("");
        }
    }

    return 0;
}


//Dipta Das CUET CSE-11

#include <cstdio>
#include <cstring>
#include <cmath>
#include <limits>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>

#define LL long long
#define Pi acos(-1.0)
#define EPS 1e-11
#define Mod 100000007
#define mset(a, s) memset(a, s, sizeof (a))
#define NN 100005
#define Inf 1000000000

using namespace std;

int par[NN],adj[505][505],cnt;
bool vis[100005];

vector<int> e[100005];

void dfs(int n)
{
    cnt++;
    vis[n]=1;
    for(int i=0; i<e[n].size(); i++)
    {
        if(vis[e[n][i]]==0) dfs(e[n][i]);
    }
}

int main()
{
    int n,m,u,v,x,k,k1[505],k2=1,k3=0;

    scanf("%d%d%d",&n,&m,&k);

    for(int i=1; i<=k; i++)
    {
        scanf("%d",&k1[i]);
        k3++;
        for(int j=0; j<k1[i]; j++)
        {
            par[k2]=k3;
            k2++;
        }
    }

    for(int i=1; i<=k; i++)
    {
        for(int j=1; j<=k; j++) adj[i][j]=Inf;
        adj[i][i]=0;
    }

    bool flg=0;

    for(int i=0; i<m; i++)
    {
        scanf("%d%d%d",&u,&v,&x);
        adj[par[u]][par[v]]=adj[par[v]][par[u]]=min(adj[par[u]][par[v]],x);
        if(par[u]==par[v])
        {
            e[u].push_back(v);
            e[v].push_back(u);
        }
        else if(x==0) flg=1;
    }

    if(flg)
    {
        printf("No");
        return 0;
    }

    int z=1;
    k1[0]=0;

    for(int i=1; i<=k; i++)
    {
        z=z+k1[i-1];
        cnt=0;
        dfs(z);
        if(cnt!=k1[i])
        {
            printf("No");
            return 0;
        }
    }

    for(int l=1; l<=k; l++)
    {
        for(int i=1; i<=k; i++)
        {
            for(int j=1; j<=k; j++)
            {
                adj[i][j]=min(adj[i][j],adj[i][l]+adj[l][j]);
            }
        }
    }

    printf("Yes\n");
    for(int i=1; i<=k; i++)
    {
        for(int j=1; j<=k; j++)
        {
            if(j!=1) printf(" ");
            if(adj[i][j]==Inf) adj[i][j]=-1;
            printf("%d",adj[i][j]);
        }
        puts("");
    }

    return 0;
}