#include <bits/stdc++.h>
#define abdelrahman001 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define F first
#define S second
#define inFile(t) freopen((t),"r",stdin)
#define outFile(t) freopen((t),"w",stdout)
#define pb push_back
#define pf push_front
#define Pb pop_back
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define Pf pop_front
#define sz size
#define MP make_pair
#define pc __builtin_popcount
#define pi 2*acos(0)
#define BiEdge(v,a,b) (v)[(a)].pb(b),(v)[(b)].pb((a))
#define mod 1000000007
#define point complex<ld>
#define X real()
#define Y imag()
#define coordinate pair<ll,ll>
#define cross(a,b)(conj(a)*(b)).imag()
#define dot(a,b) (conj(a)*(b)).real()
#define rectangle pair<coordinate,coordinate>
#define line tuple<ll,ll,ll>
#define EPS 1e-7
#define polygon vector<point>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int const N = 2*1e5+5,M = 105;
using namespace std;

string s;
ll rem(string a)
{
    ll tmp = 0;
    for(auto i:a)
        tmp = tmp*10 + (i-'0'),tmp%=7;
    return tmp;
}
int main()
{
    abdelrahman001
    string s;
    cin >> s;
    sort(all(s));
    do
    {
        if(!rem(s) && s[0]!='0')
            return cout << s,0;
    } while(next_permutation(all(s)));
    return 0;
}


#include <bits/stdc++.h>
#define abdelrahman001 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define F first
#define S second
#define inFile(t) freopen((t),"r",stdin)
#define outFile(t) freopen((t),"w",stdout)
#define pb push_back
#define pf push_front
#define Pb pop_back
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define Pf pop_front
#define sz size
#define MP make_pair
#define pc __builtin_popcount
#define pi 2*acos(0)
#define BiEdge(v,a,b) (v)[(a)].pb(b),(v)[(b)].pb((a))
#define mod 1000000007
#define point complex<ld>
#define X real()
#define Y imag()
#define coordinate pair<ll,ll>
#define cross(a,b)(conj(a)*(b)).imag()
#define dot(a,b) (conj(a)*(b)).real()
#define rectangle pair<coordinate,coordinate>
#define line tuple<ll,ll,ll>
#define EPS 1e-7
#define polygon vector<point>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int const N = 2*1e5+5,M = 105;
using namespace std;

string s;
ll rem(string a)
{
    ll tmp = 0;
    for(auto i:a)
        tmp = tmp*10 + (i-'0'),tmp%=7;
    return tmp;
}
int main()
{
    abdelrahman001
    string s;
    cin >> s;
    sort(all(s));
    do
    {
        if(!rem(s))
            return cout << s,0;
    } while(next_permutation(all(s)));
    return 0;
}


#include <bits/stdc++.h>
#define abdelrahman001 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define F first
#define S second
#define inFile(t) freopen((t),"r",stdin)
#define outFile(t) freopen((t),"w",stdout)
#define pb push_back
#define pf push_front
#define Pb pop_back
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define Pf pop_front
#define sz size
#define MP make_pair
#define pc __builtin_popcount
#define pi 2*acos(0)
#define BiEdge(v,a,b) (v)[(a)].pb(b),(v)[(b)].pb((a))
#define mod 1000000007
#define point complex<ld>
#define X real()
#define Y imag()
#define coordinate pair<ll,ll>
#define cross(a,b)(conj(a)*(b)).imag()
#define dot(a,b) (conj(a)*(b)).real()
#define rectangle pair<coordinate,coordinate>
#define line tuple<ll,ll,ll>
#define EPS 1e-7
#define polygon vector<point>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int const N = 2*1e5+5,M = 105;
using namespace std;

string s,a="1689",t;
ll arr[10];
ll rem(string a)
{
    ll tmp = 0;
    for(auto i:a)
        tmp = tmp*10 + (i-'0'),tmp%=7;
    return tmp;
}
int main()
{
    abdelrahman001
    string s;
    cin >> s;
    for(auto i:s)
        arr[i-'0']++;
    arr[1]--,arr[6]--,arr[8]--,arr[9]--;
    for(int i =1;i<=9;i++)
        while(arr[i]--)
            t.pb((i+'0'));
    do
    {
        string ans = t+a;
        for(int i = 0;i<arr[0];i++)
            ans.pb('0');
        if(!rem(ans))
            return cout << ans,0;
    } while(next_permutation(all(a)));
    return 0;
}


#include <bits/stdc++.h>
#define abdelrahman001 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define F first
#define S second
#define inFile(t) freopen((t),"r",stdin)
#define outFile(t) freopen((t),"w",stdout)
#define pb push_back
#define pf push_front
#define Pb pop_back
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define Pf pop_front
#define sz size
#define MP make_pair
#define pc __builtin_popcount
#define pi 2*acos(0)
#define BiEdge(v,a,b) (v)[(a)].pb(b),(v)[(b)].pb((a))
#define mod 1000000007
#define point complex<ld>
#define X real()
#define Y imag()
#define coordinate pair<ll,ll>
#define cross(a,b)(conj(a)*(b)).imag()
#define dot(a,b) (conj(a)*(b)).real()
#define rectangle pair<coordinate,coordinate>
#define line tuple<ll,ll,ll>
#define EPS 1e-7
#define polygon vector<point>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int const N = 2*1e5+5,M = 105;
using namespace std;

ll arr[10],rem;
vector<string>num = {"1869","1968","1689","6198","1698","1986","1896"};
string s;
int main()
{
    abdelrahman001
    cin >> s;
    for(auto i:s)
        arr[i-'0']++;
    arr[1]--,arr[6]--,arr[9]--,arr[8]--;
    for(int i = 1;i<=9;i++)
    {
        while(arr[i]--)
        {
            cout << i;
            rem*=10,rem%=7;
        }
    }
    cout << num[(7-rem)%7];
    while(arr[0]--)
        cout << 0;
    return 0;
}


#include <bits/stdc++.h>
#define abdelrahman001 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define F first
#define S second
#define inFile(t) freopen((t),"r",stdin)
#define outFile(t) freopen((t),"w",stdout)
#define pb push_back
#define pf push_front
#define Pb pop_back
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define Pf pop_front
#define sz size
#define MP make_pair
#define pc __builtin_popcount
#define pi 2*acos(0)
#define BiEdge(v,a,b) (v)[(a)].pb(b),(v)[(b)].pb((a))
#define mod 1000000007
#define point complex<ld>
#define X real()
#define Y imag()
#define coordinate pair<ll,ll>
#define cross(a,b)(conj(a)*(b)).imag()
#define dot(a,b) (conj(a)*(b)).real()
#define rectangle pair<coordinate,coordinate>
#define line tuple<ll,ll,ll>
#define EPS 1e-7
#define polygon vector<point>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int const N = 2*1e5+5,M = 105;
using namespace std;

string s;
ll rem(string a)
{
    ll tmp = 0;
    for(auto i:a)
        tmp = tmp*10 + (i-'0'),tmp%=7;
    return tmp;
}
int main()
{
    abdelrahman001
    string s;
    cin >> s;
    do
    {
        if(!rem(s))
            return cout << s,0;
    } while(next_permutation(all(s)));
    return 0;
}


#include <bits/stdc++.h>
#define abdelrahman001 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define F first
#define S second
#define inFile(t) freopen((t),"r",stdin)
#define outFile(t) freopen((t),"w",stdout)
#define pb push_back
#define pf push_front
#define Pb pop_back
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define Pf pop_front
#define sz size
#define MP make_pair
#define pc __builtin_popcount
#define pi 2*acos(0)
#define BiEdge(v,a,b) (v)[(a)].pb(b),(v)[(b)].pb((a))
#define mod 1000000007
#define point complex<ld>
#define X real()
#define Y imag()
#define coordinate pair<ll,ll>
#define cross(a,b)(conj(a)*(b)).imag()
#define dot(a,b) (conj(a)*(b)).real()
#define rectangle pair<coordinate,coordinate>
#define line tuple<ll,ll,ll>
#define EPS 1e-7
#define polygon vector<point>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int const N = 2*1e5+5,M = 105;
using namespace std;

string s;
ll rem(string a)
{
    ll tmp = 0;
    for(auto i:a)
        tmp = tmp*10 + (i-'0'),tmp%=7;
    return tmp;
}
int main()
{
    abdelrahman001
    string s;
    cin >> s;
    do
    {
        if(!rem(s))
            return cout << s,0;
        next_permutation(all(s));
    } while(1);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void rwFile() {
    freopen("INP.inp", "r", stdin);
    freopen("OUT.out", "w", stdout);
}

void solveD_PetyaAndHisFriends() {
    int n; cin >> n;
    if (n == 2) {
        cout << -1;
        return;
    }
    long long arr[n];
    arr[0] = 2;
    for (int i = 1; i < n - 1; i++) arr[i] = arr[i - 1] * 2;
    arr[0] *= 5;
    arr[n - 1] = 5;
    for (int i = 1; i < n; i++) arr[i] *= 3;
    for (int i = 0; i < n; i++) cout << arr[i] << '\n';
}

string toString(int a) {
    string res = "";
    while (a) {
        int t = a % 10;
        res += ('0' + t);
        a /= 10;
    }
    reverse(res.begin(), res.end());
    return res;
}

bool isPalindrome(int a) {
    string tmp = toString(a);
    int n = tmp.size();
    for (int i = 0; i < n; i++) if (tmp[i] != tmp[n - 1 - i]) return false;
    return true;
}

void solveA_PrimesOrPalindromes() {
    long long p, q; cin >> p >> q;
    const int maxN = 1e6;
    int rub[maxN];
    rub[1] = 1;
    for (int i = 2; i < maxN; i++) {
        if (isPalindrome(i)) rub[i] = rub[i - 1] + 1;
        else rub[i] = rub[i - 1];
    }
    //cout << rub[maxN - 1];
    vector<int> primes;
    int check[maxN];
    memset(check, 0, sizeof(check));
    for (int i = 2; i < maxN; i++) {
        if (check[i] == 0) {
            primes.push_back(i);
            for (int j = i; j < maxN; j += i) check[j] = 1;
        }
    }
    //cout << (int)primes.size() - 42 * 1998;
    int i;
    for (i = maxN - 1; i > 0; i--) {
        int t = upper_bound(primes.begin(), primes.end(), i) - primes.begin();
        if (rub[i] * p >= q * t) break;
    }
    cout << i;
}

void solveC_TrailingLovesOrLoeufs() {
    const int maxN = 1e6;
    vector<long long> primes;
    int check[maxN];
    memset(check, 0, sizeof(check));
    for (int i = 2; i < maxN; i++) {
        if (check[i] == 0) {
            primes.push_back(i);
            for (int j = i; j < maxN; j += i) check[j] = 1;
        }
    }
    long long n, b; cin >> n >> b;
    long long res = 1e18;
    for (long long i : primes) {
        if (b % i != 0 || i > b) continue;
        int e = 0;
        while (b % i == 0) {
            b /= i;
            e++;
        }
        long long ans = 0;
        long long tmp = n;
        while (tmp) {
            tmp /= i;
            ans += tmp;
        }
        ans /= e;
        //cout << i << ' ' << ans << '\n';
        res = min(res, ans);
    }
    if (b != 1) {
        long long tmp = n;
        long long ans = 0;
        while (tmp) {
            tmp /= b;
            ans += tmp;
        }
        res = min(res, ans);
    }
    cout << res;
}

void solveC_DevuAndPartitioningOfTheArray() {
    int n, k, p; cin >> n >> k >> p;
    vector<int> odd, even;
    for (int i = 0; i < n; i++) {
        int x; cin >> x;
        if (x % 2 == 0) even.push_back(x);
        else odd.push_back(x);
    }
    int oLen = odd.size(), eLen = even.size();
    if (oLen < k - p || (oLen - k + p) % 2 != 0) {
        cout << "NO";
        return;
    }
    if ((oLen - k + p) / 2 + eLen < p) {
        cout << "NO";
        return;
    }
    vector<int> arr[k];
    for (int i = 0; i < k - p; i++) {
        arr[i].push_back(odd[i]);
    }
    for (int i = 0; i < eLen; i++) {
        arr[(k - p + i) % k].push_back(even[i]);
    }
    int x = 0;
    while (k - p + x * 2 < oLen) {
        arr[(k - 1 - x + n / k * k) % k].push_back(odd[k - p + x * 2]);
        arr[(k - 1 - x + n / k * k) % k].push_back(odd[k - p + x * 2 + 1]);
        x++;
    }
    cout << "YES\n";
    for (int i = 0; i < k; i++) {
        cout << arr[i].size() << " ";
        for (int j : arr[i]) cout << j << ' ';
        cout << '\n';
    }
}

void solveB_VeryInterestingGame() {
    long long a, b, mod;
    cin >> a >> b >> mod;
    if (b + 1 >= mod) {
        cout << 2;
        return;
    }
    long long tmp = 1, res = 2e9;
    for (int i = 0; i < 9; i++) tmp = tmp * 10 % mod;
    if (tmp == 0) {
        cout << 2;
        return;
    }
    for (long long i = 0; i < mod; i++) {
        long long temp = i * tmp % mod;
        if (temp > 0 && temp < mod - b) {
            res = i;
            break;
        }
    }
    if (res > a) {
        cout << 2;
        return;
    }
    int len = 9;
    tmp = res;
    while (tmp) {
        tmp /= 10;
        len--;
    }
    cout << "1 ";
    for (int i = 0; i < len; i++) cout << 0;
    cout << res;
}

int getAns(int a, int b) {
    int res = 0;
    while (b != 0) {
        res += a / b;
        int tmp = a % b;
        a = b;
        b = tmp;
    }
    return res - 1;
}

int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

void solveB_PairsOfNumbers() {
    int n; cin >> n;
    if (n == 1) {
        cout << 0;
        return;
    }
    int res = 1e9;
    for (int i = 1; i < n; i++) {
        if (gcd(i, n) != 1) continue;
        res = min(res, getAns(i, n));
    }
    cout << res;
}

void solveB_Colliders() {
    int n, m; cin >> n >> m;
    //int arr[n + 1];
    vector<int> primes;
    int maxN = 1e5 + 1;
    int check[maxN];
    memset(check, 0, sizeof(check));
    for (int i = 2; i < 1000; i++) {
        if (check[i] == 0) {
            primes.push_back(i);
            for (int j = i * i; j < maxN; j += i) check[j] = i;
        }
    }
    for (int i = 2; i < maxN; i++) {
        if (check[i] == 0) check[i] = i;
    }
    int On[maxN];
    memset(On, 0, sizeof(On));
    map<int, set<int>> M;
    while (m--) {
        char x;
        int num;
        cin >> x >> num;
        int tmp = num;
        if (x == '+') {
            if (On[num] == 1) {
                cout << "Already on\n";
                continue;
            }
            int able = 1;
            int conf;
            while (tmp != 1) {
                int p = check[tmp];
                while (tmp % p == 0) tmp /= p;
                if (!M[p].empty()) {
                    able = 0;
                    conf = *M[p].begin();
                    break;
                }
            }
            if (able == 0) {
                cout << "Conflict with " << conf << '\n';
                continue;
            }
            tmp = num;
            while (tmp != 1) {
                int p = check[tmp];
                while (tmp % p == 0) tmp /= p;
                M[p].insert(num);
            }
            On[num] = 1;
            cout << "Success\n";
        }
        else {
            if (On[num] == 0) {
                cout << "Already off\n";
                continue;
            }
            On[num] = 0;
            while (tmp != 1) {
                int p = check[tmp];
                while (tmp % p == 0) tmp /= p;
                M[p].erase(M[p].find(num));
            }
            cout << "Success\n";
        }
    }
}

void solveA_DivisibleBySeven() {
    int arr[] = {1869, 1968, 1689, 6198, 1698, 1986, 1896};
    string s; cin >> s;
    if (s.size() == 4) {
        cout << 1869;
        return;
    }
    vector<char> num;
    int check[] = {0, 0, 0, 0};
    for (int i = 0; i < (int)s.size(); i++) {
        if (s[i] == '1' && check[0] == 0) {
            check[0] = 1;
            continue;
        }
        if (s[i] == '6' && check[1] == 0) {
            check[1] = 1;
            continue;
        }
        if (s[i] == '8' && check[2] == 0) {
            check[2] = 1;
            continue;
        }
        if (s[i] == '9' && check[3] == 0) {
            check[3] = 1;
            continue;
        }
        num.push_back(s[i]);
    }
    int mod[] = {3, 2, 6, 4, 5, 1};
    string res = "";
    int len = num.size();
    long long ans = (num[len - 1] - '0') % 7;
    res += num[len - 1];
    int i;
    for (i = 0; i < len - 1; i++) {
        res += num[i];
        ans = (ans + mod[i % 6] * (num[i] - '0')) % 7;
        //cout << ans << ' ';
    }
    int j;
    for (j = 0; j < 7; j++) {
        if ((j * mod[i % 6] + ans) % 7 == 0) break;
    }
    reverse(res.begin(), res.end());
    cout << arr[j] << res;
    //cout << res << ' ' << ans << ' ';
    //reverse(res.begin(), res.end());
    //cout << res << arr[6 - ans];
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);
    //rwFile();
    ///solveD_PetyaAndHisFriends();
    ///solveA_PrimesOrPalindromes();
    ///solveC_TrailingLovesOrLoeufs();
    ///solveC_DevuAndPartitioningOfTheArray();
    ///solveB_VeryInterestingGame();
    ///solveB_PairsOfNumbers();
    ///solveB_Colliders();
    solveA_DivisibleBySeven();
}


#include<bits/stdc++.h>
using namespace std;
#define fast                         ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int                          long long int
#define double                       long double
#define ff                           first
#define ss                           second
#define pb                           push_back
#define eb                           emplace_back
#define all(x)                       (x).begin(), (x).end()
#define sz(x)                        ((int)(x).size())
#define len(x)                       ((int)(x).length())
#define pii                          pair<int,int>
#define vi                            vector<int>
#define vd                           vector<double>
#define vs                            vector<string>
#define mp                          map<int,int>
#define ms(x,y)                     memset(x,y,sizeof(x))
#define ump                         unordered_map<int,int>

#define lb                            lower_bound
#define ub                            upper_bound
#define nx                            cout<<"\n";
#define inf                           9223372036854775807
#define PI                            3.14159265

const int MOD = 1e9 + 7;

inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a * b / gcd(a, b); }
inline int add(int a, int b) {    return ((a % MOD) + (b % MOD)) % MOD;}

inline int multi(int a, int b) {   return ((a % MOD) * (b % MOD)) % MOD;}

inline int sub(int a, int b) {       a %= MOD; b %= MOD; a -= b; if (a < 0) a += MOD; return a;}

inline int power(int a, int b) {       a %= MOD; int res = 1; while (b > 0) {if (b & 1) {res = multi(res, a);} a = multi(a, a); b >>= 1;}  return res;}

bool isPrime(int  x) {      if (x <= 4 || x % 2 == 0 || x % 3 == 0) {return x == 2 || x == 3;} for (int i = 5; i * i <= x; i += 6) {if (x % i == 0 || x % (i + 2) == 0) { return 0;}} return 1;}

vi fill()
{
	vi res(7, 0);
	string s = "1869";
	do {
		int num = 0;
		for (char c : s) {
			num = num * 10 + (c - '0');
		}
		res[num % 7] = num;
	} while (next_permutation(s.begin(), s.end()));
	return res;
}
int32_t main()
{
	fast ;
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	//1869
	vi out = fill();

	string s; cin >> s;
	int n = len(s);
	int c0 = 0;
	mp mop;
	for (int i = 0; i < n; i++) {
		if (s[i] == '0')c0++;
		else mop[s[i] - '0']++;
	}
	mop[1]--;
	mop[8]--;
	mop[6]--;
	mop[9]--;
	int rem = 0;
	string res = "";
	for (pii p : mop) {
		while (p.ss--) {
			res = res + to_string(p.ff);
			rem = (rem * 10 + p.ff) % 7;
		}
	}

	cout << res;
	cout << out[rem];
	while (c0--)cout << 0;

}


#include<bits/stdc++.h>
using namespace std;
#define fast                         ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int                          long long int
#define double                       long double
#define ff                           first
#define ss                           second
#define pb                           push_back
#define eb                           emplace_back
#define all(x)                       (x).begin(), (x).end()
#define sz(x)                        ((int)(x).size())
#define len(x)                       ((int)(x).length())
#define pii                          pair<int,int>
#define vi                            vector<int>
#define vd                           vector<double>
#define vs                            vector<string>
#define mp                          map<int,int>
#define ms(x,y)                     memset(x,y,sizeof(x))
#define ump                         unordered_map<int,int>

#define lb                            lower_bound
#define ub                            upper_bound
#define nx                            cout<<"\n";
#define inf                           9223372036854775807
#define PI                            3.14159265

const int MOD = 1e9 + 7;

inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a * b / gcd(a, b); }
inline int add(int a, int b) {    return ((a % MOD) + (b % MOD)) % MOD;}

inline int multi(int a, int b) {   return ((a % MOD) * (b % MOD)) % MOD;}

inline int sub(int a, int b) {       a %= MOD; b %= MOD; a -= b; if (a < 0) a += MOD; return a;}

inline int power(int a, int b) {       a %= MOD; int res = 1; while (b > 0) {if (b & 1) {res = multi(res, a);} a = multi(a, a); b >>= 1;}  return res;}

bool isPrime(int  x) {      if (x <= 4 || x % 2 == 0 || x % 3 == 0) {return x == 2 || x == 3;} for (int i = 5; i * i <= x; i += 6) {if (x % i == 0 || x % (i + 2) == 0) { return 0;}} return 1;}

void fill(int rem)
{

	string s = "1869";
	do {
		int num = rem;
		for (char c : s) {
			num = num * 10 + (c - '0');
		}
		if (num % 7 == 0) {cout << s; return;}
	} while (next_permutation(s.begin(), s.end()));

}
int32_t main()
{
	fast ;
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	string s; cin >> s;
	int n = len(s);
	int c0 = 0;
	ump mop;
	for (int i = 0; i < n; i++) {
		if (s[i] == '0')c0++;
		else mop[s[i] - '0']++;
	}
	mop[1]--;
	mop[8]--;
	mop[6]--;
	mop[9]--;
	int rem = 0;
	string res = "";
	for (pii p : mop) {
		while (p.ss--) {
			res = res + to_string(p.ff);
			rem = (rem * 10 + p.ff) % 7;
		}
	}

	cout << res;
	fill(rem);
	while (c0 > 0) {cout << 0; c0--;}

}


#include<bits/stdc++.h>
using namespace std;
#define fast                         ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int                          long long int
#define double                       long double
#define ff                           first
#define ss                           second
#define pb                           push_back
#define eb                           emplace_back
#define all(x)                       (x).begin(), (x).end()
#define sz(x)                        ((int)(x).size())
#define len(x)                       ((int)(x).length())
#define pii                          pair<int,int>
#define vi                            vector<int>
#define vd                           vector<double>
#define vs                            vector<string>
#define mp                          map<int,int>
#define ms(x,y)                     memset(x,y,sizeof(x))
#define ump                         unordered_map<int,int>

#define lb                            lower_bound
#define ub                            upper_bound
#define nx                            cout<<"\n";
#define inf                           9223372036854775807
#define PI                            3.14159265

const int MOD = 1e9 + 7;

inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a * b / gcd(a, b); }
inline int add(int a, int b) {    return ((a % MOD) + (b % MOD)) % MOD;}

inline int multi(int a, int b) {   return ((a % MOD) * (b % MOD)) % MOD;}

inline int sub(int a, int b) {       a %= MOD; b %= MOD; a -= b; if (a < 0) a += MOD; return a;}

inline int power(int a, int b) {       a %= MOD; int res = 1; while (b > 0) {if (b & 1) {res = multi(res, a);} a = multi(a, a); b >>= 1;}  return res;}

bool isPrime(int  x) {      if (x <= 4 || x % 2 == 0 || x % 3 == 0) {return x == 2 || x == 3;} for (int i = 5; i * i <= x; i += 6) {if (x % i == 0 || x % (i + 2) == 0) { return 0;}} return 1;}

void fill(int rem)
{

	if (rem == 0)cout << 1869;
	else if (rem == 1)cout << 6198;
	else if (rem == 2)cout << 1896;
	else if (rem == 3)cout << 6918;
	else if (rem == 4)cout << 1986;
	else if (rem == 5)cout << 1968;
	else if (rem == 6)cout << 8691;

}
int32_t main()
{
	fast ;
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	string s; cin >> s;
	int n = len(s);
	int c0 = 0;
	int c1 = 1, c6 = 1, c8 = 1, c9 = 1;
	int rem = 0;
	string res = "";
	for (int i = 0; i < n; i++) {
		if (s[i] == '0')c0++;
		else if (s[i] == '1' && c1)c1--;
		else if (s[i] == '6' && c6)c6--;
		else if (s[i] == '8' && c8)c8--;
		else if (s[i] == '9' && c9)c9--;
		else {
			res = res + s[i];
			rem = (rem * 10 + (int)(s[i] - '0') ) % 7;
		}
	}

	cout << res;
	fill(rem);
	while (c0 > 0) {cout << 0; c0--;}

}


#include<bits/stdc++.h>
using namespace std;
#define fast                         ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int                          long long int
#define double                       long double
#define ff                           first
#define ss                           second
#define pb                           push_back
#define eb                           emplace_back
#define all(x)                       (x).begin(), (x).end()
#define sz(x)                        ((int)(x).size())
#define len(x)                       ((int)(x).length())
#define pii                          pair<int,int>
#define vi                            vector<int>
#define vd                           vector<double>
#define vs                            vector<string>
#define mp                          map<int,int>
#define ms(x,y)                     memset(x,y,sizeof(x))
#define ump                         unordered_map<int,int>

#define lb                            lower_bound
#define ub                            upper_bound
#define nx                            cout<<"\n";
#define inf                           9223372036854775807
#define PI                            3.14159265

const int MOD = 1e9 + 7;

inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a * b / gcd(a, b); }
inline int add(int a, int b) {    return ((a % MOD) + (b % MOD)) % MOD;}

inline int multi(int a, int b) {   return ((a % MOD) * (b % MOD)) % MOD;}

inline int sub(int a, int b) {       a %= MOD; b %= MOD; a -= b; if (a < 0) a += MOD; return a;}

inline int power(int a, int b) {       a %= MOD; int res = 1; while (b > 0) {if (b & 1) {res = multi(res, a);} a = multi(a, a); b >>= 1;}  return res;}

bool isPrime(int  x) {      if (x <= 4 || x % 2 == 0 || x % 3 == 0) {return x == 2 || x == 3;} for (int i = 5; i * i <= x; i += 6) {if (x % i == 0 || x % (i + 2) == 0) { return 0;}} return 1;}

void fill(int rem)
{

	string s = "1869";
	do {
		int num = rem;
		for (char c : s) {
			num = num * 10 + (c - '0');
		}
		if (num % 7 == 0) {cout << s; return;}
	} while (next_permutation(s.begin(), s.end()));

}
int32_t main()
{
	fast ;
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	string s; cin >> s;
	int n = len(s);
	int c0 = 0;
	mp mop;
	for (int i = 0; i < n; i++) {
		if (s[i] == '0')c0++;
		else mop[s[i] - '0']++;
	}
	mop[1]--;
	mop[8]--;
	mop[6]--;
	mop[9]--;
	int rem = 0;
	string res = "";
	for (pii p : mop) {
		while (p.ss--) {
			res = res + to_string(p.ff);
			rem = (rem * 10 + p.ff) % 7;
		}
	}

	cout << res;
	fill(rem);
	while (c0--)cout << 0;

}


#include<bits/stdc++.h>
using namespace std;
#define fast                         ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int                          long long int
#define double                       long double
#define ff                           first
#define ss                           second
#define pb                           push_back
#define eb                           emplace_back
#define all(x)                       (x).begin(), (x).end()
#define sz(x)                        ((int)(x).size())
#define len(x)                       ((int)(x).length())
#define pii                          pair<int,int>
#define vi                            vector<int>
#define vd                           vector<double>
#define vs                            vector<string>
#define mp                          map<int,int>
#define ms(x,y)                     memset(x,y,sizeof(x))
#define ump                         unordered_map<int,int>

#define lb                            lower_bound
#define ub                            upper_bound
#define nx                            cout<<"\n";
#define inf                           9223372036854775807
#define PI                            3.14159265

const int MOD = 1e9 + 7;

inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a * b / gcd(a, b); }
inline int add(int a, int b) {    return ((a % MOD) + (b % MOD)) % MOD;}

inline int multi(int a, int b) {   return ((a % MOD) * (b % MOD)) % MOD;}

inline int sub(int a, int b) {       a %= MOD; b %= MOD; a -= b; if (a < 0) a += MOD; return a;}

inline int power(int a, int b) {       a %= MOD; int res = 1; while (b > 0) {if (b & 1) {res = multi(res, a);} a = multi(a, a); b >>= 1;}  return res;}

bool isPrime(int  x) {      if (x <= 4 || x % 2 == 0 || x % 3 == 0) {return x == 2 || x == 3;} for (int i = 5; i * i <= x; i += 6) {if (x % i == 0 || x % (i + 2) == 0) { return 0;}} return 1;}

vi fill()
{
	vi res(7, 0);
	string s = "1869";
	do {
		int num = 0;
		for (char c : s) {
			num = num * 10 + (c - '0');
		}
		res[num % 7] = num;
	} while (next_permutation(s.begin(), s.end()));
	return res;
}
int32_t main()
{
	fast ;
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	//1869
	vi out = fill();

	string s; cin >> s;
	int n = len(s);
	int c0 = 0;
	mp mop;
	for (int i = 0; i < n; i++) {
		if (s[i] == '0')c0++;
		else mop[s[i] - '0']++;
	}
	mop[1]--;
	mop[8]--;
	mop[6]--;
	mop[9]--;
	int rem = 0;
	string res = "";
	for (pii p : mop) {
		while (p.ss--) {
			res = res + to_string(p.ff);
			rem = (rem * 10 + p.ff) % 7;
		}
	}

	cout << res;
	if (rem == 0)cout << out[0];
	else if (rem == 1)cout << out[4];
	else if (rem == 2)cout << out[1];
	else if (rem == 3)cout << out[5];
	else if (rem == 4)cout << out[2];
	else if (rem == 5)cout << out[6];
	else cout << out[3];
	while (c0--)cout << 0;

}


#include<bits/stdc++.h>
using namespace std;
#define fast                         ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int                          long long int
#define double                       long double
#define ff                           first
#define ss                           second
#define pb                           push_back
#define eb                           emplace_back
#define all(x)                       (x).begin(), (x).end()
#define sz(x)                        ((int)(x).size())
#define len(x)                       ((int)(x).length())
#define pii                          pair<int,int>
#define vi                            vector<int>
#define vd                           vector<double>
#define vs                            vector<string>
#define mp                          map<int,int>
#define ms(x,y)                     memset(x,y,sizeof(x))
#define ump                         unordered_map<int,int>

#define lb                            lower_bound
#define ub                            upper_bound
#define nx                            cout<<"\n";
#define inf                           9223372036854775807
#define PI                            3.14159265

const int MOD = 1e9 + 7;

inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a * b / gcd(a, b); }
inline int add(int a, int b) {    return ((a % MOD) + (b % MOD)) % MOD;}

inline int multi(int a, int b) {   return ((a % MOD) * (b % MOD)) % MOD;}

inline int sub(int a, int b) {       a %= MOD; b %= MOD; a -= b; if (a < 0) a += MOD; return a;}

inline int power(int a, int b) {       a %= MOD; int res = 1; while (b > 0) {if (b & 1) {res = multi(res, a);} a = multi(a, a); b >>= 1;}  return res;}

bool isPrime(int  x) {      if (x <= 4 || x % 2 == 0 || x % 3 == 0) {return x == 2 || x == 3;} for (int i = 5; i * i <= x; i += 6) {if (x % i == 0 || x % (i + 2) == 0) { return 0;}} return 1;}

void fill(int rem)
{

	string s = "1869";
	do {
		int num = rem;
		for (char c : s) {
			num = num * 10 + (c - '0');
		}
		if (num % 7 == 0) {cout << s; return;}
	} while (next_permutation(s.begin(), s.end()));

}
int32_t main()
{
	fast ;
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	string s; cin >> s;
	int n = len(s);
	int c0 = 0;
	int c1 = 1, c6 = 1, c8 = 1, c9 = 1;
	int rem = 0;
	string res = "";
	for (int i = 0; i < n; i++) {
		if (s[i] == '0')c0++;
		else if (s[i] == '1' && c1)c1--;
		else if (s[i] == '6' && c6)c6--;
		else if (s[i] == '8' && c8)c8--;
		else if (s[i] == '9' && c9)c9--;
		else {
			res = res + s[i];
			rem = (rem * 10 + (int)(s[i] - '0') ) % 7;
		}
	}

	cout << res;
	fill(rem);
	while (c0 > 0) {cout << 0; c0--;}

}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

map<string,ll>mp;

int main()
{
    string s;
    ll i,j,n,m;
    cin>>s;
    ll cnt=0;
    ll len=s.size();
    ll temp=0;
    do{
            temp=0;
        for(i=0;i<len;i++){
            temp=temp*10+s[i]-'0';
            temp%=7;
        }
        //cout<<s<<" "<<temp<<endl;
        if(temp==0){
            cout<<s<<endl;
            return 0;
        }
    }while(next_permutation(s.begin(),s.end()));

    cout<<cnt<<endl;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

map<string,ll>mp;

int main()
{
    string s;
    string shajib;
    ll i,j,n,m;
    cin>>s;
    ll cnt=0;
    ll len=s.size();
    ll temp=0;

    for(i=0;i<len;i++){
        if(s[i]=='1') shajib.push_back(s[i]);
    }
        for(i=0;i<len;i++){
        if(s[i]=='0') shajib.push_back(s[i]);
    }
        for(i=0;i<len;i++){
        if(s[i]=='2') shajib.push_back(s[i]);
    }
        for(i=0;i<len;i++){
        if(s[i]=='3') shajib.push_back(s[i]);
    }
        for(i=0;i<len;i++){
        if(s[i]=='4') shajib.push_back(s[i]);
    }
            for(i=0;i<len;i++){
        if(s[i]=='5') shajib.push_back(s[i]);
    }
            for(i=0;i<len;i++){
        if(s[i]=='6') shajib.push_back(s[i]);
    }
            for(i=0;i<len;i++){
        if(s[i]=='7') shajib.push_back(s[i]);
    }
            for(i=0;i<len;i++){
        if(s[i]=='8') shajib.push_back(s[i]);
    }
            for(i=0;i<len;i++){
        if(s[i]=='9') shajib.push_back(s[i]);
    }
    s=shajib;
    do{
            temp=0;
        for(i=0;i<len;i++){
            temp=temp*10+s[i]-'0';
            temp%=7;
        }
        //cout<<s<<" "<<temp<<endl;
        if(temp==0){
            cout<<s<<endl;
            return 0;
        }
    }while(next_permutation(s.begin(),s.end()));

    cout<<cnt<<endl;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

map<string,ll>mp;

int main()
{
    string s;
    string shajib;
    ll i,j,n,m;
    cin>>s;
    ll cnt=0;
    ll len=s.size();
    ll temp=0;
    while(true){
            temp=0;
        for(i=0;i<len;i++){
            temp=temp*10+s[i]-'0';
            temp%=7;
        }
        //cout<<s<<" "<<temp<<endl;
        if(temp==0){
            cout<<s<<endl;
            return 0;
        }
        next_permutation(s.begin(),s.end());
    }

    cout<<cnt<<endl;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

map<string,ll>mp;

int main()
{
    string s;
    string shajib;
    ll i,j,n,m;
    cin>>s;
    ll cnt=0;
    ll len=s.size();
    ll temp=0;

    for(i=0;i<len;i++){
        if(s[i]=='1') shajib.push_back(s[i]);
    }
        for(i=0;i<len;i++){
        if(s[i]=='0') shajib.push_back(s[i]);
    }
        for(i=0;i<len;i++){
        if(s[i]=='6') shajib.push_back(s[i]);
    }
        for(i=0;i<len;i++){
        if(s[i]=='8') shajib.push_back(s[i]);
    }
        for(i=0;i<len;i++){
        if(s[i]=='9') shajib.push_back(s[i]);
    }
    s=shajib;
    do{
            temp=0;
        for(i=0;i<len;i++){
            temp=temp*10+s[i]-'0';
            temp%=7;
        }
        //cout<<s<<" "<<temp<<endl;
        if(temp==0){
            cout<<s<<endl;
            return 0;
        }
    }while(next_permutation(s.begin(),s.end()));

    cout<<cnt<<endl;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

map<string,ll>mp;

int main()
{
    string s;
    ll i,j,n,m;
    cin>>s;
    ll cnt=0;
    ll len=s.size();
    ll temp=0;
    do{
        for(i=0;i<len;i++){
            ll temp=temp*10+s[i]-'0';
            temp%=7;
        }
        if(temp==0){
            cout<<s<<endl;
            return 0;
        }
    }while(next_permutation(s.begin(),s.end()));

    cout<<cnt<<endl;
}


//yashverma

#include<bits/stdc++.h>
using namespace std;

#define rep(i,a,b) for(int i=a;i<b;i++)
#define F first
#define S second
#define pb push_back
#define sqr(a) (a)*(a)
#define Pi 3.141592653589793
#define MOD 1000000007
#define INF 2000000000   // 2x10^9
#define m3 1003
#define m5 100005
#define m6 1000006
// For i/o
#define sd1(a) scanf("%d",&a)
#define sd2(a,b) scanf("%d %d",&a,&b)
#define sd3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sf scanf
#define pf printf
// For debugging
#define deb(a) printf("deb%d\n",a)

typedef pair<int,int> ii;
typedef long long ll;
typedef vector<int> vi;

int main()
{
	char s[1000006];
	sf("%s",s);
	int con[8] = {1869, 1896, 1986, 1698, 6198, 1689, 1896};
	int len = strlen(s);
	int cnt0 = 0;
	int f1 = 0, f6 = 0,f8 = 0,f9 = 0;
	rep(i,0,len)
	{
		if(s[i] == '0')
		{
			cnt0 += 1;
			s[i] = 'A';
		}
		if(s[i] == '6' && f6 == 0)
		{
			s[i] = 'A';
			f6 = 1;
		}
		if(s[i] == '1' && f1 == 0)
		{
			s[i] = 'A';
			f1 = 1;
		}
		if(s[i] == '8' && f8 == 0)
		{
			s[i] = 'A';
			f8 = 1;
		}
		if(s[i] == '9' && f9 == 0)
		{
			s[i] = 'A';
			f9 = 1;
		}
	}
	sort(s,s+len);
	s[len - 4 - cnt0] = '\0';
	len = len - 4 - cnt0;
	int curr = 0;
	rep(i,0,len)
	{
		curr = curr*10 + s[i];
		curr = curr%7;
	}
	pf("%s",s);
	pf("%d",con[curr]);
	while(cnt0)
	{
		pf("0");
		cnt0 -= 1;
	}
	pf("\n");
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int N=1e6+10;
int n,m,cnt[10],ans[5];
char c;
inline void dfs(int x,int pos,int b){
	if (pos==5) {
		if (x==0){
			for (int i=1;i<=4;++i) putchar(ans[i]+48);
			for (int i=1;i<=cnt[0];++i) putchar(48);
			exit(0);
		}
		return;
	}
	if (!(b&1)) ans[pos]=1,dfs((x*10+1)%7,pos+1,b|1);
	if (!(b&2)) ans[pos]=6,dfs((x*10+6)%7,pos+1,b|2);
	if (!(b&4)) ans[pos]=8,dfs((x*10+8)%7,pos+1,b|4);
	if (!(b&8)) ans[pos]=9,dfs((x*10+9)%7,pos+1,b|8);
}
int main(){
	while (c=getchar(),c>='0'&&c<='9') ++cnt[c-48];
	--cnt[1];--cnt[6];--cnt[8];--cnt[9];
	for (int i=0;i<=9;++i) printf("%d ",cnt[i]);
	int res=0,i=1;
	while (!cnt[i]&&i<=9) ++i;
	while (i<=9){
		res=(res*10+i)%7;
		putchar(i+48);
		--cnt[i];
		while (!cnt[i]&&i<=9) ++i;
	}
	dfs(res,1,0);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int N=1e6+10;
int n,m,rest,cnt[10],ans[5];
char s[N];
inline void dfs(int x,int pos,int b){
	if (pos==5) {
		if (x==0){
			for (int i=1;i<=4;++i) putchar(ans[i]+48);
			for (int i=1;i<=cnt[0];++i) putchar(48);
			exit(0);
		}
		return;
	}
	if (!(b&1)) ans[pos]=1,dfs((x*10+1)%7,pos+1,b|1);
	if (!(b&2)) ans[pos]=6,dfs((x*10+6)%7,pos+1,b|2);
	if (!(b&4)) ans[pos]=8,dfs((x*10+8)%7,pos+1,b|4);
	if (!(b&8)) ans[pos]=9,dfs((x*10+9)%7,pos+1,b|8);
}
int main(){
	scanf("%s",s);
	for (int i=0;s[i];++i) ++cnt[s[i]-48];
	--cnt[1];--cnt[6];--cnt[8];--cnt[9];
	int pos=1;
	while (cnt[pos]<=0 && pos<=9) ++pos,printf("%d   cao  ni ma \n",pos);;

	while (pos<=9){
		rest=(rest*10+pos)%7;
		putchar(pos+48);
		--cnt[pos];
		while (cnt[pos]<=0 && pos<=9) ++pos;
	}
	dfs(rest,1,0);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int N=1e6+10;
int n,m,cnt[10],ans[5];
char c;
inline void dfs(int x,int pos,int b){
	if (pos==5) {
		if (x==0){
			for (int i=1;i<=4;++i) putchar(ans[i]+48);
			for (int i=1;i<=cnt[0];++i) putchar(48);
			exit(0);
		}
		return;
	}
	if (!(b&1)) ans[pos]=1,dfs((x*10+1)%7,pos+1,b|1);
	if (!(b&2)) ans[pos]=6,dfs((x*10+6)%7,pos+1,b|2);
	if (!(b&4)) ans[pos]=8,dfs((x*10+8)%7,pos+1,b|4);
	if (!(b&8)) ans[pos]=9,dfs((x*10+9)%7,pos+1,b|8);
}
int main(){
	while (c=getchar(),c>='0'&&c<='9') ++cnt[c-48];
	--cnt[1];--cnt[6];--cnt[8];--cnt[9];
	int res=0,i=1;
	while (cnt[i]==0&&i<=9) ++i;
	while (i<=9){
		res=(res*10+i)%7;
		putchar(i+48);
		--cnt[i];
		while (!cnt[i]&&i<=9) ++i;
	}
	printf("!233!");
	dfs(res,1,0);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int N=1e6+10;
int n,m,rest,cnt[10],ans[5];
char s[N];
inline void dfs(int x,int pos,int b){
	if (pos==5) {
		if (x==0){
			for (int i=1;i<=4;++i) putchar(ans[i]+48);
			for (int i=1;i<=cnt[0];++i) putchar(48);
			exit(0);
		}
		return;
	}
	if (!(b&1)) ans[pos]=1,dfs((x*10+1)%7,pos+1,b|1);
	if (!(b&2)) ans[pos]=6,dfs((x*10+6)%7,pos+1,b|2);
	if (!(b&4)) ans[pos]=8,dfs((x*10+8)%7,pos+1,b|4);
	if (!(b&8)) ans[pos]=9,dfs((x*10+9)%7,pos+1,b|8);
}
int main(){
	scanf("%s",s);
	for (int i=0;s[i];++i) ++cnt[s[i]-48];
	--cnt[1];--cnt[6];--cnt[8];--cnt[9];
	int pos=1;
	while (cnt[pos]<=0 && pos<=9) ++pos,printf("%d  *",pos);

	while (pos<=9){
		rest=(rest*10+pos)%7;
		putchar(pos+48);
		--cnt[pos];
		while (cnt[pos]<=0 && pos<=9) ++pos;
	}
	dfs(rest,1,0);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int N=1e6+10;
int n,m,rest,cnt[10],ans[5];
char s[N];
inline void dfs(int x,int pos,int b){
	if (pos==5) {
		if (x==0){
			for (int i=1;i<=4;++i) putchar(ans[i]+48);
			for (int i=1;i<=cnt[0];++i) putchar(48);
			exit(0);
		}
		return;
	}
	if (!(b&1)) ans[pos]=1,dfs((x*10+1)%7,pos+1,b|1);
	if (!(b&2)) ans[pos]=6,dfs((x*10+6)%7,pos+1,b|2);
	if (!(b&4)) ans[pos]=8,dfs((x*10+8)%7,pos+1,b|4);
	if (!(b&8)) ans[pos]=9,dfs((x*10+9)%7,pos+1,b|8);
}
int main(){
	cin>>s;
	for (int i=0;s[i];++i) ++cnt[s[i]-48];
	--cnt[1];--cnt[6];--cnt[8];--cnt[9];
	int pos=1;
	while (cnt[pos]<=0&&pos<=9) ++pos;
	while (pos<=9){
		rest=(rest*10+pos)%7;
		putchar(pos+48);
		--cnt[pos];
		while (cnt[pos]<=0&&pos<=9) ++pos;
	}
	dfs(rest,1,0);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int N=1e6+10;
int n,m,rest,cnt[10],ans[5];
char s[N];
inline void dfs(int x,int pos,int b){
	if (pos==5) {
		if (x==0){
			for (int i=1;i<=4;++i) putchar(ans[i]+48);
			for (int i=1;i<=cnt[0];++i) putchar(48);
			exit(0);
		}
		return;
	}
	if (!(b&1)) ans[pos]=1,dfs((x*10+1)%7,pos+1,b|1);
	if (!(b&2)) ans[pos]=6,dfs((x*10+6)%7,pos+1,b|2);
	if (!(b&4)) ans[pos]=8,dfs((x*10+8)%7,pos+1,b|4);
	if (!(b&8)) ans[pos]=9,dfs((x*10+9)%7,pos+1,b|8);
}
int main(){
	scanf("%s",s);
	for (int i=0;s[i];++i) ++cnt[s[i]-48];
	--cnt[1];--cnt[6];--cnt[8];--cnt[9];
	int pos=1;
	while (cnt[pos]<=0 && pos<=9) ++pos;
	printf("%d   cao  ni ma \n",pos);
	while (pos<=9){
		rest=(rest*10+pos)%7;
		putchar(pos+48);
		--cnt[pos];
		while (cnt[pos]<=0 && pos<=9) ++pos;
	}
	dfs(rest,1,0);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int N=1e6+10;
int n,m,rest,cnt[10],ans[5];
char c;
inline void dfs(int x,int pos,int b){
	if (pos==5) {
		if (x==0){
			for (int i=1;i<=4;++i) putchar(ans[i]+48);
			for (int i=1;i<=cnt[0];++i) putchar(48);
			exit(0);
		}
		return;
	}
	if (!(b&1)) ans[pos]=1,dfs((x*10+1)%7,pos+1,b|1);
	if (!(b&2)) ans[pos]=6,dfs((x*10+6)%7,pos+1,b|2);
	if (!(b&4)) ans[pos]=8,dfs((x*10+8)%7,pos+1,b|4);
	if (!(b&8)) ans[pos]=9,dfs((x*10+9)%7,pos+1,b|8);
}
int main(){
	while (c=getchar(),c>='0'&&c<='9') ++cnt[c-48];
	--cnt[1];--cnt[6];--cnt[8];--cnt[9];
	int pos=1;
	while (cnt[pos]<=0&&pos<=9) ++pos;
	while (pos<=9){
		rest=(rest*10+pos)%7;
		putchar(pos+48);
		--cnt[pos];
		while (cnt[pos]<=0&&pos<=9) ++pos;
	}
	dfs(rest,1,0);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int N=1e6+10;
int n,m,rest,cnt[11],ans[5];
char s[N];
inline void dfs(int x,int pos,int b){
	if (pos==5) {
		if (x==0){
			for (int i=1;i<=4;++i) putchar(ans[i]+48);
			for (int i=1;i<=cnt[0];++i) putchar(48);
			exit(0);
		}
		return;
	}
	if (!(b&1)) ans[pos]=1,dfs((x*10+1)%7,pos+1,b|1);
	if (!(b&2)) ans[pos]=6,dfs((x*10+6)%7,pos+1,b|2);
	if (!(b&4)) ans[pos]=8,dfs((x*10+8)%7,pos+1,b|4);
	if (!(b&8)) ans[pos]=9,dfs((x*10+9)%7,pos+1,b|8);
}
int main(){
	scanf("%s",s);
	for (int i=0;s[i];++i) ++cnt[s[i]-48];
	--cnt[1];--cnt[6];--cnt[8];--cnt[9];
	int pos=1;
	while (cnt[pos]<=0 && pos<=9) ++pos;

	while (pos<=9){
		rest=(rest*10+pos)%7;
		putchar(pos+48);
		--cnt[pos];
		while (cnt[pos]<=0 && pos<=9) ++pos;
	}
	dfs(rest,1,0);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int N=1e6+10;
int n,m,cnt[10],ans[5];
char c;
inline void dfs(int x,int pos,int b){
	if (pos==5) {
		if (x==0){
			for (int i=1;i<=4;++i) putchar(ans[i]+48);
			for (int i=1;i<=cnt[0];++i) putchar(48);
			exit(0);
		}
		return;
	}
	if (!(b&1)) ans[pos]=1,dfs((x*10+1)%7,pos+1,b|1);
	if (!(b&2)) ans[pos]=6,dfs((x*10+6)%7,pos+1,b|2);
	if (!(b&4)) ans[pos]=8,dfs((x*10+8)%7,pos+1,b|4);
	if (!(b&8)) ans[pos]=9,dfs((x*10+9)%7,pos+1,b|8);
}
int main(){
	while (c=getchar(),c>='0'&&c<='9') ++cnt[c-48];
	--cnt[1];--cnt[6];--cnt[8];--cnt[9];
	int res=0,i=1;
	while (!cnt[i]&&i<=9) ++i;
	while (i<=9){
		res=(res*10+i)%7;
		putchar(i+48);
		--cnt[i];
		while (!cnt[i]&&i<=9) ++i;
	}
	dfs(res,1,0);
	return 0;
}


import java.util.*;
public class div469{
	public static void main(String s[]){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		ArrayList<Integer> al1 = new ArrayList<>();
		ArrayList<Integer> al2 = new ArrayList<>();
		for(int i=0;i<n;i++)
			al1.add(sc.nextInt());

		for(int i=0;i<m;i++)
			al2.add(sc.nextInt());

		int a=0;
		int b=0;
		long sum1=0;
		long sum2=0;
		long count=0;
		HashMap<Integer,Integer> hm1 = new HashMap<>();
		HashMap<Integer,Integer> hm2 = new HashMap<>();
		while(a<n && b<m){
			if(!hm1.containsKey(a))
			sum1+=al1.get(a);
			if(!hm2.containsKey(b))
			sum2+=al2.get(b);
			hm1.put(a,0);
			hm2.put(b,0);
			if(sum1==sum2){
			count++;
			sum1=0;
			sum2=0;
			a++;
			b++;
			}
			if(sum1<sum2){
				a++;
			}
			if(sum2<sum1){
				b++;
			}
		}
		System.out.println(count);
	}
}


// package problemB;

import java.util.*;
import java.io.*;

public class TestClass
{
	static BufferedReader br;
	static StringTokenizer st;
	static PrintWriter out;

	public static void main(String[] args)
	{
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(new OutputStreamWriter(System.out));
		solve();
		out.close();
	}

	public static void solve()
	{
		int n = nextInt();
		int m = nextInt();
		int[] first = new int[n];
		int[] second = new int[m];

		for(int i = 0; i < n; i++)
			first[i] = nextInt();
		for(int i = 0; i < m; i++)
			second[i] = nextInt();

		int[] prefixFirst = new int[n];
		prefixFirst[0] = first[0];

		int[] prefixSecond = new int[m];
		prefixSecond[0] = second[0];

		for(int i = 1; i < n; i++) {
			prefixFirst[i] = prefixFirst[i - 1] + first[i];
		}

		for(int i = 1; i < m; i++) {
			prefixSecond[i] = prefixSecond[i - 1] + second[i];
		}

		int answer = 0;
		int lastIndex = 0;
		for(int i = 0; i < n; i++) {
			int indexInSecond = binarySearch(prefixFirst[i], prefixSecond, lastIndex);
			if(indexInSecond != -1) {
				lastIndex = indexInSecond;
				answer += 1;
			}
		}
		out.println(answer);
	}

	private static int binarySearch(int i, int[] arr, int low) {
		int high = arr.length - 1;
		while(low <= high) {
			int mid = (low + high) / 2;
			if(arr[mid] == i)
				return mid;
			if(arr[mid] < i) {
				low = mid + 1;
			}
			else {
				high = mid - 1;
			}
		}
		return -1;
	}

	static String next()
	{
		while (st == null || !st.hasMoreElements()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return st.nextToken();
	}

	static int nextInt()
	{
		return Integer.parseInt(next());
	}

	static long nextLong()
	{
		return Long.parseLong(next());
	}

	static String nextLine()
	{
		String str = "";
		try {
			str = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return str;
	}
}


// package problemB;

import java.util.*;
import java.io.*;

public class TestClass
{
	static BufferedReader br;
	static StringTokenizer st;
	static PrintWriter out;

	public static void main(String[] args)
	{
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(new OutputStreamWriter(System.out));
		solve();
		out.close();
	}

	public static void solve()
	{
		int n = nextInt();
		int m = nextInt();
		int[] first = new int[n];
		int[] second = new int[m];
		for(int i = 0; i < n; i++)
			first[i] = nextInt();
		for(int i = 0; i < m; i++)
			second[i] = nextInt();
		int[] prefixFirst = new int[n];
		prefixFirst[0] = first[0];
		int[] prefixSecond = new int[m];
		prefixSecond[0] = second[0];
		for(int i = 1; i < n; i++) {
			prefixFirst[i] = prefixFirst[i - 1] + first[i];
		}
		for(int i = 1; i < m; i++) {
			prefixSecond[i] = prefixSecond[i - 1] + second[i];
		}
		int answer = 0;
		for(int i = 0; i < n; i++) {
			int indexInSecond = binarySearch(prefixFirst[i], prefixSecond);
			if(indexInSecond != -1)
				answer += 1;
		}
		out.println(answer);
	}

	private static int binarySearch(int i, int[] arr) {
		int low = 0;
		int high = arr.length - 1;
		while(low <= high) {
			int mid = (low + high) / 2;
			if(arr[mid] == i)
				return mid;
			if(arr[mid] < i) {
				low = mid + 1;
			}
			else {
				high = mid - 1;
			}
		}
		return -1;
	}

	static String next()
	{
		while (st == null || !st.hasMoreElements()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return st.nextToken();
	}

	static int nextInt()
	{
		return Integer.parseInt(next());
	}

	static long nextLong()
	{
		return Long.parseLong(next());
	}

	static String nextLine()
	{
		String str = "";
		try {
			str = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return str;
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] a = new int[n + 1];
            int[] b = new int[m + 1];
            int res = 0;
            int ia = 1;
            int ib = 1;

            for (int i = 0; i < n; i++) a[i] = in.nextInt();
            for (int i = 0; i < m; i++) b[i] = in.nextInt();
            int top = a[0];
            int bottom = b[0];
            while (ia <= n || ib <= m) {
                if (top == bottom) {
                    res++;
                    top = a[ia++];
                    bottom = b[ib++];
                } else if (top < bottom) {
                    top += a[ia];
                    ia++;
                } else {
                    bottom += b[ib];
                    ib++;
                }
            }
            out.println(res);
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            List<Integer> a = new ArrayList<>();
            List<Integer> b = new ArrayList<>();
            int res = 0;

            for (int i = 0; i < n; i++) a.add(in.nextInt());
            for (int i = 0; i < m; i++) b.add(in.nextInt());
//        if (a.size() == 1 && b.size() == 1){
//            if (a.get(0) == b.get(0)){
//                out.println(1);
//                return;
//            }else{
//                out.println(0);
//                return;
//            }
//        }
            while (!b.isEmpty() && !a.isEmpty()) {
                if (a.get(0).equals(b.get(0))) {
                    a.remove(0);
                    b.remove(0);
                    res++;
                } else if (a.get(0) > b.get(0)) {
                    b.set(0, (b.get(0) + b.get(1)));
                    b.remove(1);
                } else {
                    a.set(0, (a.get(0) + a.get(1)));
                    a.remove(1);
                }
            }
            out.println(res);
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            List<Integer> a = new ArrayList<>();
            List<Integer> b = new ArrayList<>();
            int res = 0;

            for (int i = 0; i < n; i++) a.add(in.nextInt());
            for (int i = 0; i < m; i++) b.add(in.nextInt());
            while (!b.isEmpty() || !a.isEmpty()) {
                if (a.get(0) == b.get(0)) {
                    a.remove(0);
                    b.remove(0);
                    res++;
                } else if (a.get(0) > b.get(0)) {
                    b.set(0, (b.get(0) + b.get(1)));
                    b.remove(1);
                } else {
                    a.set(0, (a.get(0) + a.get(1)));
                    a.remove(1);
                }
            }
            out.println(res);
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


package codes;

import java.util.Scanner;

public class IntercepterMessage {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int m = scanner.nextInt();

        int[] a = new int[n + 1];
        int[] b = new int[m + 1];

        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }

        for (int i = 0; i < m; i++) {
            b[i] = scanner.nextInt();
        }

        int i = 0, j = 0, suma = a[0], sumb = b[0], br = 0;
        while(i < n && j < m) {
//            System.out.println(String.format("%d %d %d %d", i, j , suma, sumb));
            if (suma == sumb) {
                br ++;
                suma = sumb = 0;
                i++; j++;
                suma += a[i];
                sumb += b[j];
            }
            else if (suma > sumb) {
                j++;
                sumb += b[j];
            }
            else {
                i++;
                suma += a[i];
            }
        }

        System.out.println(br);
    }
}


import java.util.Scanner;

public class IntercepterMessage {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int m = scanner.nextInt();

        int[] a = new int[n + 1];
        int[] b = new int[m + 1];

        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }

        for (int i = 0; i < m; i++) {
            b[i] = scanner.nextInt();
        }

        int i = 0, j = 0, suma = a[0], sumb = b[0], br = 0;
        while(i < n && j < m) {
//            System.out.println(String.format("%d %d %d %d", i, j , suma, sumb));
            if (suma == sumb) {
                br ++;
                suma = sumb = 0;
                i++; j++;
                suma += a[i];
                sumb += b[j];
            }
            else if (suma > sumb) {
                j++;
                sumb += b[j];
            }
            else {
                i++;
                suma += a[i];
            }
        }

        System.out.println(br);
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author caoash
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        BInterceptedMessage solver = new BInterceptedMessage();
        solver.solve(1, in, out);
        out.close();
    }

    static class BInterceptedMessage {
        public void solve(int testNumber, FastScanner br, PrintWriter pw) {
            int n = br.nextInt();
            int m = br.nextInt();
            int[] a = br.nextIntArray(n);
            int[] b = br.nextIntArray(m);
            int sum1 = a[0];
            int sum2 = b[0];
            int p1 = 0;
            int p2 = 0;
            int p3 = 0;
            int p4 = 0;
            int ans = 0;
            while (p1 <= p2 && p3 <= p4 && p2 < n && p4 < m) {
//            System.out.println(p1 + " " + p2 + " " + p3 + " " + p4);
                if (sum1 > sum2) {
                    p4++;
                    if (p4 == m) {
                        pw.println(-1);
                        return;
                    }
                    sum2 += b[p4];
                } else if (sum2 > sum1) {
                    p2++;
                    if (p2 == n) {
                        pw.println(-1);
                        return;
                    }
                    sum1 += a[p2];
                } else {
                    ans++;
                    p2++;
                    p4++;
                    p1 = p2;
                    p3 = p4;
                    if (p2 < n && p4 < m) {
                        sum1 = a[p1];
                        sum2 = b[p3];
                    } else {
                        break;
                    }
                }
            }
            pw.println(ans);
            pw.close();
        }

    }

    static class FastScanner {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private FastScanner.SpaceCharFilter filter;

        public FastScanner(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public int[] nextIntArray(int n) {
            int[] array = new int[n];
            for (int i = 0; i < n; ++i) array[i] = nextInt();
            return array;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


    import java.io.*;
    import java.util.*;

    public class Main {

    public static void main(String[] args) throws IOException{
        //FastScanner in = new FastScanner(new FileInputStream(new File("phi.in")));
        //PrintWriter out = new PrintWriter(new File("output.txt"));
        PrintWriter out = new PrintWriter(System.out);
        FastScanner in = new FastScanner(System.in);
        new Main().solveB(in, out);
        out.close();
        System.exit(0);
    }

    /* begin() */

    void solveA(FastScanner in, PrintWriter out){
    }

    void solveB(FastScanner in, PrintWriter out){
        int n = in.nextInt();
        int m = in.nextInt();
        long F1[] = new long[n+1];
        long F2[] = new long[m+1];
        for(int i=1; i<=n; i++) F1[i] = in.nextLong() + F1[i-1];
        for(int i=1; i<=m; i++) F2[i] = in.nextLong() + F2[i-1];
        int l1 = 1, l2 = 1, r1 = 1, r2 = 1;
        int ans = 0;
        while(r1 <= n && r2<=m){
            if(F1[r1] - F1[l1 - 1] == F2[r2] - F2[l2 - 1]){
                ans++;
                l1++; r1++; r2++; l2++;
            }
            else if(F1[r1] - F1[l1 - 1] > F2[r2] - F2[l2 - 1]) r2++;
            else if(F1[r2] - F1[l1 - 1] < F2[r2] - F2[l2 - 1]) r1++;
        }
        out.println(ans);
    }

    void solveC(FastScanner in, PrintWriter out){
    }

    void solveD(FastScanner in, PrintWriter out){
    }

    /* end() */

    private static class FastScanner {

        private final InputStream stream;
        private final byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public FastScanner(InputStream stream) {
            this.stream = stream;
        }

        int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        boolean isEndline(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public String nextLine() {
            int c = read();
            while (isEndline(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndline(c));
            return res.toString();
        }
    }
}


    import java.io.*;
    import java.util.*;

    public class Main {

    public static void main(String[] args) throws IOException{
        //FastScanner in = new FastScanner(new FileInputStream(new File("phi.in")));
        //PrintWriter out = new PrintWriter(new File("output.txt"));
        PrintWriter out = new PrintWriter(System.out);
        FastScanner in = new FastScanner(System.in);
        new Main().solveB(in, out);
        out.close();
        System.exit(0);
    }

    /* begin() */

    void solveA(FastScanner in, PrintWriter out){
    }

    void solveB(FastScanner in, PrintWriter out){
        int n = in.nextInt();
        int m = in.nextInt();
        int F1[] = new int[n+1];
        boolean a[] = new boolean[(int)1e6+1];
        for(int i=1; i<=n; i++) {
            F1[i] = in.nextInt() + F1[i-1];
            a[F1[i]] = true;
        }
        int x = 0;
        int ans = 0;
        for(int i=0; i<m; i++){
            x += in.nextInt();
            if(a[x]== true) ans++;
        }
        out.println(ans);
    }

    void solveC(FastScanner in, PrintWriter out){
    }

    void solveD(FastScanner in, PrintWriter out){
    }

    /* end() */

    private static class FastScanner {

        private final InputStream stream;
        private final byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public FastScanner(InputStream stream) {
            this.stream = stream;
        }

        int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        boolean isEndline(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public String nextLine() {
            int c = read();
            while (isEndline(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndline(c));
            return res.toString();
        }
    }
}


        import java.io.*;
    import java.util.*;

    public class Main {

    public static void main(String[] args) throws IOException{
        //FastScanner in = new FastScanner(new FileInputStream(new File("phi.in")));
        //PrintWriter out = new PrintWriter(new File("output.txt"));
        PrintWriter out = new PrintWriter(System.out);
        FastScanner in = new FastScanner(System.in);
        new Main().solveB(in, out);
        out.close();
        System.exit(0);
    }

    /* begin() */

    void solveA(FastScanner in, PrintWriter out){
    }

    void solveB(FastScanner in, PrintWriter out){
        int n = in.nextInt();
        int m = in.nextInt();
        long F1[] = new long[n+1];
        long F2[] = new long[m+1];
        for(int i=1; i<=n; i++) F1[i] = in.nextLong() + F1[i-1];
        for(int i=1; i<=m; i++) F2[i] = in.nextLong() + F2[i-1];
        int l1 = 1, l2 = 1, r1 = 1, r2 = 1;
        int ans = 0;
        while(r1 <= n && r2<=m){
            if(F1[r1] - F1[l1 - 1] == F2[r2] - F2[l2 - 1]){
                ans++;
                l1++; r1++; r2++; l2++;
            }
            else if(F1[r1] - F1[l1 - 1] > F2[r2] - F2[l2 - 1]) r2++;
            else if(F1[r2] - F1[l1 - 1] < F2[r2] - F2[l2 - 1]) r1++;
        }
        out.println(ans);
    }

    void solveC(FastScanner in, PrintWriter out){
    }

    void solveD(FastScanner in, PrintWriter out){
    }

    /* end() */

    private static class FastScanner {

        private final InputStream stream;
        private final byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public FastScanner(InputStream stream) {
            this.stream = stream;
        }

        int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        boolean isEndline(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public String nextLine() {
            int c = read();
            while (isEndline(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndline(c));
            return res.toString();
        }
    }
}


import java.util.*;
import java.io.*;

public class B950{
	public static void main(String args[])throws IOException{
		Scanner sc=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		PrintWriter pw=new PrintWriter(System.out);
		int n=sc.nextInt();
		int m=sc.nextInt();
		int arr[]=new int[n];
		int brr[]=new int[m];
		for(int i=0;i<n;i++)
			arr[i]=sc.nextInt();
		for(int j=0;j<m;j++)
			brr[j]=sc.nextInt();
		int sum=0;
		int i=1,j=1;
		int asum=arr[0],bsum=brr[0];
		while(i<n||j<m){
			if(asum<bsum)
				asum+=arr[i++];
			else if(bsum<asum)
				bsum+=brr[j++];
			else{
				sum++;
				asum=arr[i++];
				bsum=brr[j++];
			}
		}
		pw.print(sum+1);
		pw.close();
	}
}


//package CodeForces;

/*In the name of Allah the Most Merciful.
 * Author
 * Md. Toufiqul Islam
 * Dept. Of CSE
 * Ahsanullah University Of Science And Technology
*/
import java.util.*;
import java.math.*;
import java.io.*;
public class R469B {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int x[] = new int[n];
        int y[] = new int[m];
        int ans=0;
        for(int i=0;i<n;i++){
            x[i] = sc.nextInt();
        }
        for(int i=0;i<m;i++){
            y[i] = sc.nextInt();
        }
        int nopx=0,nopy=0, sumx=0, sumy=0;

        while(nopx!=n || nopy!=m){
            if(sumx==0 && sumy==0){
                sumx=x[nopx++];
                sumy = y[nopy++];
            }
            if(sumx<sumy){
                sumx+=x[nopx++];
            }
            if(sumx>sumy){
                sumy+=y[nopy++];
            }
            if(sumx!=0 && sumy!=0 && sumx==sumy){
                ans++;

                sumx=0;
                sumy=0;
            }

        }
        System.out.println(ans);

    }

    public static int getCOunt(int[] list1,int[] list2,int n,int m){
        int counter =0;

        int i=0;
        int j =0;
        long sum1 =0,sum2=0;

        while (i!=n || j!=m){

            if(sum1==0 && sum2==0){

                    sum1 = list1[(i++)];

                    sum2 = list2[(j++)];

            }

            if(sum1<sum2){

                    sum1 += list1[(i++)];

            }
            if(sum2<sum1){

                    sum1 += list2[(j++)];

            }

            if(  sum1!=0 && sum2!=0 && sum1== sum2){
                counter++;
                sum1 =0;
                sum2 = 0;
            }
        }

        return counter;

    }
     public static class InputReader {
             private boolean finished = false;

             private InputStream stream;
             private byte[] buf = new byte[1024];
             private int curChar;
             private int numChars;
             private SpaceCharFilter filter;

             public InputReader(InputStream stream) {
                 this.stream = stream;
             }

             public int read() {
                 if (numChars == -1) {
                     throw new InputMismatchException();
                 }
                 if (curChar >= numChars) {
                     curChar = 0;
                     try {
                         numChars = stream.read(buf);
                     } catch (IOException e) {
                         throw new InputMismatchException();
                     }
                     if (numChars <= 0) {
                         return -1;
                     }
                 }
                 return buf[curChar++];
             }

             public int peek() {
                 if (numChars == -1) {
                     return -1;
                 }
                 if (curChar >= numChars) {
                     curChar = 0;
                     try {
                         numChars = stream.read(buf);
                     } catch (IOException e) {
                         return -1;
                     }
                     if (numChars <= 0) {
                         return -1;
                     }
                 }
                 return buf[curChar];
             }

             public int nextInt() {
                 int c = read();
                 while (isSpaceChar(c)) {
                     c = read();
                 }
                 int sgn = 1;
                 if (c == '-') {
                     sgn = -1;
                     c = read();
                 }
                 int res = 0;
                 do {
                     if (c < '0' || c > '9') {
                         throw new InputMismatchException();
                     }
                     res *= 10;
                     res += c - '0';
                     c = read();
                 } while (!isSpaceChar(c));
                 return res * sgn;
             }

             public long nextLong() {
                 int c = read();
                 while (isSpaceChar(c)) {
                     c = read();
                 }
                 int sgn = 1;
                 if (c == '-') {
                     sgn = -1;
                     c = read();
                 }
                 long res = 0;
                 do {
                     if (c < '0' || c > '9') {
                         throw new InputMismatchException();
                     }
                     res *= 10;
                     res += c - '0';
                     c = read();
                 } while (!isSpaceChar(c));
                 return res * sgn;
             }

             public String nextString() {
                 int c = read();
                 while (isSpaceChar(c)) {
                     c = read();
                 }
                 StringBuilder res = new StringBuilder();
                 do {
                     if (Character.isValidCodePoint(c)) {
                         res.appendCodePoint(c);
                     }
                     c = read();
                 } while (!isSpaceChar(c));
                 return res.toString();
             }

             public boolean isSpaceChar(int c) {
                 if (filter != null) {
                     return filter.isSpaceChar(c);
                 }
                 return isWhitespace(c);
             }

             public static boolean isWhitespace(int c) {
                 return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
             }

             private String readLine0() {
                 StringBuilder buf = new StringBuilder();
                 int c = read();
                 while (c != '\n' && c != -1) {
                     if (c != '\r') {
                         buf.appendCodePoint(c);
                     }
                     c = read();
                 }
                 return buf.toString();
             }

             public String readLine() {
                 String s = readLine0();
                 while (s.trim().length() == 0) {
                     s = readLine0();
                 }
                 return s;
             }

             public String readLine(boolean ignoreEmptyLines) {
                 if (ignoreEmptyLines) {
                     return readLine();
                 } else {
                     return readLine0();
                 }
             }

             public BigInteger readBigInteger() {
                 try {
                     return new BigInteger(nextString());
                 } catch (NumberFormatException e) {
                     throw new InputMismatchException();
                 }
             }

             public char nextCharacter() {
                 int c = read();
                 while (isSpaceChar(c)) {
                     c = read();
                 }
                 return (char) c;
             }

             public double nextDouble() {
                 int c = read();
                 while (isSpaceChar(c)) {
                     c = read();
                 }
                 int sgn = 1;
                 if (c == '-') {
                     sgn = -1;
                     c = read();
                 }
                 double res = 0;
                 while (!isSpaceChar(c) && c != '.') {
                     if (c == 'e' || c == 'E') {
                         return res * Math.pow(10, nextInt());
                     }
                     if (c < '0' || c > '9') {
                         throw new InputMismatchException();
                     }
                     res *= 10;
                     res += c - '0';
                     c = read();
                 }
                 if (c == '.') {
                     c = read();
                     double m = 1;
                     while (!isSpaceChar(c)) {
                         if (c == 'e' || c == 'E') {
                             return res * Math.pow(10, nextInt());
                         }
                         if (c < '0' || c > '9') {
                             throw new InputMismatchException();
                         }
                         m /= 10;
                         res += (c - '0') * m;
                         c = read();
                     }
                 }
                 return res * sgn;
             }

             public boolean isExhausted() {
                 int value;
                 while (isSpaceChar(value = peek()) && value != -1) {
                     read();
                 }
                 return value == -1;
             }

             public String next() {
                 return nextString();
             }

             public SpaceCharFilter getFilter() {
                 return filter;
             }

             public void setFilter(SpaceCharFilter filter) {
                 this.filter = filter;
             }

             public interface SpaceCharFilter {
                 public boolean isSpaceChar(int ch);
             }
             public int[] nextIntArray(int n){
                 int[] array=new int[n];
                 for(int i=0;i<n;++i)array[i]=nextInt();
                 return array;
             }
             public int[] nextSortedIntArray(int n){
                 int array[]=nextIntArray(n);
                 Arrays.sort(array);
                 return array;
             }
             public int[] nextSumIntArray(int n){
                 int[] array=new int[n];
                 array[0]=nextInt();
                 for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt();
                 return array;
             }
             public long[] nextLongArray(int n){
                 long[] array=new long[n];
                 for(int i=0;i<n;++i)array[i]=nextLong();
                 return array;
             }
             public long[] nextSumLongArray(int n){
                 long[] array=new long[n];
                 array[0]=nextInt();
                 for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt();
                 return array;
             }
             public long[] nextSortedLongArray(int n){
                 long array[]=nextLongArray(n);
                 Arrays.sort(array);
                 return array;
             }
         }
      public static class OutputWriter {
              private final PrintWriter writer;

              public OutputWriter(OutputStream outputStream) {
                  writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
              }

              public OutputWriter(Writer writer) {
                  this.writer = new PrintWriter(writer);
              }

              public void print(char[] array) {
                  writer.print(array);
              }

              public void print(Object... objects) {
                  for (int i = 0; i < objects.length; i++) {
                      if (i != 0) {
                          writer.print(' ');
                      }
                      writer.print(objects[i]);
                  }
              }

              public void print(int[] array) {
                  for (int i = 0; i < array.length; i++) {
                      if (i != 0) {
                          writer.print(' ');
                      }
                      writer.print(array[i]);
                  }
              }

              public void print(double[] array) {
                  for (int i = 0; i < array.length; i++) {
                      if (i != 0) {
                          writer.print(' ');
                      }
                      writer.print(array[i]);
                  }
              }

              public void print(long[] array) {
                  for (int i = 0; i < array.length; i++) {
                      if (i != 0) {
                          writer.print(' ');
                      }
                      writer.print(array[i]);
                  }
              }

              public void println(int[] array) {
                  print(array);
                  writer.println();
              }

              public void println(double[] array) {
                  print(array);
                  writer.println();
              }

              public void println(long[] array) {
                  print(array);
                  writer.println();
              }

              public void println() {
                  writer.println();
              }

              public void println(Object... objects) {
                  print(objects);
                  writer.println();
              }

              public void print(char i) {
                  writer.print(i);
              }

              public void println(char i) {
                  writer.println(i);
              }

              public void println(char[] array) {
                  writer.println(array);
              }

              public void printf(String format, Object... objects) {
                  writer.printf(format, objects);
              }

              public void close() {
                  writer.close();
              }

              public void flush() {
                  writer.flush();
              }

              public void print(long i) {
                  writer.print(i);
              }

              public void println(long i) {
                  writer.println(i);
              }

              public void print(int i) {
                  writer.print(i);
              }

              public void println(int i) {
                  writer.println(i);
              }

              public void separateLines(int[] array) {
                  for (int i : array) {
                      println(i);
                  }
              }
          }

}