#include <bits/stdc++.h>
using namespace std;
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cout<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cout<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 2147483600

map<vector<int>,int> conv;
vector<int> f[300005];
vector<int> g[300005];
int pre[300005];
int val[300005];
bool checked[300005];

// vec?????§v???????????¨?????????
vector<int> center(int v, vector<int> *vec){
  auto bfs = [&](int d){
    queue<pair<int,int>> q;
    q.push(mp(d,-1));
    val[d] = 0;
    int last = d;
    while(q.size()){
      auto p = q.front(); q.pop();
      int node = p.first;
      int from = p.second;
      checked[node]=true;
      for(auto to : vec[node]) if(to!=from){
        q.push(mp(to,node));
        val[to] = val[node]+1;
        pre[to] = node;
      }
      last = node;
    }
    return last;
  };
  int p = bfs(v);
  int q = bfs(p);

  int d = q;
  rep(i,val[q]/2) d = pre[d];

  if(val[q]%2==1) return {d, pre[d]};
  else return {d};
}

// vec?????§i???????????¨?????????????????¨?????????????????°
pair<int,int> tree2idx(int i, vector<int> *vec){
  auto c = center(i, vec);

  function<int(int,int)> dfs = [&](int d, int from){
    vector<int> ord;
    queue<pair<int,int>> q;
    q.push(mp(d,from));
    while(q.size()){
      auto p = q.front(); q.pop();
      int node = p.first;
      int prev = p.second;
      ord.pb(node);
      pre[node] = prev;
      for(auto to : vec[node]) if(to != prev){
        q.push(mp(to, node));
      }
    }
    reverse(all(ord));
    for(auto node : ord){
      vector<int> v;
      for(auto to : vec[node]) if(to!=pre[node]) v.pb(val[to]);
      sort(all(v));
      if(conv.count(v)==0) conv[v] = conv.size();
      val[node] = conv[v];
    }
    return val[d];
  };

  if(c.size()==1){
    return mp(-1, dfs(c[0],-1));
  }
  else if(c.size()==2){
    int a = dfs(c[0], c[1]);
    int b = dfs(c[1], c[0]);
    return mp(min(a,b), max(a,b));
  }
  else while(1);
}

int main(){
  int n,m;
  cin>>n>>m;
  rep(i,m){
    int a,b;
    cin>>a>>b;
    a--;b--;
    f[a].pb(b);
    f[b].pb(a);
  }

  vector<pair<int,int>> nums;

  fill(checked, checked+n, false);
  rep(i,n) if(!checked[i]){
    nums.pb(tree2idx(i, f));
  }

  cin>>n;
  rep(i,n-1){
    int a,b;
    cin>>a>>b;
    a--;b--;
    g[a].pb(b);
    g[b].pb(a);
  }

  auto p = tree2idx(0, g);

  int cnt = 0;
  for(auto q : nums) if(p == q) cnt++;

  cout << cnt << endl;

  return 0;
}