#include <bits/stdc++.h>
#include <chrono>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/*............................................................................*/ 
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
/* find_by_order(x) give iterator of the index x 
   order_by_key(x) give the position where x will be placed*/
#define int long long
#define lop(i,a,b,c) for (int i=a;i<b;i+=c)
#define rlop(i,a,b,c) for (int i=a-1;i>=b;i-=c)
#define prii pair <int,int>
#define PB push_back
#define S second
#define F first
#define all(x) x.begin(),x.end()
#define vvii vector < vector < int > >
#define vii vector <int>
#define count_1(x) __builtin_popcount(x)
#define cn(a) scanf("%lld",&a);
#define cn_vl(arr,n) lop(i,0,n,1){scanf("%lld",arr+i);}
#define shw_vl(arr,n) lop(i,0,n,1){printf("%lld ",arr[i]);}printf("\n");
const int MAX=2e5+10;
/*......................................................................*/
void fastscan(int &number)
{
    bool negative = false;
    register int c;
    number = 0;
    c = getchar();
    if (c=='-')
    {
        negative = true;
        c = getchar();
    }
    for (; (c>47 && c<58); c=getchar())
        number = number *10 + c - 48;
    if (negative)
        number *= -1;
}
struct dsu
{
	int parent[MAX];int size[MAX];
	void init(){
		lop (i,1,MAX,1){parent[i]=i;size[i]=1;}
	}
	int trace(int a){
		return (a==parent[a])?a:parent[a]=trace(parent[a]);
	}
	bool connect(int a,int b){
		if ((a=trace(a))==(b=trace(b)))return false;
		if (size[b]>size[a])swap(a,b);
		parent[b]=a;size[a]+=size[b];
		return true;
	}
}ori;
struct abc{
	int u,c,d;
};
int f(int a,int b){
	int re=LLONG_MAX;
	lop (i,-1,2,1){
		int z=b+i;
		if (z<0)z=0;
		re=min(z+a/(1+z),re);
	}
	return re;
}
vector <abc> adj[MAX];const int INF=LLONG_MAX;
void dijsktra(int u,vii &dst,int n){
	
	dst.assign(n+1,INF);
	dst[u]=0;
	priority_queue < prii ,vector <prii> ,greater <prii>> q;
	q.push({0,u});
	while (!q.empty()){
		 int wt=q.top().F;
		 int u=q.top().S;q.pop();
		 if  (wt!=dst[u]){
		 	continue;
		 }
		 //cout<<u<<" "<<wt<<"\n";
		 for (auto v:adj[u]){
		 	int re=sqrt(v.d);
		 	int zr=dst[u];
		 	// cout<<v.u<<" "<<re<<" ";
		 	if (wt>re){
		 		re=wt;
		 	}
		 	zr=re;
		 	re=v.c+v.d/(re+1);
					 	// cout<<ren";
		 	if (re+zr<dst[v.u]){
		 		dst[v.u]=re+zr;
		 		q.push({dst[v.u],v.u});
		 	}
		 }
	}
	
}
void solve(){
	int n,m;cn(n)cn(m)
	lop (i,0,m,1){
		int a,b,c,d;
		cn(a)cn(b)cn(c)cn(d)
		 if (a!=b){
		 	adj[a].PB({b,c,d});
			adj[b].PB({a,c,d});
		 }
	}
	vii dst;
	dijsktra(1,dst,n);
	int z=dst[n];
	if (z==INF){
		z=-1;
	}
	cout<<z<<"\n";
}
int32_t main(){
	int t;t=1;
	// cin>>t;
	while (t--){
		auto t_1=chrono::high_resolution_clock::now();
		solve();
		auto t_2=chrono::high_resolution_clock::now();
		// cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl;
	}
	return 0;
}
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double dd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;

const int Mx=1e6+11;
const double PI = acos(-1);
#define MOD 1000000007

#define nl '\n'
#define pb push_back
#define F first
#define S second
#define I insert
#define mp make_pair

#define sz(x) x.size()
#define tt int t; cin>>t; while(t--)
#define be(a) (a).begin(),(a).end()
#define rbe(a) (a).rbegin(),(a).rend()
#define mem(a,b) memset(a,b,sizeof(a))
#define yes cout<<"Yes\n"
#define no cout<<"No\n"

#define su ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define frac cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);


int main()
{
    su;
    string s,t;
    cin>>s;
    int n=sz(s),cnt;
    if(n==1&&s=="8")
        yes;
    else if(n==2)
    {
        if(stoi(s)%8==0)
            yes;
        else
        {
            swap(s[0],s[1]);
            if(stoi(s)%8==0)
                yes;
            else
                no;
        }
    }
    else if(n>=3)
    {
        map<char,int>m,mp;
        for(int i=0; i<n; i++)
        {
            m[s[i]]++;
        }
        if(m['0']>=3)
            yes;
        else
        {
            for(int i=104; i<1000; i+=8)
            {
                t=to_string(i);
                cnt=0;
                mp=m;
                for(int j=0; j<sz(t); j++)
                {
                    if(mp[t[j]]>=1)
                    {
                        cnt++;
                        mp[t[j]]--;
                    }
                }
                if(cnt==sz(t))
                {
                    yes;
                    return 0;
                }
            }
            no;
        }
    }
    else
        no;

    return 0;
}
