#include<bits/stdc++.h>
using namespace std;
#define lli               long long int
#define ulli              unsigned long long int
#define vi                vector<int>
#define ii                pair<int,int>
#define all(v)            v.begin(),v.end()
#define getunique(v)      {v.erase(unique(v.begin(),v.end()),v.end());}
#define pb                push_back
#define ff                first
#define ss                second
#define endl              "\n"
#define fast_io           ios_base::sync_with_stdio(false);cin.tie(NULL)

const int MS=1000001;
const lli INF=1e9+7;

template<typename T>
 bool comp(T a, T b){
  if(a<b)
    return true;
  return false;
 }
 int vis[3000];
 set<ii > s;
 int cnt=0;
 void dfs(int cnode,vi ar[],int par){
  s.insert({par,cnode});
  vis[cnode]=1;
  cnt++;
  for(auto child:ar[cnode]){
    if(vis[child]==0)
      dfs(child,ar,par);
  }
 }
int main(){
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
   fast_io;
   int n,m;
   cin>>n>>m;
   vi ar[n+1];
   for(int i=0; i<m; i++){
    int a,b;
    cin>>a>>b;
    ar[a].pb(b);
   }
   int ans=0;
   for(int i=1; i<=n; i++){
    memset(vis,0,sizeof vis);
      dfs(i,ar,i);

   }
   // for(auto itr:s)
   //  cout<<itr.ff<<" "<<itr.ss<<endl;
   cout<<s.size();

   
}
#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T>
using V = vector<T>;
template <class T>
inline void chmin(T &a, const T &b) { a = min(a, b); }
template <class T>
inline void chmax(T &a, const T &b) { a = max(a, b); }
template <class T>
inline bool kbit(const T &x, const int &k) { return ((x >> k) & 1LL); }
inline int popcount(const int &n) { return __builtin_popcount(n); }
inline ll popcountll(const ll &n) { return __builtin_popcountll(n); }
inline ll mask(const ll &k) { return (1LL << k) - 1LL; }
template <class T>
void zip(V<T> &v) {
    sort(all(v));
    v.erase(unique(all(v)), v.end());
}
template <class T>
int lwb(V<T> &v, const T &x) { return lower_bound(all(v), x) - v.begin(); }
template <class T>
int upb(V<T> &v, const T &x) { return upper_bound(all(v), x) - v.begin(); }
void dump() {
    cerr << '\n';
}
template <class Head, class... Tail>
void dump(Head &&head, Tail &&...tail) {
    cerr << head << (sizeof...(Tail) == 0 ? " " : ", ");
    dump(std::move(tail)...);
}
template <class T>
void print(const vector<T> &v) {
    for (int i = 0; i < v.size(); i++) cout << v[i] << (i + 1 == v.size() ? '\n' : ' ');
}
template <class T>
void read(vector<T> &v) {
    for (int i = 0; i < v.size(); i++) cin >> v[i];
}
constexpr char sp = ' ', newl = '\n';
constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
constexpr ll INF = (1LL << 60) - 1LL;
constexpr ll MOD = 998244353LL;
//@docs Docs/UnionFind.md
struct UnionFind {
    vector<int> par, sz;
    UnionFind(int n) : par(n), sz(n, 1) {
        iota(par.begin(), par.end(), 0);
    }
    inline int find(int x) {
        if (x == par[x]) return x;
        return par[x] = find(par[x]);
    }
    bool unite(int u, int v) {
        u = find(u), v = find(v);
        if (u == v) return false;
        if (sz[u] < sz[v]) swap(u, v);
        par[v] = u;
        sz[u] += sz[v];
        return true;
    }
    inline int size(int x) {
        return sz[find(x)];
    }
    inline bool same(int u, int v) {
        return find(u) == find(v);
    }
};
//////////////////////////////////////////////INSERT ABOVE HERE
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    V<ll> a(n), b(n);
    read(a);
    read(b);
    UnionFind uf(n);
    for (int i = 0; i < m; i++) {
        int c, d;
        cin >> c >> d;
        --c;
        --d;
        uf.unite(c, d);
    }
    V<ll> sa(n), sb(n);
    for (int i = 0; i < n; i++) {
        sa[uf.find(i)] += a[i];
        sb[uf.find(i)] += b[i];
    }
    for (int i = 0; i < n; i++) {
        if (sa[i] != sb[i]) {
            cout << "No" << newl;
            return 0;
        }
    }
    cout << "Yes" << newl;
}
