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

long long int func(long long int x)
{
    long long int val1=0;
    long long int val2=0;
    string s="";
    while(x>0)
    {
        int temp=x%10;
        s.push_back(temp+'0');
        x/=10;
    }
    
    sort(s.begin() , s.end());
   // cout<<s<<endl;
    long long int ten=1;
    int n=s.size();
    for(int i=0;i<n;i++)
    {
        val1 += ten*(s[i] - '0');
        ten*=10;
    }
    
    reverse(s.begin(),s.end());
    
    ten=1;
    
    for(int i=0;i<n;i++)
    {
        val2 += ten*(s[i] - '0');
        ten*=10;
    }
    
    
    
    return (val1 - val2);
    
    
    
    
}
int main() 
{ 
     
     int test;
     test=1;
     while(test--)
     {
        
        long long int n,k;
        cin>>n>>k;
        
        for(int i=1;i<=k;i++)
        {
            long long int val=func(n);
            n=val;
           // cout<<val<<endl;
            
        }
        
        cout << n<<endl;
        
        
     }
      

	return 0; 
}

/*************************************************************************
    > File Name: C.cpp
    > Author: Bran
    > Mail: 1811161330@qq.com 
    > Created Time: 2021/1/30 20:29:30
 ************************************************************************/

#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define fr first
#define sc second
#define pii pair<int, int>
#define pll pair<long long, long long>
#define rep(i,x,n) for (int i = x; i < int(n); ++i)

using namespace std;
using LL = long long;
const double PI = 3.14159265358979323846;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;

void solve() {
        int n, m;
        cin >> n >> m;
        vector<pii> condition(m);
        rep(i, 0, m) {
                cin >> condition[i].fr >> condition[i].sc;
                --condition[i].fr, --condition[i].sc;
        }
        int k;
        cin >> k;
        vector<pii> dish(k);
        rep(i, 0, k) {
                cin >> dish[i].fr >> dish[i].sc;
                --dish[i].fr, --dish[i].sc;
        }
        int res = 0;
        for (int i = 0; i < (1 << k); ++i) {
                vector<int> cnt(n, 0);
                rep(j, 0, k) {
                        if ((i >> j) & 1) ++cnt[dish[j].sc];
                        else ++cnt[dish[j].fr];
                }
                int ans = 0;
                rep(j, 0, condition.size()) 
                        if (cnt[condition[j].fr] && cnt[condition[j].sc]) {
                                ++ans;
                        }
                res = max(res, ans);
        }
        cout << res << endl;
}

int main() {
        solve();
        return 0;
}
