#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<math.h>
#include<random>
#include<queue>
#include<tuple>
#include<stack>
#include<string>
#include<iomanip>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pp;
typedef tuple<int, int, int> tt;

#define Go_Go_Power_Rangers ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

const ll mod = 998244353;
const int inf = 1000000000;
const int MAXL = 500;

ll poW(ll a, ll b) {
	if (b == 0)
		return 1;
	else if (b % 2 == 0) {
		ll t = poW(a, b / 2);
		return (t*t) % mod;
	}
	else {
		ll t = poW(a, (b - 1) / 2);
		t = (t*t) % mod;
		return (t*a) % mod;
	}
}

int main() {

	Go_Go_Power_Rangers;

	int n, k;
	cin >> n >> k;
	vector<int> a, b;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		if (i % 2 == 0)
			a.push_back(x);
		else
			b.push_back(x);
	}

	ll ans = 0;

	ll res = 0;

	vector<ll> f(n + 1);
	f[1] = 0;
	ll t = k - 1;
	for (int i = 2; i <= n; i++) {
		t = (t*(k - 1)) % mod;
		f[i] = t - f[i - 1];
	}

	int m = a.size();

	int pre = -1;
	int cnt = 0;

	bool tof = m==0;

	for (int i = 0; i < m; i++) {
		if (a[i] == -1)
			cnt++, tof = true;
		else {
			if (cnt == 0) {
				if (pre == a[i]) {
					cout << 0;
					return 0;
				}
			}
			else {
				ll x = (pre == a[i] || pre == -1) ? k - 1 : k - 2;
				if (cnt % 2 == 0)
					x = -x;
				x = (x + mod + f[cnt]) % mod;
				res += x;
				res %= mod;
				cnt = 0;
			}
			pre = a[i];
		}
	}

	if (!tof)
		res++;

	if (cnt != 0) {
		ll y = pre == -1 ? k : k - 1;
		for (int i = 1; i < cnt; i++)
			y = (y*(k - 1)) % mod;
		res = (res + y) % mod;
	}

	m = b.size();

	pre = -1;
	cnt = 0;

	ans = res;
	res = 0;
	tof = m==0;

	for (int i = 0; i < m; i++) {
		if (b[i] == -1)
			cnt++, tof = true;
		else {
			if (cnt == 0) {
				if (pre == b[i]) {
					cout << 0;
					return 0;
				}
			}
			else {
				ll x = (pre == b[i] || pre == -1) ? k - 1 : k - 2;
				if (cnt % 2 == 0)
					x = -x;
				x = (x + mod + f[cnt]) % mod;
				res += x;
				res %= mod;
				cnt = 0;
			}
			pre = b[i];
		}
	}

	if (!tof)
		res++;

	if (cnt != 0) {
		ll z = pre == -1 ? k : k - 1;
		for (int i = 1; i < cnt; i++)
			z = (z*(k - 1)) % mod;
		res = (res + z) % mod;
	}

	ans = (ans*res) % mod;
	cout << ans;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<math.h>
#include<random>
#include<queue>
#include<tuple>
#include<stack>
#include<string>
#include<iomanip>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pp;
typedef tuple<int, int, int> tt;

#define Go_Go_Power_Rangers ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

const ll mod = 998244353;
const int inf = 1000000000;
const int MAXL = 500;

ll poW(ll a, ll b) {
	if (b == 0)
		return 1;
	else if (b % 2 == 0) {
		ll t = poW(a, b / 2);
		return (t*t) % mod;
	}
	else {
		ll t = poW(a, (b - 1) / 2);
		t = (t*t) % mod;
		return (t*a) % mod;
	}
}

int main() {

	Go_Go_Power_Rangers;

	int n, k;
	cin >> n >> k;
	vector<int> a, b;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		if (i % 2 == 0)
			a.push_back(x);
		else
			b.push_back(x);
	}

	ll ans = 0;

	ll res = 0;

	vector<ll> f(n + 1);
	f[1] = 0;
	ll t = k - 1;
	for (int i = 2; i <= n; i++) {
		t = (t*(k - 1)) % mod;
		f[i] = t - f[i - 1];
	}

	int m = a.size();

	int pre = -1;
	int cnt = 0;

	res = 1;

	for (int i = 0; i < m; i++) {
		if (a[i] == -1)
			cnt++;
		else {
			if (cnt == 0) {
				res = pre == a[i] ? 0 : res;
			}
			else {
				ll x = (pre == a[i] || pre == -1) ? k - 1 : k - 2;
				if (cnt % 2 == 0)
					x = -x;
				x = (x + mod + f[cnt]) % mod;
				res = (res*x)%mod;
				cnt = 0;
			}
			pre = a[i];
		}
	}

	if (cnt != 0) {
		ll y = pre == -1 ? k : k - 1;
		for (int i = 1; i < cnt; i++)
			y = (y*(k - 1)) % mod;
		res = (res * y) % mod;
	}

	m = b.size();

	pre = -1;
	cnt = 0;

	ans = res;
	res = 1;

	for (int i = 0; i < m; i++) {
		if (b[i] == -1)
			cnt++;
		else {
			if (cnt == 0) {
				res = pre==b[i] ? 0 : res;
			}
			else {
				ll x = (pre == b[i] || pre == -1) ? k - 1 : k - 2;
				if (cnt % 2 == 0)
					x = -x;
				x = (x + mod + f[cnt]) % mod;
				res = (res*x) % mod;
				cnt = 0;
			}
			pre = b[i];
		}
	}

	if (cnt != 0) {
		ll z = pre == -1 ? k : k - 1;
		for (int i = 1; i < cnt; i++)
			z = (z*(k - 1)) % mod;
		res = (res * z) % mod;
	}

	ans = (ans*res) % mod;
	cout << ans;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<math.h>
#include<random>
#include<queue>
#include<tuple>
#include<stack>
#include<string>
#include<iomanip>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pp;
typedef tuple<int, int, int> tt;

#define Go_Go_Power_Rangers ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

const ll mod = 998244353;
const int inf = 1000000000;
const int MAXL = 500;

ll poW(ll a, ll b) {
	if (b == 0)
		return 1;
	else if (b % 2 == 0) {
		ll t = poW(a, b / 2);
		return (t*t) % mod;
	}
	else {
		ll t = poW(a, (b - 1) / 2);
		t = (t*t) % mod;
		return (t*a) % mod;
	}
}

int main() {

	Go_Go_Power_Rangers;

	int n, k;
	cin >> n >> k;
	vector<int> a, b;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		if (i % 2 == 0)
			a.push_back(x);
		else
			b.push_back(x);
	}

	ll ans = 0;

	ll res = 0;

	vector<ll> f(n + 1);
	f[1] = 0;
	ll t = k - 1;
	for (int i = 2; i <= n; i++) {
		t = (t*(k - 1)) % mod;
		f[i] = (t - f[i - 1] + mod)%mod;
	}

	int m = a.size();

	int pre = -1;
	int cnt = 0;

	res = 1;

	for (int i = 0; i < m; i++) {
		if (a[i] == -1)
			cnt++;
		else {
			if (cnt == 0) {
				res = pre == a[i] ? 0 : res;
			}
			else {
				if (pre != -1) {
					ll x = (pre == a[i] || pre == -1) ? k - 1 : k - 2;
					if (cnt % 2 == 0)
						x = -x;
					x = (x + mod + f[cnt]) % mod;
					res = (res*x) % mod;
				}
				else {
					ll x = poW(k - 1, cnt);
					res = (res*x) % mod;
				}
				cnt = 0;
			}
			pre = a[i];
		}
	}

	if (cnt != 0) {
		ll y = pre == -1 ? k : k - 1;
		y = (y*poW(k - 1, cnt - 1))%mod;
		res = (res * y) % mod;
	}

	m = b.size();

	pre = -1;
	cnt = 0;

	ans = res;
	res = 1;

	for (int i = 0; i < m; i++) {
		if (b[i] == -1)
			cnt++;
		else {
			if (cnt == 0) {
				res = pre==b[i] ? 0 : res;
			}
			else {
				if (pre != -1) {
					ll x = (pre == b[i] || pre == -1) ? k - 1 : k - 2;
					if (cnt % 2 == 0)
						x = -x;
					x = (x + mod + f[cnt]) % mod;
					res = (res*x) % mod;
				}
				else {
					ll x = poW(k - 1, cnt);
					res = (res*x) % mod;
				}
				cnt = 0;
			}
			pre = b[i];
		}
	}

	if (cnt != 0) {
		ll z = pre == -1 ? k : k - 1;
		z = (z*poW(k - 1, cnt - 1))%mod;
		res = (res * z) % mod;
	}

	ans = (ans*res) % mod;
	cout << ans;

	return 0;
}

//9 3
//-1 - 1 - 1 - 1 1 2 3 - 1 - 1
//-1 -1 1 3 -1
//-1 -1 2 -1


#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<math.h>
#include<random>
#include<queue>
#include<tuple>
#include<stack>
#include<string>
#include<iomanip>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pp;
typedef tuple<int, int, int> tt;

#define Go_Go_Power_Rangers ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

const ll mod = 998244353;
const int inf = 1000000000;
const int MAXL = 500;

ll poW(ll a, ll b) {
	if (b == 0)
		return 1;
	else if (b % 2 == 0) {
		ll t = poW(a, b / 2);
		return (t*t) % mod;
	}
	else {
		ll t = poW(a, (b - 1) / 2);
		t = (t*t) % mod;
		return (t*a) % mod;
	}
}

int main() {

	Go_Go_Power_Rangers;

	int n, k;
	cin >> n >> k;
	vector<int> a, b;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		if (i % 2 == 0)
			a.push_back(x);
		else
			b.push_back(x);
	}

	ll ans = 0;

	ll res = 0;

	vector<ll> f(n + 1);
	f[1] = 0;
	ll t = k - 1;
	for (int i = 2; i <= n; i++) {
		t = (t*(k - 1)) % mod;
		f[i] = t - f[i - 1];
	}

	int m = a.size();

	int pre = -1;
	int cnt = 0;

	bool tof = false;

	for (int i = 0; i < m; i++) {
		if (a[i] == -1)
			cnt++, tof = true;
		else {
			if (cnt == 0) {
				if (pre == a[i]) {
					cout << 0;
					return 0;
				}
			}
			else {
				ll x = (pre == a[i] || pre == -1) ? k - 1 : k - 2;
				if (cnt % 2 == 0)
					x = -x;
				x = (x + mod + f[cnt]) % mod;
				res += x;
				res %= mod;
				cnt = 0;
			}
			pre = a[i];
		}
	}

	if (!tof)
		res++;

	if (cnt != 0) {
		ll y = pre == -1 ? k : k - 1;
		for (int i = 1; i < cnt; i++)
			y = (y*(k - 1)) % mod;
		res = (res + y) % mod;
	}

	m = b.size();

	pre = -1;
	cnt = 0;

	ans = res;
	res = 0;
	tof = false;

	for (int i = 0; i < m; i++) {
		if (b[i] == -1)
			cnt++, tof = true;
		else {
			if (cnt == 0) {
				if (pre == b[i]) {
					cout << 0;
					return 0;
				}
			}
			else {
				ll x = (pre == b[i] || pre == -1) ? k - 1 : k - 2;
				if (cnt % 2 == 0)
					x = -x;
				x = (x + mod + f[cnt]) % mod;
				res += x;
				res %= mod;
				cnt = 0;
			}
			pre = b[i];
		}
	}

	if (!tof)
		res++;

	if (cnt != 0) {
		ll z = pre == -1 ? k : k - 1;
		for (int i = 1; i < cnt; i++)
			z = (z*(k - 1)) % mod;
		res = (res + z) % mod;
	}

	ans = (ans*res) % mod;
	cout << ans;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<math.h>
#include<random>
#include<queue>
#include<tuple>
#include<stack>
#include<string>
#include<iomanip>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pp;
typedef tuple<int, int, int> tt;

#define Go_Go_Power_Rangers ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

const ll mod = 998244353;
const int inf = 1000000000;
const int MAXL = 500;

ll poW(ll a, ll b) {
	if (b == 0)
		return 1;
	else if (b % 2 == 0) {
		ll t = poW(a, b / 2);
		return (t*t) % mod;
	}
	else {
		ll t = poW(a, (b - 1) / 2);
		t = (t*t) % mod;
		return (t*a) % mod;
	}
}

int main() {

	Go_Go_Power_Rangers;

	int n, k;
	cin >> n >> k;
	vector<int> a, b;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		if (i % 2 == 0)
			a.push_back(x);
		else
			b.push_back(x);
	}

	ll ans = 0;

	ll res = 0;

	vector<ll> f(n + 1);
	f[1] = 0;
	ll t = k - 1;
	for (int i = 2; i <= n; i++) {
		t = (t*(k - 1)) % mod;
		f[i] = t - f[i - 1];
	}

	int m = a.size();

	int pre = -1;
	int cnt = 0;

	for (int i = 0; i < m; i++) {
		if (a[i] == -1)
			cnt++;
		else {
			if (cnt == 0) {
				if (pre == a[i]) {
					cout << 0;
					return 0;
				}
			}
			else {
				ll x = (pre == a[i] || pre == -1) ? k - 1 : k - 2;
				if (cnt % 2 == 0)
					x = -x;
				x = (x + mod + f[cnt]) % mod;
				res += x;
				res %= mod;
				cnt = 0;
			}
			pre = a[i];
		}
	}

	if (cnt != 0) {
		ll y = pre == -1 ? k : k - 1;
		for (int i = 1; i < cnt; i++)
			y = (y*(k - 1)) % mod;
		res = (res + y) % mod;
	}

	m = b.size();

	pre = -1;
	cnt = 0;

	ans = res;
	res = 0;

	for (int i = 0; i < m; i++) {
		if (b[i] == -1)
			cnt++;
		else {
			if (cnt == 0) {
				if (pre == b[i]) {
					cout << 0;
					return 0;
				}
			}
			else {
				ll x = (pre == b[i] || pre == -1) ? k - 1 : k - 2;
				if (cnt % 2 == 0)
					x = -x;
				x = (x + mod + f[cnt]) % mod;
				res += x;
				res %= mod;
				cnt = 0;
			}
			pre = b[i];
		}
	}

	if (cnt != 0) {
		ll z = pre == -1 ? k : k - 1;
		for (int i = 1; i < cnt; i++)
			z = (z*(k - 1)) % mod;
		res = (res + z) % mod;
	}

	ans = (ans*res) % mod;
	cout << ans;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define ll long long int
#define mod 998244353
#define pl pair<int,int>
vector<ll> v1,v2;
ll a[200005],cnsame[200005],cndif[200005];
ll power(ll x,ll y,ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;

        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
ll ans1(ll p,ll q)
{
    ll i,j,k=q-p-1;
    if(k==0)
    {
        if(v1[p]==v1[q])
        return 0;
        else
        return 1;
    }
    else
    {
        if(v1[p]==v1[q])
            return cnsame[k];
            else
            return cndif[k];
    }
}
ll ans2(ll p,ll q)
{
    ll i,j,k=q-p-1;
    if(k==0)
    {
        if(v2[p]==v2[q])
        return 0;
        else
        return 1;
    }
    else
    {
        if(v2[p]==v2[q])
        return cnsame[k];
        else
        return cndif[k];
    }
}
int main()
{
	ll i=0,j,k=0,n,m;
    cin>>n>>k;
    cnsame[0]=0;
    cndif[0]=1;
    for(i=1;i<n;i++)
    {
        if(i%2==0)
        {
            cnsame[i]=(k-1)*cndif[i-1];
            cndif[i]=cnsame[i-1]+(k-2)*cndif[i-1];
            cnsame[i]%=mod;
            cndif[i]%=mod;
        }
        else
        {
            cnsame[i]=((cnsame[i/2]*cnsame[i/2]))%mod+(k-1)*((cndif[i/2]*cndif[i/2])%mod);
            cndif[i]=(2*cnsame[i/2]*cndif[i/2])%mod+(k-2)*((cndif[i/2]*cndif[i/2])%mod);
            cnsame[i]%=mod;
            cndif[i]%=mod;
        }
    }
    for(i=0;i<n;i++)
    {

        //cin>>a[i];
        a[i]=-1;
        if(i%2==0)
        v1.push_back(a[i]);
        else
        v2.push_back(a[i]);
    }
    ll a1=1,a2=1,te=0,d=0,pre=0,post;
    i=0;
    if(v1[i]==-1)
    {
        while(i<v1.size()&&v1[i]==-1)
        i++;
        if(i==v1.size())
        {
            a1=k*power(k-1,v1.size()-1,mod);
            a1%=mod;
            pre=v1.size()-1;
        }
        else
        {
            pre=i;
            a1=cnsame[i-1]+(k-1)*cndif[i-1];
            a1%=mod;
        }

    }
    else
    pre=0;
    i++;
    for(;i<v1.size();i++)
    {
        if(v1[i]!=-1)
        {
            a1*=ans1(pre,i);
            pre=i;
            a1%=mod;
        }
    }
    j=v1.size()-(pre)-1;
    if(j!=0)
    {
        a1*=(cnsame[j-1]+(cndif[j-1]*(k-1))%mod);
        a1%=mod;
    }
    i=0;
    pre=0;

    if(v2.size()>0&&v2[i]==-1)
    {
        while(i<v2.size()&&v2[i]==-1)
        i++;
        if(i==v2.size())
        {
            a2=k*power(k-1,v2.size()-1,mod);
            a2%=mod;
            pre=v2.size()-1;
        }
        else
        {
            pre=i;
            a2=cnsame[i-1]+(k-1)*cndif[i-1];
            a2%=mod;
        }

    }
    else
    pre=0;
    i++;
    for(;i<v2.size();i++)
    {
        if(v2[i]!=-1)
        {
            a2*=ans2(pre,i);
            pre=i;
            a2%=mod;
        }
    }
    j=v2.size()-(pre)-1;
    if(j!=0)
    {
        a2*=(cnsame[j-1]+(cndif[j-1]*(k-1))%mod);
        a2%=mod;
    }
    //cout<<a1<<" "<<a2<<" ";
    cout<<(a1*a2)%mod;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define ll long long int
#define mod 998244353
#define pl pair<int,int>
vector<ll> v1,v2;
ll a[200005],cnsame[200005],cndif[200005];
ll power(ll x,ll y,ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;

        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
ll ans1(ll p,ll q)
{
    ll i,j,k=q-p-1;
    if(k==0)
    {
        if(v1[p]==v1[q])
        return 0;
        else
        return 1;
    }
    else
    {
        if(v1[p]==v1[q])
            return cnsame[k];
            else
            return cndif[k];
    }
}
ll ans2(ll p,ll q)
{
    ll i,j,k=q-p-1;
    if(k==0)
    {
        if(v2[p]==v2[q])
        return 0;
        else
        return 1;
    }
    else
    {
        if(v2[p]==v2[q])
        return cnsame[k];
        else
        return cndif[k];
    }
}
int main()
{
	ll i=0,j,k=0,n,m;
    cin>>n>>k;
    cnsame[0]=0;
    cndif[0]=1;
    for(i=1;i<n;i++)
    {
        if(i%2==0)
        {
            cnsame[i]=(k-1)*cndif[i-1];
            cndif[i]=cnsame[i-1]+(k-2)*cndif[i-1];
            cnsame[i]%=mod;
            cndif[i]%=mod;
        }
        else
        {
            cnsame[i]=((cnsame[i/2]*cnsame[i/2]))%mod+(k-1)*((cndif[i/2]*cndif[i/2])%mod);
            cndif[i]=(2*cnsame[i/2]*cndif[i/2])%mod+(k-2)*((cndif[i/2]*cndif[i/2])%mod);
            cnsame[i]%=mod;
            cndif[i]%=mod;
        }
    }
    for(i=0;i<n;i++)
    {

        cin>>a[i];
        if(i%2==0)
        v1.push_back(a[i]);
        else
        v2.push_back(a[i]);
    }
    ll a1=1,a2=1,te=0,d=0,pre=0,post;
    i=0;
    if(v1[i]==-1)
    {
        while(i<v1.size()&&v1[i]==-1)
        i++;
        if(i==v1.size())
        {
            a1=k*power(k-1,v1.size()-1,mod);
            a1%=mod;
        }
        else
        {
            pre=i;
            a1=cnsame[i-1]+(k-1)*cndif[i-1];
            a1%=mod;
        }

    }
    else
    pre=0;
    i++;
    for(;i<v1.size();i++)
    {
        if(v1[i]!=-1)
        {
            a1*=ans1(pre,i);
            pre=i;
            a1%=mod;
        }
    }
    j=v1.size()-(pre)-1;
    if(j!=0)
    {
        a1*=(cnsame[j-1]+(cndif[j-1]*(k-1)));
        a1%=mod;
    }
    i=0;
    pre=0;

    if(v2.size()>0&&v2[i]==-1)
    {
        while(i<v2.size()&&v2[i]==-1)
        i++;
        if(i==v2.size())
        {
            a2=k*power(k-1,v2.size()-1,mod);
            a2%=mod;
            pre=v2.size()-1;
        }
        else
        {
            pre=i;
            a2=cnsame[i-1]+(k-1)*cndif[i-1];
            a2%=mod;
        }

    }
    else
    pre=0;
    i++;
    for(;i<v2.size();i++)
    {
        if(v2[i]!=-1)
        {
            a2*=ans2(pre,i);
            pre=i;
            a2%=mod;
        }
    }
    j=v2.size()-(pre)-1;
    if(j!=0)
    {
        a2*=(cnsame[j-1]+(cndif[j-1]*(k-1))%mod);
        a2%=mod;
    }
    //cout<<a1<<" "<<a2<<" ";
    cout<<((a1*a2)%mod+mod)%mod;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define ll long long int
#define mod 998244353
#define pl pair<int,int>
vector<ll> v1,v2;
ll a[200005],cnsame[200005],cndif[200005];
ll power(ll x,ll y,ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;

        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
ll ans1(ll p,ll q)
{
    ll i,j,k=q-p-1;
    if(k==0)
    {
        if(v1[p]==v1[q])
        return 0;
        else
        return 1;
    }
    else
    {
        if(v1[p]==v1[q])
            return cnsame[k];
            else
            return cndif[k];
    }
}
ll ans2(ll p,ll q)
{
    ll i,j,k=q-p-1;
    if(k==0)
    {
        if(v2[p]==v2[q])
        return 0;
        else
        return 1;
    }
    else
    {
        if(v2[p]==v2[q])
        return cnsame[k];
        else
        return cndif[k];
    }
}
int main()
{
	ll i=0,j,k=0,n,m;
    cin>>n>>k;
    cnsame[0]=0;
    cndif[0]=1;
    for(i=1;i<n;i++)
    {
        if(i%2==0)
        {
            cnsame[i]=(k-1)*cndif[i-1];
            cndif[i]=cnsame[i-1]+(k-2)*cndif[i-1];
            cnsame[i]%=mod;
            cndif[i]%=mod;
        }
        else
        {
            cnsame[i]=((cnsame[i/2]*cnsame[i/2]))%mod+(k-1)*((cndif[i/2]*cndif[i/2])%mod);
            cndif[i]=(2*cnsame[i/2]*cndif[i/2])%mod+(k-2)*((cndif[i/2]*cndif[i/2])%mod);
            cnsame[i]%=mod;
            cndif[i]%=mod;
        }
    }
    for(i=0;i<n;i++)
    {

        cin>>a[i];
        if(i%2==0)
        v1.push_back(a[i]);
        else
        v2.push_back(a[i]);
    }
    ll a1=1,a2=1,te=0,d=0,pre=0,post;
    i=0;
    if(v1[i]==-1)
    {
        while(i<v1.size()&&v1[i]==-1)
        i++;
        if(i==v1.size())
        {
            a1=k*power(k-1,v1.size()-1,mod);
            a1%=mod;
        }
        else
        {
            pre=i;
            a1=cnsame[i-1]+(k-1)*cndif[i-1];
            a1%=mod;
        }

    }
    else
    pre=0;
    i++;
    for(;i<v1.size();i++)
    {
        if(v1[i]!=-1)
        {
            a1*=ans1(pre,i);
            pre=i;
            a1%=mod;
        }
    }
    j=v1.size()-(pre)-1;
    if(j!=0)
    {
        a1*=(cnsame[j-1]+(cndif[j-1]*(k-1)));
        a1%=mod;
    }
    i=0;
    pre=0;

    if(v2.size()>0&&v2[i]==-1)
    {
        while(i<v2.size()&&v2[i]==-1)
        i++;
        if(i==v2.size())
        {
            a2=k*power(k-1,v2.size()-1,mod);
            a2%=mod;
            pre=v2.size()-1;
        }
        else
        {
            pre=i;
            a2=cnsame[i-1]+(k-1)*cndif[i-1];
            a2%=mod;
        }

    }
    else
    pre=0;
    i++;
    for(;i<v2.size();i++)
    {
        if(v2[i]!=-1)
        {
            a2*=ans2(pre,i);
            pre=i;
            a2%=mod;
        }
    }
    j=v2.size()-(pre)-1;
    if(j!=0)
    {
        a2*=(cnsame[j-1]+(cndif[j-1]*(k-1))%mod);
        a2%=mod;
    }
    //cout<<a1<<" "<<a2<<" ";
    cout<<(a1*a2)%mod;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define ll long long int
#define mod 998244353
#define pl pair<int,int>
vector<ll> v1,v2;
ll a[200005],cnsame[200005],cndif[200005];
ll power(ll x,ll y,ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;

        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
ll ans1(ll p,ll q)
{
    ll i,j,k=q-p-1;
    if(k==0)
    {
        if(v1[p]==v1[q])
        return 0;
        else
        return 1;
    }
    else
    {
        if(v1[p]==v1[q])
            return cnsame[k];
            else
            return cndif[k];
    }
}
ll ans2(ll p,ll q)
{
    ll i,j,k=q-p-1;
    if(k==0)
    {
        if(v2[p]==v2[q])
        return 0;
        else
        return 1;
    }
    else
    {
        if(v2[p]==v2[q])
        return cnsame[k];
        else
        return cndif[k];
    }
}
int main()
{
	ll i=0,j,k=0,n,m;
    cin>>n>>k;
    cnsame[0]=0;
    cndif[0]=1;
    for(i=1;i<n;i++)
    {
        if(i%2==0)
        {
            cnsame[i]=(k-1)*cndif[i-1];
            cndif[i]=cnsame[i-1]+(k-2)*cndif[i-1];
            cnsame[i]%=mod;
            cndif[i]%=mod;
        }
        else
        {
            cnsame[i]=((cnsame[i/2]*cnsame[i/2]))%mod+(k-1)*((cndif[i/2]*cndif[i/2])%mod);
            cndif[i]=(2*cnsame[i/2]*cndif[i/2])%mod+(k-2)*((cndif[i/2]*cndif[i/2])%mod);
            cnsame[i]%=mod;
            cndif[i]%=mod;
        }
    }
    for(i=0;i<n;i++)
    {

        cin>>a[i];
        //a[i]=-1;
        if(i%2==0)
        v1.push_back(a[i]);
        else
        v2.push_back(a[i]);
    }
    ll a1=1,a2=1,te=0,d=0,pre=0,post;
    i=0;
    if(v1[i]==-1)
    {
        while(i<v1.size()&&v1[i]==-1)
        i++;
        if(i==v1.size())
        {
            a1=k*power(k-1,v1.size()-1,mod);
            a1%=mod;
            pre=v1.size()-1;
        }
        else
        {
            pre=i;
            a1=cnsame[i-1]+(k-1)*cndif[i-1];
            a1%=mod;
        }

    }
    else
    pre=0;
    i++;
    for(;i<v1.size();i++)
    {
        if(v1[i]!=-1)
        {
            a1*=ans1(pre,i);
            pre=i;
            a1%=mod;
        }
    }
    j=v1.size()-(pre)-1;
    if(j!=0)
    {
        a1*=(cnsame[j-1]+(cndif[j-1]*(k-1))%mod);
        a1%=mod;
    }
    i=0;
    pre=0;

    if(v2.size()>0&&v2[i]==-1)
    {
        while(i<v2.size()&&v2[i]==-1)
        i++;
        if(i==v2.size())
        {
            a2=k*power(k-1,v2.size()-1,mod);
            a2%=mod;
            pre=v2.size()-1;
        }
        else
        {
            pre=i;
            a2=cnsame[i-1]+(k-1)*cndif[i-1];
            a2%=mod;
        }

    }
    else
    pre=0;
    i++;
    for(;i<v2.size();i++)
    {
        if(v2[i]!=-1)
        {
            a2*=ans2(pre,i);
            pre=i;
            a2%=mod;
        }
    }
    j=v2.size()-(pre)-1;
    if(j!=0)
    {
        a2*=(cnsame[j-1]+(cndif[j-1]*(k-1))%mod);
        a2%=mod;
    }
    //cout<<a1<<" "<<a2<<" ";
    cout<<(a1*a2)%mod;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define ll long long int
#define mod 998244353
#define pl pair<int,int>
vector<int> v1,v2;
int a[200005],cnsame[200005],cndif[200005];
ll power(ll x,ll y,ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;

        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
ll ans1(ll p,ll q)
{
    ll i,j,k=q-p-1;
    if(k==0)
    {
        if(v1[p]==v1[q])
        return 0;
        else
        return 1;
    }
    else
    {
        if(v1[p]==v1[q])
            return cnsame[k];
            else
            return cndif[k];
    }
}
ll ans2(ll p,ll q)
{
    ll i,j,k=q-p-1;
    if(k==0)
    {
        if(v2[p]==v2[q])
        return 0;
        else
        return 1;
    }
    else
    {
        if(v2[p]==v2[q])
        return cnsame[k];
        else
        return cndif[k];
    }
}
int main()
{
	ll i=0,j,k=0,n,m;
    cin>>n>>k;
    cnsame[0]=0;
    cndif[0]=1;
    for(i=1;i<n;i++){
        if(i%2==0)
        {
            cnsame[i]=(k-1)*cndif[i-1];
            cndif[i]=cnsame[i-1]+(k-2)*cndif[i-1];
        }
        else
        {
            cnsame[i]=(cnsame[i/2]*cnsame[i/2])+(k-1)*(cndif[i/2]*cndif[i/2]);
            cndif[i]=2*cnsame[i/2]*cndif[i/2]+(k-2)*cndif[i/2]*cndif[i/2];
        }
    }
    for(i=0;i<n;i++)
    {

        cin>>a[i];
        if(i%2==0)
        v1.push_back(a[i]);
        else
        v2.push_back(a[i]);
    }
    ll a1=1,a2=1,te=0,d=0,pre=0,post;
    i=0;
    if(v1[i]==-1)
    {
        while(i<v1.size()&&v1[i]==-1)
        i++;
        if(i==v1.size())
        {
            a1=k*power(k-1,v1.size()-1,mod);
            a1%=mod;
        }
        else
        {
            pre=i;
            a1=cnsame[i-1]+(k-1)*cndif[i-1];
            a1%=mod;
        }

    }
    else
    pre=0;
    i++;
    for(;i<v1.size();i++)
    {
        if(v1[i]!=-1)
        {
            a1*=ans1(pre,i);
            pre=i;
            a1%=mod;
        }
    }
    j=v1.size()-(pre)-1;
    if(j!=0)
    {
        a1*=(cnsame[j-1]+cndif[j-1]*(k-1));
        a1%=mod;
    }
    i=0;
    pre=0;

    if(v2.size()>0&&v2[i]==-1)
    {
        while(i<v2.size()&&v2[i]==-1)
        i++;
        if(i==v2.size())
        {
            a2=k*power(k-1,v2.size()-1,mod);
            a2%=mod;
        }
        else
        {
            pre=i;
            a2=cnsame[i-1]+(k-1)*cndif[i-1];
            a2%=mod;
        }
    }
    else
    pre=0;
    i++;
    for(;i<v2.size();i++)
    {
        if(v2[i]!=-1)
        {
            a2*=ans2(pre,i);
            pre=i;
            a2%=mod;
        }
    }
    j=v2.size()-(pre)-1;
    if(j!=0)
    {
        a2*=(cnsame[j-1]+cndif[j-1]*(k-1));
        a2%=mod;
    }
    //cout<<a1<<" "<<a2;
    cout<<(a1*a2)%mod;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define ll long long int
#define mod 998244353
#define pl pair<int,int>
vector<int> v1,v2;
int a[200005],cnsame[200005],cndif[200005];
ll power(ll x,ll y,ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;

        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
ll ans1(ll p,ll q)
{
    ll i,j,k=q-p-1;
    if(k==0)
    {
        if(v1[p]==v1[q])
        return 0;
        else
        return 1;
    }
    else
    {
        if(v1[p]==v1[q])
            return cnsame[k];
            else
            return cndif[k];
    }
}
ll ans2(ll p,ll q)
{
    ll i,j,k=q-p-1;
    if(k==0)
    {
        if(v2[p]==v2[q])
        return 0;
        else
        return 1;
    }
    else
    {
        if(v2[p]==v2[q])
        return cnsame[k];
        else
        return cndif[k];
    }
}
int main()
{
	ll i=0,j,k=0,n,m;
    cin>>n>>k;
    cnsame[0]=0;
    cndif[0]=1;
    for(i=1;i<n;i++){
        if(i%2==0)
        {
            cnsame[i]=(k-1)*cndif[i-1];
            cndif[i]=cnsame[i-1]+(k-2)*cndif[i-1];
        }
        else
        {
            cnsame[i]=(cnsame[i/2]*cnsame[i/2])+(k-1)*(cndif[i/2]*cndif[i/2]);
            cndif[i]=2*cnsame[i/2]*cndif[i/2]+(k-2)*cndif[i/2]*cndif[i/2];
        }
    }
    for(i=0;i<n;i++)
    {

        cin>>a[i];
        if(i%2==0)
        v1.push_back(a[i]);
        else
        v2.push_back(a[i]);
    }
    ll a1=1,a2=1,te=0,d=0,pre,post;
    i=0;
    if(v1[i]==-1)
    {
        while(v1[i]==-1&&i<v1.size())
        i++;
        if(i==v1.size())
        {
            a1=k*power(k-1,v1.size()-1,mod);
            a1%=mod;
        }
        else
        {
            pre=i;
            a1=cnsame[i-1]+(k-1)*cndif[i-1];
            a1%=mod;
        }

    }
    else
    pre=0;
    i++;
    for(;i<v1.size();i++)
    {
        if(v1[i]!=-1)
        {
            a1*=ans1(pre,i);
            pre=i;
            a1%=mod;
        }
    }
    j=v1.size()-(pre)-1;
    if(j!=0)
    {
        a1*=(cnsame[j-1]+cndif[j-1]*(k-1));
        a1%=mod;
    }
    i=0;
    if(v2[i]==-1)
    {
        while(v2[i]==-1&&i<v2.size())
        i++;
        if(i==v2.size())
        {
            a2=k*power(k-1,v2.size()-1,mod);
            a2%=mod;
        }
        else
        {
            pre=i;
            a2=cnsame[i-1]+(k-1)*cndif[i-1];
            a2%=mod;
        }
    }
    else
    pre=0;
    i++;

    for(;i<v2.size();i++)
    {
        if(v2[i]!=-1)
        {
            a2*=ans2(pre,i);
            pre=i;
            a2%=mod;
        }
    }
    j=v2.size()-(pre)-1;
    if(j!=0)
    {
        a2*=(cnsame[j-1]+cndif[j-1]*(k-1));
        a2%=mod;
    }
    //cout<<a1<<" "<<a2;
    cout<<(a1*a2)%mod;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define ub upper_bound
#define lb lower_bound
#define sz(a) int((a).size())
#define fill(a) memset(a,0,sizeof(a))
#define present(c,x) (c.find(x) != c.end())
#define removeDuplicates(a) a.resize(unique(all(a))-a.begin())
#define endl '\n'
#define INF 1000000007
#define MOD 998244353
#define INF64 1e18

void solve(){
	string s;
	cin>>s;
	int n;
	cin>>n;
	map<char,char>m;
	for(int i=0;i<n;i++){
		string a;
		cin>>a;
		m[a[0]]=a[1];
		m[a[1]]=a[0];
	}
	int i,head;
	vector<pair<char,int>>a(s.length());
	for(i=0,head=0;head<s.length();i++){
		int cnt=0;
		while(s[head]==s[head+cnt]){
			cnt++;
		}
		a[i]=mp(s[head],cnt);
		head+=cnt;
	}
	int n=i;
	int ans=0;
	for(i=0,head=0;i<n;){
		head=i;
		int bad=0,good=0;
		for(;i<n;i++){
			if(m[a[head].fi]==a[i].fi){
				bad+=a[i].se;
			}else if(a[head].fi==a[i].fi){
				good+=a[i].se;
			}else{
				break;
			}
		}
		ans+=min(good,bad);
	}
	cout<<ans<<endl;
}

signed main(){

	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int T;
	//cin>>T;
	T=1;
	while(T--){
		solve();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define ub upper_bound
#define lb lower_bound
#define sz(a) int((a).size())
#define fill(a) memset(a,0,sizeof(a))
#define present(c,x) (c.find(x) != c.end())
#define removeDuplicates(a) a.resize(unique(all(a))-a.begin())
#define endl '\n'
#define INF 1000000007
#define MOD 998244353
#define INF64 1e18

void solve(){
	string s;
	cin>>s;
	int n;
	cin>>n;
	map<char,char>m;
	for(int i=0;i<n;i++){
		string a;
		cin>>a;
		m[a[0]]=a[1];
		m[a[1]]=a[0];
	}
	int i,head;
	vector<pair<char,int>>a(s.length());
	for(i=0,head=0;head<s.length();i++){
		int cnt=0;
		while(s[head]==s[head+cnt]){
			cnt++;
		}
		a[i]=mp(s[head],cnt);
		head+=cnt;
	}
	n=i;
	int ans=0;
	for(i=0,head=0;i<n;){
		head=i;
		int bad=0,good=0;
		for(;i<n;i++){
			if(m[a[head].fi]==a[i].fi){
				bad+=a[i].se;
			}else if(a[head].fi==a[i].fi){
				good+=a[i].se;
			}else{
				break;
			}
		}
		ans+=min(good,bad);
	}
	cout<<ans<<endl;
}

signed main(){

	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int T;
	//cin>>T;
	T=1;
	while(T--){
		solve();
	}
}


/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
*/

#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define pi acos(-1.0)
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()

//#define inf 1<<30

// isupper() -> upper case check
// tolower() -> convert upper to lower case
// idx = str.find("bear", pos)) != string::npos =>substring find
// Tuple access -> get<0>(v[i])
// cmnt - ctrl []
// stoi("...") -> decimel
// istringstream ss(str) while(ss >> n)

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using tpl = tuple<int, int, int>;
using pcc = pair<char, char>;

const ll inf = 2e18;
const ll mod = 1e9+7;

int dr[] = {-1, 0, 1,  0};// up, right, down, left
int dc[] = { 0, 1, 0, -1};

// King moves similar to 8 direction
//int dr[] = {-1, -1, 0, 1, 1,  1, 0, -1};
//int dc[] = { 0,  1, 1, 1, 0, -1,-1, -1};

// Knight moves
int kr[] = {-1,  1, -2, -2, -1, 1,  2,  2};
int kc[] = {-2, -2, -1,  1,  2, 2, -1,  1};
// diagonal moves
int dgr[] = {-1, -1,  1, 1};
int dgc[] = { 1, -1, -1, 1};

int main()
{
    string str, s, cur1, cur2, rev1, rev2;
    int k, len, ans = 0;
    int cnt[33];
    memset(cnt, 0, sizeof cnt);

    set<string>v;

    cin>>str>>k;
    while(k--) {
        cin>>s;
        v.insert(s);
    }
    len = str.size();
    for(int i=0; i<len-1; i++) {
        //cur += str[i]+str[i+1];
        cur1.pb(str[i]); cur1.pb(str[i+1]);
        /*if(i) {
            cur2.pb(str[i]), cur2.pb(str[i-1]);
        }*/
        //cout << cur1 <<endl;
        rev1 = cur1; //rev2 = cur2;
        reverse(all(rev1));
        //reverse(all(rev2));

        if(v.count(cur1) || v.count(rev1)) {
            //printf("cur %s\n", cur1.c_str());
            int j = i-1;
            while(j >= 0 && (str[j] == cur1[0]
                || str[j] == cur2[0])) {
                cnt[str[j]-'a']++;
                j--;
            }
            while(i<len && (str[i] == cur1[0] ||
                str[i] == cur1[1])) {
                cnt[str[i]-'a']++;
                i++;
            }
            //printf("i %d\n", i);
            i--;
            ans += min(cnt[cur1[0]-'a'], cnt[cur1[1]-'a']);
            cnt[cur1[0]-'a'] = cnt[cur1[1]-'a'] = 0;
            //printf("ans %d\n", ans);
        }
        cur1 = "";
    }
    /*
    for(string str: v) {
        printf("%d %d\n", cnt[str[0]-'a'], cnt[str[1]-'a']);
        ans += min(cnt[str[0]-'a'], cnt[str[1]-'a']);
    }*/

    cout << ans<<endl;

    return 0;
}


/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
*/

#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define pi acos(-1.0)
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()

//#define inf 1<<30

// isupper() -> upper case check
// tolower() -> convert upper to lower case
// idx = str.find("bear", pos)) != string::npos =>substring find
// Tuple access -> get<0>(v[i])
// cmnt - ctrl []
// stoi("...") -> decimel
// istringstream ss(str) while(ss >> n)

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using tpl = tuple<int, int, int>;
using pcc = pair<char, char>;

const ll inf = 2e18;
const ll mod = 1e9+7;

int dr[] = {-1, 0, 1,  0};// up, right, down, left
int dc[] = { 0, 1, 0, -1};

// King moves similar to 8 direction
//int dr[] = {-1, -1, 0, 1, 1,  1, 0, -1};
//int dc[] = { 0,  1, 1, 1, 0, -1,-1, -1};

// Knight moves
int kr[] = {-1,  1, -2, -2, -1, 1,  2,  2};
int kc[] = {-2, -2, -1,  1,  2, 2, -1,  1};
// diagonal moves
int dgr[] = {-1, -1,  1, 1};
int dgc[] = { 1, -1, -1, 1};

int main()
{
    string str, s, cur1, cur2, rev1, rev2;
    int k, len, ans = 0;
    int cnt[33];
    memset(cnt, 0, sizeof cnt);

    set<string>v;

    cin>>str>>k;
    while(k--) {
        cin>>s;
        v.insert(s);
    }
    len = str.size();
    for(int i=0; i<len-1; i++) {
        //cur += str[i]+str[i+1];
        cur1.pb(str[i]); cur1.pb(str[i+1]);
        if(i) {
            cur2.pb(str[i]), cur2.pb(str[i-1]);
        }
        //cout << cur <<endl;
        rev1 = cur1; rev2 = cur2;
        reverse(all(rev1));
        reverse(all(rev2));

        if(v.count(cur1) || v.count(rev1)
            || v.count(cur2) || v.count(rev2)) {
            cnt[str[i]-'a']++;
        }
        cur1 = cur2 = "";
    }
    for(string str: v) {
        ans += min(cnt[str[0]-'a'], cnt[str[1]-'a']);
    }

    cout << ans<<endl;

    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

bool operator <(pair<int, int> x,pair <int,int > y)
{
	return (x.first<y.first || (x.first==y.first && x.second<y.second));
}

int main(){
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	string s;
	cin >>s;
	int k;
	cin >>k;
	vector <int> p(26,-1);
	vector <pair <char, char> > v(k);
	for (int i=0; i<k; ++i)
	{
		cin >>v[i].first >>v[i].second;
		p[v[i].first-'a']=v[i].second;
		p[v[i].second-'a']=v[i].first;
	}
	string s1;
	vector <int> h;
	s1=s[0];
	h.push_back(1);
	for (int i=1; i<(int)s.length();++i)
		if (s[i]==s[i-1])
			++h.back();
		else
		{
			s1+=s[i];
			h.push_back(1);
		}
	int pos=0,count=0;
	while (pos<(int)h.size())
		if (pos<(int)h.size()-1 && s1[pos+1]==p[s1[pos]-'a'])
		{
			int count1=h[pos],count2=h[pos+1],l=pos+2;
			while (l<(int)s1.length())
				if ((l-pos)%2)
					if (s1[l]==s1[pos+1])
					{
						count2+=h[l];
						++l;
					}
					else
						break;
				else
					if (s1[l]==s1[pos])
					{
						count1+=h[l];
						++l;
					}
					else
						break;
			count+=min(count1,count2);
			pos=l;
		}
		else
			++pos;
	cout <<count;
	return 0;
}


#include<stdio.h>
#include<stack>
#include<math.h>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
#include<set>
#include<memory.h>
#include<vector>
#include<map>
#include<queue>
#include<deque>
#include<cassert>
#include<time.h>
#include<bitset>
#include<ctime>
#include<unordered_set>

using namespace std;

const int INF = 1e9;
const double pi = 3.14159265358979323846;

typedef long long li;
typedef long double ld;

#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define pb push_back
#define mp make_pair

string s;
int k;
bool pr[26][26];

bool read()
{
    if (!(cin >> s))
        return false;
    cin >> k;
    forn (i, k)
    {
        char a, b;
        cin >> a >> b;
        pr[a - 'a'][b - 'a'] = true;
    }
    return true;
}

int dp[100001][26];

void solve()
{
    forn (i, 100000)
        forn (j, 26)
            dp[i + 1][j] = -1;
    int n = s.size();
    int ans = 0;
    forn (i, n)
    {
        forn (j, 26)
        {
            if (!pr[j][s[i] - 'a'])
                dp[i + 1][s[i] - 'a'] = max(dp[i + 1][s[i] - 'a'], dp[i][j] + 1);
            dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
        }
    }
    forn (i, 26)
        ans = max(ans, dp[n][i]);
    cout << n - ans << endl;
}

int main()
{
#ifdef Baster
    freopen("input.txt", "r", stdin);
//  freopen("output.txt", "w", stdout);
#endif
    while(read())
        solve();
    return 0;
}


#include<stdio.h>
#include<stack>
#include<math.h>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
#include<set>
#include<memory.h>
#include<vector>
#include<map>
#include<queue>
#include<deque>
#include<cassert>
#include<time.h>
#include<bitset>
#include<ctime>
#include<unordered_set>

using namespace std;

const int INF = 1e9;
const double pi = 3.14159265358979323846;

typedef long long li;
typedef long double ld;

#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define pb push_back
#define mp make_pair

string s;
int k;
bool pr[26][26];

bool read()
{
    if (!(cin >> s))
        return false;
    cin >> k;
    forn (i, k)
    {
        char a, b;
        cin >> a >> b;
        pr[a - 'a'][b - 'a'] = true;
        pr[b - 'a'][a - 'a'] = true;
    }
    return true;
}

int dp[100001][26];

void solve()
{
    forn (i, 100000)
        forn (j, 26)
            dp[i + 1][j] = -1;
    int n = s.size();
    int ans = 0;
    forn (i, n)
    {
        forn (j, 26)
        {
            if (!pr[j][s[i] - 'a'])
                dp[i + 1][s[i] - 'a'] = max(dp[i + 1][s[i] - 'a'], dp[i][j] + 1);
            dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
        }
    }
    forn (i, 26)
        ans = max(ans, dp[n][i]);
    cout << n - ans << endl;
}

int main()
{
#ifdef Baster
    freopen("input.txt", "r", stdin);
//  freopen("output.txt", "w", stdout);
#endif
    while(read())
        solve();
    return 0;
}


#include<stdio.h>
#include<stack>
#include<math.h>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
#include<set>
#include<memory.h>
#include<vector>
#include<map>
#include<queue>
#include<deque>
#include<cassert>
#include<time.h>
#include<bitset>
#include<ctime>
#include<unordered_set>

using namespace std;

const int INF = 1e9;
const double pi = 3.14159265358979323846;

typedef long long li;
typedef long double ld;

#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define pb push_back
#define mp make_pair

string s;
int k;
bool pr[26][26];

bool read()
{
    if (!(cin >> s))
        return false;
    cin >> k;
    forn (i, k)
    {
        char a, b;
        cin >> a >> b;
        pr[a - 'a'][b - 'a'] = true;
    }
    return true;
}

int dp[100001][26];

void solve()
{
    forn (i, 100000)
        forn (j, 26)
            dp[i + 1][j] = -1;
    int n = s.size();
    int ans = 0;
    forn (i, n)
    {
        forn (j, 26)
        {
            if (!pr[j][s[i] - 'a'])
                dp[i + 1][s[i] - 'a'] = max(dp[i + 1][s[i] - 'a'], dp[i][j] + 1);
            dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
        }
    }
    forn (i, 26)
        ans = max(ans, dp[n][i]);
    cout << ans << endl;
}

int main()
{
#ifdef Baster
    freopen("input.txt", "r", stdin);
//  freopen("output.txt", "w", stdout);
#endif
    while(read())
        solve();
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;

int N, P;
int Forbid[27][27];
char Buf[100000 + 10], Tmp[20];
int DP[100000 + 10][27];

int main()
{
	scanf("%s", Buf);
	scanf("%d", &P);
	while (P --)
	{
		scanf("%s", Tmp);
		Forbid[Tmp[0] - 'a' + 1][Tmp[1] - 'a' + 1] = 1;
		Forbid[Tmp[1] - 'a' + 1][Tmp[0] - 'a' + 1] = 1;
	}
	N = strlen(Buf);
	memset(DP, 1, sizeof(DP));
	DP[0][0] = 1;
	DP[0][Buf[0] - 'a' + 1] = 0;
	for (int i = 0; i < N - 1; i ++)
		for (int j = 0; j < 27; j ++)
		{
			DP[i + 1][j] = min(DP[i + 1][j], DP[i][j] + 1);
			if (! Forbid[j][Buf[i + 1] - 'a' + 1])
				DP[i + 1][Buf[i + 1] - 'a' + 1] = min(DP[i + 1][Buf[i + 1] - 'a' + 1], DP[i][j]);
		}
	int Ans = 1000000000;
	for (int j = 0; j < 27; j ++)
		Ans = min(Ans, DP[N - 1][j]);
	printf("%d\n", Ans);
	return 0;
}


#include <iostream>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <math.h>
#include <queue>
#include <string>
#include <fstream>

#define forn(i, n) for (int i = 0; i < n; i++)
using namespace std;

int n;

map<pair<char, char>, int>m;

pair<char, char>b[15];

int main()
{
    // ifstream cin("input.txt");
     //ofstream cout("outout.txt");

     string s;

     cin >> s >> n;

     forn(i, n){
         string h;
         cin >> h;
         m[make_pair(h[0], h[1])] = i + 1;
         m[make_pair(h[1], h[0])] = i + 1;
         b[i + 1] = make_pair(h[0], h[1]);
     }

     int ans = 0;

     for (int i = 1; i <= n; i++){
         forn(j, s.length()){
             int t1 = 0, t2 = 0;
             if (s[j] == b[i].first || s[j] == b[i].second){
                 if(s[j] == b[i].first)
                     t1++;
                 else
                     t2++;

                 for (int k = j+1; k < s.length(); k++)
                     if (s[k] == b[i].first){
                         t1++;
                         j++;
                     }
                     else
                         if (s[k] == b[i].second){
                             t2++;
                             j++;
                         }
                         else
                             break;
                 ans += min(t1, t2
                     );
             }
         }
     }

     cout << ans;

    return 0;
}


#pragma comment(linker, "/STACK:500000000")
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <sstream>
#include <ctype.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <math.h>
#include <time.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define Int long long
#define INF 0x3F3F3F3F
#define eps 1e-9
using namespace std;

char A[100009], buf[11], B[100009];
int d[100009];

void er(char c, int & len)
{
	int n = 0, i;
	for(i = 0; i < len; i++)
		if (A[i] != c)
			B[n++] = A[i];
	len = n;
	for(i = 0; i < n; i++)
		A[i] = B[i];
	A[i] = 0;
}

int CNT = 0;

void pp(char c, int L, int R)
{
	while(L < R)
	{
		if (A[L] == c)
		{
			A[L] = 0;
			CNT++;
		}
		L++;
	}
}

int main()
{
	gets(A);
	int i, len = strlen(A), n;
	scanf("%d", &n);
	for(i = 0; i < n; i++)
	{
		scanf("%s", buf);
		memset(d, 0, sizeof(d));
		d[0] = A[0] == buf[0] || A[0] == buf[1];
		for(int j = 1; j < len; j++)
		{
			if (A[j] == buf[0] || A[j] == buf[1])
				d[j] = d[j - 1] + 1;
			else
				d[j] = 0;
		}
		int prev = -1;
		for(int j = 1; j <= len; j++)
		{
			if (d[j] == 0 && d[j - 1] != 0)
			{
				int cntA = 0, cntB = 0;
				for(int k = j - 1; k > prev; k--)
				{
					if (buf[0] == A[k])
						cntA++;
					else
						cntB++;
				}
				if (cntA < cntB)
					pp(buf[0], prev + 1, j);
				else
					pp(buf[1], prev + 1, j);
			}
			if (d[j] == 0)
				prev = j;
		}
		er(0, len);
	}
	printf("%d\n", CNT);
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int n, k;
char str[100009], pairs[15][9];

void ReadData()
{
  scanf("%s%*c", str);
  n = strlen(str);
  scanf("%d%*c", &k);
  for(int i = 0; i < k; i++)
  {
    scanf("%s%*c", pairs[i]);
  }
}

int answer;
bool excluded[100009];

void Solve()
{
  answer = 0;
  memset(excluded, 0, sizeof(excluded));
  for(int i = 0; i < k; i++)
  {
    int first = 0, second = 0, start = -1;
    for(int index = 0; index < n; index++)
    {
      if(excluded[index])
        continue;
      if(str[index] == pairs[i][0]
          || str[index] == pairs[i][1])
      {
        if(str[index] == pairs[i][0])
        {
          first++;
        }
        else
        {
          second++;
        }
        if(start == -1)
        {
          start = index;
        }
        continue;
      }
      if(start != -1)
      {
        int toDel = first < second ? 0 : 1;
        for(int j = start; j <= index; j++)
        {
          if(str[j] == pairs[i][toDel])
          {
            excluded[j] = true;
            answer++;
          }
        }
        first = 0;
        second = 0;
        start = -1;
      }
    }
    if(start != -1)
      {
        int toDel = first < second ? 0 : 1;
        for(int j = start; j < n; j++)
        {
          if(str[j] == pairs[i][toDel])
          {
            excluded[j] = true;
            answer++;
          }
        }
        first = 0;
        second = 0;
        start = -1;
      }
  }
}

void WriteData()
{
  printf("%d\n", answer);
}

int main()
{
  int QWE = 1;
  #ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  scanf("%d%*c", &QWE);
  #endif
  for(int T = 0; T < QWE; T++)
  {
    ReadData();
    Solve();
    WriteData();
  }
  return 0;
}


#if 1
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <cassert>
#include <ctime>
#include <iostream>
#include <sstream>
#include <utility>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair <int, int> pii;
typedef vector <int> veci;
typedef vector <veci> graph;
const LD pi = acos(- 1.0);
const LD eps = 1e-9;
const int inf = 1000 * 1000 * 1000;
const LL inf64 = LL(inf) * inf;

//template <class T> T xabs(const T &a) { if(a < 0) return -a; else return +a; }

#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define iss istringstream
#define oss ostringstream
#define dbg(x) {cerr << #x << " = " << x << endl;}
#define dbgv(x) {cerr << #x << " = {"; for (int _i = 0; _i < (x).size(); _i++) cerr << x[_i] << ", "; cerr << "}" << endl;}

int mod = 1e+9 + 7;
int main()
{
	string str;
    cin >> str;
    int k;
    cin >> k;
    set <string> strings;
    for (int i = 0; i < k; i++)
    {
        string s;
        cin >> s;
        strings.insert(s);
        reverse(s.begin(), s.end());
        strings.insert(s);
    }
    int n = str.size();
    vector <pair <char, int> > text;
    for (int i = 0; i < n; i++)
    {
        int cnt = 0;
        char q = str[i];
        while( i < n && str[i] == q)
        {
            cnt++;
            i++;
        }
        text.pb(mp(q, cnt));
        i--;
    }
    LL del_count = 0;
    int m = text.size();
    for (int i = 0; i < m - 1; i++)
    {
		int start = i;
		map <char, int> sp;
		int fl = 1;
		int not = 1;
		while (fl)
		{
			string st;
			st.pb(text[i].first);
			st.pb(text[i + 1].first);
			if (strings.count(st) != 0)
			{
				sp[text[i].first] += text[i].second;
				i++;
				if (i + 1 >= m)
					fl = 0;
				not = 0;
			}
			else
			{
				fl = 0;

			}
		}
		if (!not)
		{
			sp[text[i].first] += text[i].second;
			del_count += min(sp.begin()->second, (++sp.begin())->second);
		}
	}
    cout << del_count << endl;
    return 0;
}
/********************

**********/
#endif


    #include <cmath>
    #include <ctime>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>

    using namespace std;

	string coupleForChar(vector <string> cpls, char a)
	{
		string result = "";
		for (int j = 0; j < cpls.size(); j++)
		{
			if (cpls[j][0] == a || cpls[j][1] == a)
			{
				result = cpls[j];
				break;
			}
		}
		return result;
	}

     void main()
    {
		string s;
		cin >> s;
		int k;
		cin >> k;
		vector <string> couples;
		string temp;
		for (int i = 0; i < k; i++)
		{
			cin >> temp;
			couples.push_back(temp);
		}
		int result = 0;

		int step = 1;
		temp = "";
		char first;
		char second;
		int sum1;
		int sum2;
		for (int i = 0; i < s.length(); i++)
		{
			temp = coupleForChar(couples, s[i]);
			if (temp != "")
			{
				first = (s[i] == temp[0]) ? temp[0] : temp[1];
				second = (s[i] == temp[0]) ? temp[1] : temp[0];
				sum1 = 1;
				sum2 = 0;
				int j = i + 1;
				bool flag = true;
				while (flag)
				{
					if ( j >= s.length())
					{
						result += min(sum1, sum2);
						flag = false;
					}
					else if (s[j] == first)
					{
						sum1++;
						j++;
					}
					else if (s[j] == second)
					{
						sum2++;
						j++;
					}
					else
					{
						result+= min(sum1, sum2);
						flag = false;
					}
				}
				i = j - 1;
			}
		}

		cout << result;
    }


#include <stdio.h>
#include <vector>
#include <functional>
#include <algorithm>
#include <iostream>
#include <string>
#include <climits>

using namespace std;

int main()
{
    vector<vector<bool> > forbidden(26, vector<bool>(26, false));

    int forbiddenPairs;

    ios::sync_with_stdio(false);

    string s;
    getline(cin, s);

    cin >> forbiddenPairs;
    string dummy;
    getline(cin, dummy);

    for (int i = 0; i < forbiddenPairs; ++i)
    {
        string forbiddenPair;
        getline(cin, forbiddenPair);

        forbidden[forbiddenPair[0] - 'a'][forbiddenPair[1] - 'a'] =
        forbidden[forbiddenPair[1] - 'a'][forbiddenPair[0] - 'a'] =
            true;
    }

    vector <int> prices(26, 1);
    prices[s[0] - 'a'] = 0;

    for (int i = 1; i < (int)s.size(); ++i)
    {
        const int currentSymbol = s[i] - 'a';

        vector<int> nextPrices(26, INT_MAX);
        for (int k = 0; k < 26; ++k)
        {
            nextPrices[k] = prices[k] + 1;
            for (int j = 0; j < 26; ++j)
            {
                if (!forbidden[j][k])
                {
                    nextPrices[k] = min(
                        prices[j] + ((k == currentSymbol) ? 0 : 1),
                        nextPrices[k]);
                }
            }
        }

//        for (int i = 0; i < prices.size(); ++i)
//        {
//          cout << prices[i] << " ";
//        }
//        cout << endl;

        prices = nextPrices;
    }

    cout << *min_element(prices.begin(), prices.end()) << endl;

    return 0;
}


#include <stdio.h>
#include <vector>
#include <functional>
#include <algorithm>
#include <iostream>
#include <string>
#include <climits>

using namespace std;

int main()
{
    vector<vector<bool> > forbidden(26, vector<bool>(26, false));

    int forbiddenPairs;

    ios::sync_with_stdio(false);

    string s;
    getline(cin, s);

    cin >> forbiddenPairs;
    string dummy;
    getline(cin, dummy);

    for (int i = 0; i < forbiddenPairs; ++i)
    {
        string forbiddenPair;
        getline(cin, forbiddenPair);

        forbidden[forbiddenPair[0] - 'a'][forbiddenPair[1] - 'a'] =
        forbidden[forbiddenPair[1] - 'a'][forbiddenPair[0] - 'a'] =
            true;
    }

    vector <int> prices(26, 1);
    prices[s[0] - 'a'] = 0;

    for (int i = 1; i < (int)s.size(); ++i)
    {
        const int currentSymbol = s[i] - 'a';

        vector<int> nextPrices(26, INT_MAX);
        for (int k = 0; k < 26; ++k)
        {
            nextPrices[k] = prices[k] + 1;
        }
        for (int j = 0; j < 26; ++j)
        {
            if (!forbidden[j][currentSymbol])
            {
                nextPrices[currentSymbol] = min(prices[j], nextPrices[currentSymbol]);
            }
        }

//        for (int i = 0; i < prices.size(); ++i)
//        {
//          cout << prices[i] << " ";
//        }
//        cout << endl;

        prices = nextPrices;
    }

    cout << *min_element(prices.begin(), prices.end()) << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    string s; cin >> s;
    int n, r = 0; cin >> n;
    vector<pair<char, char>> banned(n);
    for (int i = 0; i<n; i++) {
        char c1, c2; cin >> c1 >> c2;
        banned[i] = make_pair(c1, c2);
    }
    char prev1, prev2, cur, series;
    int k1, k2;
    prev1 = s[0];   k1 = 1;
    prev2 = 0;      k2 = 0;
    series = 0;
    for (int i = 1; i<s.size(); i++) {
        cur = s[i];

        if (cur==prev1) {k1++; continue;}
        if (cur==prev2) {k2++; continue;}

        if (prev2==0) {
            prev2 = cur;
            pair<char, char> p1 = make_pair(prev1, prev2), p2 = make_pair(prev2, prev1);
            for (int j = 0; j<n; j++) {
                if (banned[j] == p1 || banned[j] == p2) {series = 1; k2++; break;}
            }
            if (series == 0) {
                prev1 = cur;    k1 = 1;
                prev2 = 0;      k2 = 0;
            }
        }

        if (cur != prev1 && cur != prev2) {
            //конец серии
            series = 0;
            r += min(k1, k2);
            prev1 = cur;    k1 = 1;
            prev2 = 0;      k2 = 0;
        }
    }
    if (series==1) {r += min(k1, k2);}

    cout << r;

    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    string s; cin >> s;
    int n, r = 0; cin >> n;
    vector<pair<char, char>> banned(n);
    for (int i = 0; i<n; i++) {
        char c1, c2; cin >> c1 >> c2;
        banned[i] = make_pair(c1, c2);
    }
    char prev1, prev2, cur, series;
    int k1, k2;
    prev1 = s[0];   k1 = 1;
    prev2 = 0;      k2 = 0;
    series = 0;
    for (int i = 1; i<s.size(); i++) {
        cur = s[i];

        if (cur==prev1) {k1++; continue;}
        if (cur==prev2) {k2++; continue;}

        if (prev2==0) {
            prev2 = cur;
            pair<char, char> p1 = make_pair(prev1, prev2), p2 = make_pair(prev2, prev1);
            for (int j = 0; j<n; j++) {
                if (banned[j] == p1 || banned[j] == p2) {series = 1; k2++; break;}
            }
            if (series == 0) {prev1 = cur; prev2 = 0;}
        }

        if (cur != prev1 && cur != prev2) {
            //конец серии
            series = 0;
            r += min(k1, k2);
            prev1 = cur;    k1 = 1;
            prev2 = 0;      k2 = 0;
        }
    }
    if (series==1) {r += min(k1, k2);}

    cout << r;

    return 0;
}


#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen ("output.txt", "w", stdout);
    long long n;
    string s;
    cin >> s;
    cin >> n;
    long long res=0;
    for (long long i=0; i<n; i++)
    {
        string pa;
        cin >> pa;
        for (long long j=0; j<s.length()-1; j++)
        {
            if (((s[j]==pa[0])&&(s[j+1]==pa[1])) || ((s[j]==pa[1])&&(s[j+1]==pa[0])))
            {
                long long offs=0;
                while (true)
                {
                    if ((j-offs<0) || (j+1+offs>=s.length())) break;
                    if ((s[j]==s[j-offs]) && (s[j+1]==s[j+1+offs])) offs++;
                    else break;
                }
                res+=offs;
                j+=offs;
            }
        }
    }
    cout << res << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen ("output.txt", "w", stdout);
    long long n;
    string s;
    cin >> s;
    cin >> n;
    long long res=0;
    for (long long i=0; i<n; i++)
    {
        string pa;
        cin >> pa;
        for (long long j=0; j<s.length()-1; j++)
        {
            if (((s[j]==pa[0])&&(s[j+1]==pa[1])) || ((s[j]==pa[1])&&(s[j+1]==pa[0])))
            {
                long long offs=0;
                while (true)
                {
                    if ((j-offs<0) || (j+1+offs>=s.length())) break;
                    if ((s[j]==s[j-offs]) && (s[j+1]==s[j+1+offs])) offs++;
                    else break;
                }
                res+=offs;
                if ((j-offs>=0)&&(s[j]==s[j-offs]))
                {
                    s.erase(j+1, offs);
                }
                else
                {
                    s.erase(j-offs+1, offs);
                }
            }
        }
    }
    cout << res << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen ("output.txt", "w", stdout);
    long long n;
    string s;
    cin >> s;
    cin >> n;
    long long res=0;
    for (long long i=0; i<n; i++)
    {
        string pa;
        cin >> pa;
        int en=0;
            int count0=0;
            int count1=0;
            while (en<s.length())
            {
                if (s[en]==pa[0])
                {
                    count0++;
                }
                else if (s[en]==pa[1])
                {
                    count1++;
                }
                else
                {
                    res+=min(count0,count1);
                    count0=0;
                    count1=0;
                }
                en++;
            }
        res+=min(count0,count1);
    }
    cout << res << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen ("output.txt", "w", stdout);
    int n;
    string s;
    cin >> s;
    cin >> n;
    int res=0;
    for (int i=0; i<n; i++)
    {
        string pa;
        cin >> pa;
        for (int j=0; j<s.length()-1; j++)
        {
            if (((s[j]==pa[0])&&(s[j+1]==pa[1])) || ((s[j]==pa[1])&&(s[j+1]==pa[0])))
            {
                int offs=0;
                while (true)
                {
                    if ((j-offs<0) || (j+1+offs>=s.length())) break;
                    if ((s[j]==s[j-offs]) && (s[j+1]==s[j+1+offs])) offs++;
                    else break;
                }
                res+=offs;
                j+=offs;
            }
        }
    }
    cout << res << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen ("output.txt", "w", stdout);
    int n;
    string s;
    cin >> s;
    cin >> n;
    int res=0;
    for (int i=0; i<n; i++)
    {
        string pa;
        cin >> pa;
        for (int j=0; j<s.length()-1; j++)
        {
            if (((s[j]==pa[0])&&(s[j+1]==pa[1])) || ((s[j]==pa[1])&&(s[j+1]==pa[0])))
            {
                int offs=0;
                while (true)
                {
                    if ((j-offs<0) || (j+1+offs>=s.length())) break;
                    if ((s[j]==s[j-offs]) && (s[j+1]==s[j+1+offs])) offs++;
                    else break;
                }
                res+=offs;
                j+=1+offs;
            }
        }
    }
    cout << res << endl;
    return 0;
}


#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <map>
#include <set>
#include <utility>
#include <algorithm>
#include <queue>
#include <stack>
#include <deque>
#include <vector>

using namespace std;

#pragma comment (linker, "/STACK:64000000")

string str, stmp[13], str2;
int k, count = 0;
bool g[26][26] = {0};

bool is_forbidden(char c1, char c2)
{
    return g[c1-'a'][c2-'a'];
}

int count_it_4_me(int l, int r, int q)
{
    int r1=0,r2=0;
    for (;l<=r;++l)
    {
        if (str[l] == stmp[q][0])
            ++r1;
        else if (str[l] == stmp[q][1])
            ++r2;
    }
    return min(r1,r2);
}

int main()
{
    /*freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    */
    cin >> str;
    cin >> k;
    if (!k)
    {
        cout << 0;
        return 0;
    }

    for (int i = 0; i < k; ++i)
    {
        cin >> stmp[i];
        g[stmp[i][0]-'a'][stmp[i][1]-'a'] = g[stmp[i][1]-'a'][stmp[i][0]-'a'] = 1;
    }

    str2 = str;
    int res = 0;
    for (int i = 0; i < k; ++i)
    {
        int left = -1, right = -1;
        for (int j = 0; j < str.length(); ++j)
        {
            if (str[j] == stmp[i][0] || str[j] == stmp[i][1])
            {
                if (left == -1)
                    left = j, right = j;
                else
                    ++right;
            }
            else if (left != -1)
            {
                res += count_it_4_me(left, right, i);
                left = -1, right = -1;
            }

            if (left != -1 && j+1 == str.length())
                res += count_it_4_me(left, right, i);
        }
    }

    cout << res;
    return 0;
}


#include <iostream>
#include <string>

using namespace std;

//ifstream cin("input.txt");
//ofstream cout("output.txt");

string pr[14];

bool nottwosym(char a, char b, int n)
{
    for (int i=0; i<n; i++)
    {
        if (pr[i][0]==a && pr[i][1]==b || pr[i][0]==b && pr[i][1]==a)
            return 0;
    }
    return 1;
}

int main()
{
    string s;
    cin >> s;
    int n, cnt1, cnt2, j, res=0, xz;
    char ch;
    cin >> n;
    for (int i=0; i<n; i++)
        cin >> pr[i];
    ch=s[0];
    cnt1=cnt2=1;
    xz=0;
    for (int i=0; i<s.length()-1;)
    {
        if (s[i]==s[i+1])
        {
            cnt1++;
            xz++;
        }
        if (nottwosym(s[i],s[i+1], n))
        {
            if (s[i]!=s[i+1])
            {
                cnt1=1;
                xz=0;
            }
            i++;
        }
        else
        {
            j=i+2;
            while (j<s.length() && (s[j]==s[i] || s[j]==s[i+1]))
            {
                if (s[j]==s[i])
                    cnt1++;
                else
                    cnt2++;
                j++;
            }
            res+=min(cnt1,cnt2);
            i+=cnt1+cnt2-xz;
            cnt1=cnt2=1;
            xz=0;
        }
    }
    cout << res;
}


#include <iostream>
#include <string>

using namespace std;

//ifstream cin("input.txt");
//ofstream cout("output.txt");

string pr[14];

bool nottwosym(char a, char b, int n)
{
    for (int i=0; i<n; i++)
    {
        if (pr[i][0]==a && pr[i][1]==b || pr[i][0]==b && pr[i][1]==a)
            return 0;
    }
    return 1;
}

int main()
{
    string s;
    cin >> s;
    int n, cnt1, cnt2, j, res=0;
    cin >> n;
    for (int i=0; i<n; i++)
        cin >> pr[i];
    cnt1=cnt2=1;
    for (int i=0; i<s.length()-1;)
    {
        if (s[i]==s[i+1])
            cnt1++;
        if (nottwosym(s[i],s[i+1], n))
        {
            i++;
        }
        else
        {
            j=i+2;
            while (j<s.length() && (s[j]==s[i] || s[j]==s[i+1]))
            {
                if (s[j]==s[i])
                    cnt1++;
                else
                    cnt2++;
                j++;
            }
            res+=min(cnt1,cnt2);
            i+=cnt1+cnt2;
            cnt1=cnt2=1;
        }
    }
    cout << res;
}


#include <iostream>
#include <string>

using namespace std;

//ifstream cin("input.txt");
//ofstream cout("output.txt");

string pr[14];

bool nottwosym(char a, char b, int n)
{
    for (int i=0; i<n; i++)
    {
        if (pr[i][0]==a && pr[i][1]==b || pr[i][0]==b && pr[i][1]==a)
            return 0;
    }
    return 1;
}

int main()
{
    string s;
    cin >> s;
    int n, cnt1, cnt2, j, res=0;
    char ch=s[0];
    cin >> n;
    for (int i=0; i<n; i++)
        cin >> pr[i];
    cnt1=cnt2=1;
    for (int i=0; i<s.length()-1;)
    {
        if (s[i]==s[i+1])
        {
            if (s[i]!=ch)
            {
                cnt1=2;
                ch=s[i];
            }
            else
                cnt1++;
        }
        if (nottwosym(s[i],s[i+1], n))
        {
            i++;
        }
        else
        {
            j=i+2;
            while (j<s.length() && (s[j]==s[i] || s[j]==s[i+1]))
            {
                if (s[j]==s[i])
                    cnt1++;
                else
                    cnt2++;
                j++;
            }
            res+=min(cnt1,cnt2);
            i+=cnt1+cnt2;
            cnt1=cnt2=1;
        }
    }
    cout << res;
}


#include <iostream>
#include <string>

using namespace std;

//ifstream cin("input.txt");
//ofstream cout("output.txt");

string pr[14];

bool twosym(char a, char b, int n)
{
    for (int i=0; i<n; i++)
    {
        if (pr[i][0]==a && pr[i][1]==b || pr[i][0]==b && pr[i][1]==a)
            return 1;
    }
    return 0;
}

int main()
{
    string s;
    cin >> s;
    int n, cnt1, cnt2, j, res=0;
    cin >> n;
    for (int i=0; i<n; i++)
        cin >> pr[i];
    for (int i=0; i<s.length()-1;)
    {
        if (twosym(s[i],s[i+1], n))
        {
            cnt1=cnt2=1;
            j=i+2;
            while (j<s.length() && (s[j]==s[i] || s[j]==s[i+1]))
            {
                if (s[j]==s[i])
                    cnt1++;
                else
                    cnt2++;
                j++;
            }
            res+=min(cnt1,cnt2);
            i+=cnt1+cnt2;
        }
        else
            i++;
    }
    cout << res;
}


#include <iostream>
#include <string>

using namespace std;

//ifstream cin("input.txt");
//ofstream cout("output.txt");

string pr[14];

bool nottwosym(char a, char b, int n)
{
    for (int i=0; i<n; i++)
    {
        if (pr[i][0]==a && pr[i][1]==b || pr[i][0]==b && pr[i][1]==a)
            return 0;
    }
    return 1;
}

int main()
{
    string s;
    cin >> s;
    int n, cnt1, cnt2, j, res=0, xz;
    char ch;
    cin >> n;
    for (int i=0; i<n; i++)
        cin >> pr[i];
    ch=s[0];
    cnt1=cnt2=1;
    xz=0;
    for (int i=0; i<s.length()-1;)
    {
        if (s[i]==s[i+1])
        {
            cnt1++;
            xz++;
        }
        if (nottwosym(s[i],s[i+1], n))
        {
            i++;
            if (s[i]!=s[i+1])
            {
                cnt1=1;
                xz=0;
            }
        }
        else
        {
            j=i+2;
            while (j<s.length() && (s[j]==s[i] || s[j]==s[i+1]))
            {
                if (s[j]==s[i])
                    cnt1++;
                else
                    cnt2++;
                j++;
            }
            res+=min(cnt1,cnt2);
            i+=cnt1+cnt2-xz;
            cnt1=cnt2=1;
            xz=0;
        }
    }
    cout << res;
}


#include <iostream>
#include <string>

using namespace std;

//ifstream cin("input.txt");
//ofstream cout("output.txt");

string pr[14];

bool nottwosym(char a, char b, int n)
{
    for (int i=0; i<n; i++)
    {
        if (pr[i][0]==a && pr[i][1]==b || pr[i][0]==b && pr[i][1]==a)
            return 0;
    }
    return 1;
}

int main()
{
    string s;
    cin >> s;
    int n, cnt1, cnt2, j, res=0, xz;
    cin >> n;
    for (int i=0; i<n; i++)
        cin >> pr[i];
    cnt1=cnt2=1;
    xz=0;
    for (int i=0; i<s.length()-1;)
    {
        if (s[i]==s[i+1])
        {
            cnt1++;
            xz++;
        }
        if (nottwosym(s[i],s[i+1], n))
        {
            i++;
        }
        else
        {
            j=i+2;
            while (j<s.length() && (s[j]==s[i] || s[j]==s[i+1]))
            {
                if (s[j]==s[i])
                    cnt1++;
                else
                    cnt2++;
                j++;
            }
            res+=min(cnt1,cnt2);
            i+=cnt1+cnt2-xz;
            cnt1=cnt2=1;
            xz=0;
        }
    }
    cout << res;
}


#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>

using namespace std;

int main() {
    string s; cin >> s;
    int k; cin >> k;
    map<char, char> f;
    for (int i=0; i < k; i++) {
        char a,b; cin >> a >> b;
        f[a] = b;
        f[b] = a;
    }

    char x = ' ', y = ' ';
    int nx = 0, ny = 0, nc = 0, count = 0;
    for (int i=0; i < s.size(); i++ ){
        if (x == ' ') {
            x = s[i];
            nx = 1;
        }
        else if (y == ' ') {
            y = s[i];
            ny = 1;
        }
        else if (s[i] == x) nx++;
        else if (s[i] == y) ny++;
        else if (s[i] != x && s[i] != y) {
            if (f[x] == y) {
                count += min(nx, ny);
            }
            x = s[i-1];
            nx = nc;
            y = s[i];
            ny = 1;
        }
        if (i != 0 && s[i] == s[i-1]) {
            nc++;
        } else {
            nc = 1;
        }
    }
    if (f[x] == y) {
        count += min(nx, ny);
    }
    cout << count << endl;
    return 0;
}


#include <iostream>
#include <fstream>
#include <cstdio>

#include <cmath>
#include <cstring>
#include <cassert>
#include <ctime>
#include <cctype>

#include <algorithm>
#include <utility>
#include <functional>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>

#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

const int MAXN = 1000 + 1;
const int INF = 1000000000;

string s;
int n;

int calc(int begin, int end)
{
    int fa, fb;
    fa = 0;
    fb = 0;

    for (int i = begin; i <= end; i++)
    {
        if (s[i] == s[begin])
        {
            fa++;
        }
        else
        {
            fb++;
        }
    }

    return (fa < fb) ? fa : fb;
}

int main()
{
    map<char, char> f;

    cin >> s >> n;

    for (int i = 0; i < n; i++)
    {
        char a, b;
        cin >> a >> b;
        f[a] = b;
        f[b] = a;
    }

    char last = s[0];
    int begin = 0;
    int end = 0;

    int ans = 0;

    for (int i = 1; i < s.length(); i++)
    {
        if (f[s[i]] == last)
        {
            end++;
        }
        else
        {
            ans += calc(begin, end);
            begin = end = i;
        }
        last = s[i];
    }

    if (begin == 0 && end != 0)
    {
        ans += calc(begin, end);
    }

    cout << ans;

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

int m[256][256];

int check(int a)
{
    for(int i = 'a'; i <= 'z'; i++)
        if(m[a][i])  return i;
    return 0;
}

int main()
{
    string s;
    cin >> s;

    int n = s.length();

    int k;
    cin >> k;

    memset(m, 0, sizeof(m));
    for(int i = 0; i < k; i++)
    {
        string t;
        cin >> t;

        m[t[0]][t[1]] = 1;
        m[t[1]][t[0]] = 1;
    }

    int i = 0, cnt = 1, ans = 0, beg = s[0];
    int a = 1, b = 0;
    while(i + 1 < n)
    {
        if(s[i + 1] == s[i]
        || s[i + 1] == check(s[i]))
        {
            if(s[i + 1] == beg)  a++;
            else  b++;
        }
        else
        {
            ans += min(a, b);
            a = 1, b = 0;
            beg = s[i + 1];
            //ans += (cnt / 2);
            //cnt = 1;
        }
        i++;
    }

    ans += min(a, b);

    cout << ans << endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

int m[256][256];

int main()
{
    string s;
    cin >> s;

    int n = s.length();

    int k, ans = 1;
    cin >> k;

    memset(m, 0, sizeof(m));
    for(int i = 0; i < k; i++)
    {
        string t;
        cin >> t;

        m[t[0]][t[1]] = 1;
        m[t[1]][t[0]] = 1;
    }

    int beg = s[0];
    for(int i = 1; i < n; i++)
    {
        if(m[s[i]][beg])  continue;

        beg = s[i];
        ans++;
    }

    cout << n - ans << endl;

    /*
    int i = 0, cnt = 1, ans = 0;
    while(i < n)
    {
        if(i + 1 < n && m[s[i]][s[i + 1]])
        {
            cnt++;
        }
        else
        {
            ans += (cnt / 2);
            cnt = 1;
        }
        i++;
    }

    ans += (cnt / 2);
    */

    //cout << ans << endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

int m[256][256];

int main()
{
    string s;
    cin >> s;

    int n = s.length();

    int k;
    cin >> k;

    memset(m, 0, sizeof(m));
    for(int i = 0; i < k; i++)
    {
        string t;
        cin >> t;

        m[t[0]][t[1]] = 1;
        m[t[1]][t[0]] = 1;
    }

    int i = 0, cnt = 1, ans = 0;
    while(i < n)
    {
        if(i + 1 < n && m[s[i]][s[i + 1]])
        {
            cnt++;
        }
        else
        {
            ans += (cnt / 2);
            cnt = 1;
        }
        i++;
    }

    ans += (cnt / 2);

    cout << ans << endl;
}


#include<stdio.h>
#include<string.h>

#define N 100000
#define K 13

int n, ans, check[N+10][3], k, list[N+10];
char data[N+10], pair[K+10][3];

int main(){
	int i, j, cnt, recent, even, odd;
	char q, w;
	scanf("%s",&data);
	n = strlen(data);
	scanf("%d",&k);
	for(i=0;i<k;i++)
		scanf("%s",&pair[i]);
	for(i=0;i<n-1;i++){
		check[i][2] = -1;
		for(j=0;j<k;j++){
			if((data[i] == pair[j][0] && data[i+1] == pair[j][1]) || (data[i+1] == pair[j][0] && data[i] == pair[j][1])){
				check[i][1] = check[i+1][1] = check[i][0] = check[i+1][0] = 1;

				break;
			}
		}
	}
	check[i][2] = -1;
	for(i=0;i<n;i++){
		if(check[i][0] == 1 && check[i][2] == -1){
			check[i][2] = i;
			for(j=i-1;j>=0;j--){
				if(data[j] == data[i]){
					check[i][1] ++;
					check[j][0] = 1;
					check[j][2] = i;
				}
				else
					break;
			}
			for(j=i+1;j<n;j++){
				if(data[j] == data[i]){
					check[i][1] ++;
					check[j][0] = 1;
					check[j][2] = i;
				}
				else
					break;
			}
		}
	}
	cnt = 0;
	recent = -1;
	q = w = ' ';
	for(i=0;i<n;i++){
		if(check[i][0] == 0 || (cnt >= 2 &&  data[i] != q && data[i] != w)){
			even = odd = 0;
			for(j=0;j<cnt;j++){
				if(j % 2 == 0)
					even += check[list[j]][1];
				else
					odd += check[list[j]][1];
			}
			if(even > odd)
				ans += odd;
			else
				ans += even;
			recent = -1;
			cnt = 0;
		}
		else if(check[i][0] == 1 && recent != check[i][2]){
			if(cnt == 0)
				q = data[i];
			else if(cnt == 1 && q != data[i])
				w = data[i];
			list[cnt++] = check[i][2];
			recent = check[i][2];
		}
	}
	if(cnt > 0){
		even = odd = 0;
		for(j=0;j<cnt;j++){
			if(j % 2 == 0)
				even += check[list[j]][1];
			else
				odd += check[list[j]][1];
		}
		if(even > odd)
			ans += odd;
		else
			ans += even;
		recent = -1;
		cnt = 0;
	}
	printf("%d\n",ans);
	return 0;
}


#include<stdio.h>
#include<string.h>

#define N 100000
#define K 13

int n, ans, check[N+10][3], k, list[N+10];
char data[N+10], pair[K+10][3];

int main(){
	int i, j, cnt, recent, even, odd;
	scanf("%s",&data);
	n = strlen(data);
	scanf("%d",&k);
	for(i=0;i<k;i++)
		scanf("%s",&pair[i]);
	for(i=0;i<n-1;i++){
		check[i][2] = -1;
		for(j=0;j<k;j++){
			if((data[i] == pair[j][0] && data[i+1] == pair[j][1]) || (data[i+1] == pair[j][0] && data[i] == pair[j][1])){
				check[i][1] = check[i+1][1] = check[i][0] = check[i+1][0] = 1;

				break;
			}
		}
	}
	check[i][2] = -1;
	for(i=0;i<n;i++){
		if(check[i][0] == 1 && check[i][2] == -1){
			check[i][2] = i;
			for(j=i+1;j<n;j++){
				if(data[j] == data[i]){
					check[i][1] ++;
					check[j][0] = 1;
					check[j][2] = i;
				}
				else
					break;
			}
		}
	}
	cnt = 0;
	recent = -1;
	for(i=0;i<n;i++){
		if(check[i][0] == 1 && recent != check[i][2]){
			list[cnt++] = check[i][2];
			recent = check[i][2];
		}
		else if(check[i][0] == 0){
			even = odd = 0;
			for(j=0;j<cnt;j++){
				if(j % 2 == 0)
					even += check[list[j]][1];
				else
					odd += check[list[j]][1];
			}
			if(even > odd)
				ans += odd;
			else
				ans += even;
			recent = -1;
			cnt = 0;
		}
	}
	if(cnt > 0){
		even = odd = 0;
		for(j=0;j<cnt;j++){
			if(j % 2 == 0)
				even += check[list[j]][1];
			else
				odd += check[list[j]][1];
		}
		if(even > odd)
			ans += odd;
		else
			ans += even;
		recent = -1;
		cnt = 0;
	}
	printf("%d\n",ans);
	return 0;
}


#include<stdio.h>
#include<string.h>

#define N 100000
#define K 13

int n, ans, check[N+10][3], k, list[N+10];
char data[N+10], pair[K+10][3];

int main(){
	int i, j, cnt, recent, even, odd;
	char q, w;
	scanf("%s",&data);
	n = strlen(data);
	scanf("%d",&k);
	for(i=0;i<k;i++)
		scanf("%s",&pair[i]);
	for(i=0;i<n-1;i++){
		check[i][2] = -1;
		for(j=0;j<k;j++){
			if((data[i] == pair[j][0] && data[i+1] == pair[j][1]) || (data[i+1] == pair[j][0] && data[i] == pair[j][1])){
				check[i][1] = check[i+1][1] = check[i][0] = check[i+1][0] = 1;

				break;
			}
		}
	}
	check[i][2] = -1;
	for(i=0;i<n;i++){
		if(check[i][0] == 1 && check[i][2] == -1){
			check[i][2] = i;
			for(j=i-1;j>=0;j--){
				if(data[j] == data[i]){
					check[i][1] ++;
					check[j][0] = 1;
					check[j][2] = i;
				}
				else
					break;
			}
			for(j=i+1;j<n;j++){
				if(data[j] == data[i]){
					check[i][1] ++;
					check[j][0] = 1;
					check[j][2] = i;
				}
				else
					break;
			}
		}
	}
	cnt = 0;
	recent = -1;
	q = w = ' ';
	for(i=0;i<n;i++){
		if(check[i][0] == 0 || (cnt >= 2 &&  data[i] != q && data[i] != w)){
			even = odd = 0;
			for(j=0;j<cnt;j++){
				if(j % 2 == 0)
					even += check[list[j]][1];
				else
					odd += check[list[j]][1];
			}
			if(even > odd)
				ans += odd;
			else
				ans += even;
			recent = -1;
			cnt = 0;
		}
		if(check[i][0] == 1 && recent != check[i][2]){
			if(cnt == 0)
				q = data[i];
			else if(cnt == 1 && q != data[i])
				w = data[i];
			list[cnt++] = check[i][2];
			recent = check[i][2];
		}
	}
	if(cnt > 0){
		even = odd = 0;
		for(j=0;j<cnt;j++){
			if(j % 2 == 0)
				even += check[list[j]][1];
			else
				odd += check[list[j]][1];
		}
		if(even > odd)
			ans += odd;
		else
			ans += even;
		recent = -1;
		cnt = 0;
	}
	printf("%d\n",ans);
	return 0;
}


#include<stdio.h>
#include<string.h>

#define N 100000
#define K 13

int n, ans, check[N+10][3], k, list[N+10];
char data[N+10], pair[K+10][3];

int main(){
	int i, j, cnt, recent, even, odd;
	char q, w;
	scanf("%s",&data);
	n = strlen(data);
	scanf("%d",&k);
	for(i=0;i<k;i++)
		scanf("%s",&pair[i]);
	for(i=0;i<n-1;i++){
		check[i][2] = -1;
		for(j=0;j<k;j++){
			if((data[i] == pair[j][0] && data[i+1] == pair[j][1]) || (data[i+1] == pair[j][0] && data[i] == pair[j][1])){
				check[i][1] = check[i+1][1] = check[i][0] = check[i+1][0] = 1;

				break;
			}
		}
	}
	check[i][2] = -1;
	for(i=0;i<n;i++){
		if(check[i][0] == 1 && check[i][2] == -1){
			check[i][2] = i;
			for(j=i-1;j>=0;j--){
				if(data[j] == data[i]){
					check[i][1] ++;
					check[j][0] = 1;
					check[j][2] = i;
				}
				else
					break;
			}
			for(j=i+1;j<n;j++){
				if(data[j] == data[i]){
					check[i][1] ++;
					check[j][0] = 1;
					check[j][2] = i;
				}
				else
					break;
			}
		}
	}
	cnt = 0;
	recent = -1;
	for(i=0;i<n;i++){
		if(check[i][0] == 0 && cnt >= 2 &&  data[i] != q && data[i] != w){
			even = odd = 0;
			for(j=0;j<cnt;j++){
				if(j % 2 == 0)
					even += check[list[j]][1];
				else
					odd += check[list[j]][1];
			}
			if(even > odd)
				ans += odd;
			else
				ans += even;
			recent = -1;
			cnt = 0;
		}
		else if(check[i][0] == 1 && recent != check[i][2]){
			if(cnt == 0)
				q = data[i];
			if(cnt > 0 && q != data[i])
				w = data[i];
			list[cnt++] = check[i][2];
			recent = check[i][2];
		}
	}
	if(cnt > 0){
		even = odd = 0;
		for(j=0;j<cnt;j++){
			if(j % 2 == 0)
				even += check[list[j]][1];
			else
				odd += check[list[j]][1];
		}
		if(even > odd)
			ans += odd;
		else
			ans += even;
		recent = -1;
		cnt = 0;
	}
	printf("%d\n",ans);
	return 0;
}


#include<stdio.h>
#include<string.h>

#define N 100000
#define K 13

int n, ans, check[N+10][3], k, list[N+10];
char data[N+10], pair[K+10][3];

int main(){
	int i, j, cnt, recent, even, odd;
	scanf("%s",&data);
	n = strlen(data);
	scanf("%d",&k);
	for(i=0;i<k;i++)
		scanf("%s",&pair[i]);
	for(i=0;i<n-1;i++){
		check[i][2] = -1;
		for(j=0;j<k;j++){
			if((data[i] == pair[j][0] && data[i+1] == pair[j][1]) || (data[i+1] == pair[j][0] && data[i] == pair[j][1])){
				check[i][1] = check[i+1][1] = check[i][0] = check[i+1][0] = 1;

				break;
			}
		}
	}
	check[i][2] = -1;
	for(i=0;i<n;i++){
		if(check[i][0] == 1 && check[i][2] == -1){
			check[i][2] = i;
			for(j=i-1;j>=0;j--){
				if(data[j] == data[i]){
					check[j][1] ++;
					check[j][0] = 1;
					check[j][2] = i;
				}
				else
					break;
			}
			for(j=i+1;j<n;j++){
				if(data[j] == data[i]){
					check[j][1] ++;
					check[j][0] = 1;
					check[j][2] = i;
				}
				else
					break;
			}
		}
	}
	cnt = 0;
	recent = -1;
	for(i=0;i<n;i++){
		if(check[i][0] == 1 && recent != check[i][2]){
			list[cnt++] = check[i][2];
			recent = check[i][2];
		}
		else if(check[i][0] == 0){
			even = odd = 0;
			for(j=0;j<cnt;j++){
				if(j % 2 == 0)
					even += check[list[j]][1];
				else
					odd += check[list[j]][1];
			}
			if(even > odd)
				ans += odd;
			else
				ans += even;
			recent = -1;
			cnt = 0;
		}
	}
	if(cnt > 0){
		even = odd = 0;
		for(j=0;j<cnt;j++){
			if(j % 2 == 0)
				even += check[list[j]][1];
			else
				odd += check[list[j]][1];
		}
		if(even > odd)
			ans += odd;
		else
			ans += even;
		recent = -1;
		cnt = 0;
	}
	printf("%d\n",ans);
	return 0;
}


#include <iostream>
#include <string>

using namespace std;

string s;

int main() {
	int k, res = 0;
	cin >> s >> k;
	for (int i = 0; i < k; i++) {
		string a;
		cin >> a;
		int j = 0;
		while (j < s.size()) {
			while (j < s.size() && a.find(s[j]) == -1) {
				j++;
			}
			if (j == s.size()) {
				continue;
			}
			int l = j;
			while (j < s.size() - 1 && a.find(s[j]) != -1 && a.find(s[j + 1]) != -1) {
				j++;
			}
			int b[2] = {0, 0}, r = j++;
			for (int jj = l; jj <= r; jj++) {
				if (s[jj] == a[0]) {
					b[0]++;
				} else {
					b[1]++;
				}
			}
			res += min(b[0], b[1]);
		}
	}
	cout << res << endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <math.h>

#define PI 3.14159265

using namespace std;

int main ()
{
    string slovo;
    __int64 n;
    cin>>slovo;
    cin>>n;
    __int64 ans = 0;
    string temp;
    for (int k=0; k<n; k++)
    {
        cin>>temp;
        for (int i=1; i<slovo.length(); i++)
        {
            int first = 0;
            int second = 0;
            while ( (slovo[i-1]==temp[0] || slovo[i-1]==temp[1]) && (slovo[i]==temp[0] || slovo[i]==temp[1]))
            {
                if (slovo[i-1] == temp[0])
                    first++;
                else
                    second++;
                i++;
                if (i>=slovo.length())
                    break;
            }
            if (slovo[i-1] == temp[0])
                    first++;
                else
                    second++;
            ans+=min(first,second);
            first = 0;
            second = 0;
        }
    }

    cout<<ans<<endl;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <sstream>

#define pb push_back
#define mp make_pair
#define PI 3.14159265358979
#define forn(i, n) for(int i = 0; i < n; ++i)
#define ALL(x) x.begin(), x.end()
#define L(s) (int)((s).size())
#define ms(x) memset(x,0,sizeof(x));

typedef long long ll;
using namespace std;
typedef pair<int,int> pii;
const int INF = 2147483647;
const ll LLINF = 9223372036854775807LL;
const int ST = 100010;
const int ST1 = 1000010;
const ll MOD = 1000000007;

ll ABS(ll a)
{
	if(a<0)
		return a*(-1);
	else
		return a;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
	string a;
	cin >> a;
	int k;
	cin >> k;
	vector <string> zapr(k);
	for(int i = 0;i < k;i++)
	{
		cin >> zapr[i];
	}
	ll count = 0;
	for(int i = 0;i < L(a);i++)
	{
		for(int j = 0;j < k;j++)
		{
			if(a[i] == zapr[j][0] || a[i] == zapr[j][1])
				count++;
		}
	}
	count/=2;
	cout << count;
}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <sstream>

#define pb push_back
#define mp make_pair
#define PI 3.14159265358979
#define forn(i, n) for(int i = 0; i < n; ++i)
#define ALL(x) x.begin(), x.end()
#define L(s) (int)((s).size())
#define ms(x) memset(x,0,sizeof(x));

typedef long long ll;
using namespace std;
typedef pair<int,int> pii;
const int INF = 2147483647;
const ll LLINF = 9223372036854775807LL;
const int ST = 100010;
const int ST1 = 1000010;
const ll MOD = 1000000007;

ll ABS(ll a)
{
	if(a<0)
		return a*(-1);
	else
		return a;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
	string a;
	cin >> a;
	int k;
	cin >> k;
	k*=2;
	vector <string> zapr(k);
	for(int i = 0;i < k;i+=2)
	{
		cin >> zapr[i];
		string ba;
		ba += zapr[i][1];
		ba += zapr[i][0];
		zapr[i+1] = ba;
	}
	ll count = 0;
	for(int i = 0;i < L(a)-1;i++)
	{
		for(int j = 0;j < k;j++)
		{
			if(i == L(a))
				break;
			if(a[i] == zapr[j][0] && a[i+1] == zapr[j][1])
			{
				int a1 = 0,a2 = 0;
				while(i < L(a) && (a[i] == zapr[j][0] || a[i] == zapr[j][1]))
				{
					if(a[i] == zapr[j][0])
						a1++;
					else
						a2++;
					i++;
				}
				count+=min(a1,a2);
			}
		}
	}
	cout << count;
}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <sstream>

#define pb push_back
#define mp make_pair
#define PI 3.14159265358979
#define forn(i, n) for(int i = 0; i < n; ++i)
#define ALL(x) x.begin(), x.end()
#define L(s) (int)((s).size())
#define ms(x) memset(x,0,sizeof(x));

typedef long long ll;
using namespace std;
typedef pair<int,int> pii;
const int INF = 2147483647;
const ll LLINF = 9223372036854775807LL;
const int ST = 100010;
const int ST1 = 1000010;
const ll MOD = 1000000007;

ll ABS(ll a)
{
	if(a<0)
		return a*(-1);
	else
		return a;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
	string a;
	cin >> a;
	int k;
	cin >> k;
	k*=2;
	vector <string> zapr(k);
	for(int i = 0;i < k;i+=2)
	{
		cin >> zapr[i];
		string ba;
		ba += zapr[i][1];
		ba += zapr[i][0];
		zapr[i+1] = ba;
	}
	ll count = 0;
	for(int i = 0;i < L(a)-1;i++)
	{
		for(int j = 0;j < k;j++)
		{
			if(a[i] == zapr[j][0] && a[i+1] == zapr[j][1])
			{
				count++;
				a[i] = '0';
				a[i+1] = '0';
			}
		}
	}
	cout << count;
}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <sstream>

#define pb push_back
#define mp make_pair
#define PI 3.14159265358979
#define forn(i, n) for(int i = 0; i < n; ++i)
#define ALL(x) x.begin(), x.end()
#define L(s) (int)((s).size())
#define ms(x) memset(x,0,sizeof(x));

typedef long long ll;
using namespace std;
typedef pair<int,int> pii;
const int INF = 2147483647;
const ll LLINF = 9223372036854775807LL;
const int ST = 100010;
const int ST1 = 1000010;
const ll MOD = 1000000007;

ll ABS(ll a)
{
	if(a<0)
		return a*(-1);
	else
		return a;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
	string a;
	cin >> a;
	int k;
	cin >> k;
	k*=2;
	vector <string> zapr(k);
	for(int i = 0;i < k;i+=2)
	{
		cin >> zapr[i];
		string ba;
		ba += zapr[i][1];
		ba += zapr[i][0];
		zapr[i+1] = ba;
	}
	ll count = 0;
	for(int i = 0;i < L(a)-1;i++)
	{
		for(int j = 0;j < k;j++)
		{
			if(i == L(a))
				break;
			int a1 = 0,a2 = 0;
			while(i < L(a) && (a[i] == zapr[j][0] || a[i] == zapr[j][1]))
			{
				if(a[i] == zapr[j][0])
					a1++;
				else
					a2++;
				i++;
			}
			count+=min(a1,a2);
		}
	}
	cout << count;
}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <sstream>

#define pb push_back
#define mp make_pair
#define PI 3.14159265358979
#define forn(i, n) for(int i = 0; i < n; ++i)
#define ALL(x) x.begin(), x.end()
#define L(s) (int)((s).size())
#define ms(x) memset(x,0,sizeof(x));

typedef long long ll;
using namespace std;
typedef pair<int,int> pii;
const int INF = 2147483647;
const ll LLINF = 9223372036854775807LL;
const int ST = 100010;
const int ST1 = 1000010;
const ll MOD = 1000000007;

ll ABS(ll a)
{
	if(a<0)
		return a*(-1);
	else
		return a;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
	string a;
	cin >> a;
	int k;
	cin >> k;
	vector <string> zapr(k);
	for(int i = 0;i < k;i++)
	{
		cin >> zapr[i];
	}
	ll count = 0;
	for(int i = 0;i < L(a);i++)
	{
		for(int j = 0;j < k;j++)
		{
			if(a[i] == zapr[k][0] || a[i] == zapr[k][1])
				count++;
		}
	}
	count/=2;
	cout << count;
}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <sstream>

#define pb push_back
#define mp make_pair
#define PI 3.14159265358979
#define forn(i, n) for(int i = 0; i < n; ++i)
#define ALL(x) x.begin(), x.end()
#define L(s) (int)((s).size())
#define ms(x) memset(x,0,sizeof(x));

typedef long long ll;
using namespace std;
typedef pair<int,int> pii;
const int INF = 2147483647;
const ll LLINF = 9223372036854775807LL;
const int ST = 100010;
const int ST1 = 1000010;
const ll MOD = 1000000007;

ll ABS(ll a)
{
	if(a<0)
		return a*(-1);
	else
		return a;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
	string a;
	cin >> a;
	int k;
	cin >> k;
	vector <string> zapr(k);
	for(int i = 0;i < k;i+=1)
	{
		cin >> zapr[i];
	}
	ll count = 0;
	for(int i = 0;i < L(a)-1;i++)
	{
		for(int j = 0;j < k;j++)
		{
			if(i == L(a))
				break;
			int a1 = 0,a2 = 0;
			bool was = false;
			while(i < L(a) && (a[i] == zapr[j][0] || a[i] == zapr[j][1]))
			{
				if(a[i] == zapr[j][0])
					a1++;
				else
					a2++;
				i++;
				was = true;
			}
			if(was)
				j = -1;
			count+=min(a1,a2);
		}
	}
	cout << count;
}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>
using namespace std;
int main ()
{
long long i,j,m,b,e1,e2,e3,mn,z1,z2,n,x;
string c[20];
string a,b1,b2,b3;
getline(cin,a);
cin>>n;
x=0;
for (i=0;i<n;i++)
cin>>c[i];
for (i=0;i<n;i++)
{
	z1=0;
	z2=0;
	for (j=0;j<a.length();j++)
	{
		if (a[j]==c[i][0]) z1++; else
		if (a[j]==c[i][1]) z2++; else
			if ((z1!=0) && (z2!=0)) {x=x+min(z1,z2);z1=0;z2=0;} else {z1=0;z2=0;}
	}
	if ((z1!=0) && (z2!=0)) {x=x+min(z1,z2);z1=0;z2=0;} else {z1=0;z2=0;}
}
cout<<x;
return 0;
}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>
using namespace std;
int main ()
{
long long i,j,m,b,e1,e2,e3,mn,n,x;
string c[20];
string a,b1,b2,b3;
getline(cin,a);
cin>>n;
x=0;
for (i=0;i<n;i++)
cin>>c[i];
for (i=0;i<n;i++)
{
	b1=c[i];
	b2=c[i];
	reverse(b2.begin(),b2.end());
	for (j=0;j<a.length();j++)
	{
		if (j==a.length()-1) break; else {b3=a[j];
		b3=b3+a[j+1];}
		if ((b3==b1) || (b2==b3)) {x++;j++;}
	}

}
cout<<x;
return 0;
}


#define _CRT_SECURE_NO_WARNINGS
//#pragma comment(linker, "/STACK:100000000")
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <stdio.h>
#include <vector>
#include <map>

#define ll long long
#define ld long double
#define f(i, a, b) for(int i=(int)(a); i<(int)(b); ++i)
#define fn(i, b) for(int i=0; i<(int) (b); i++)
#define fz(i, a, b) for (int i=(int)(a); i>=(int)(b); --i)
#define mp make_pair
#define pii pair<ll, ll>
#define pb push_back
#define bg begin()
#define ed end()
#define pi 3.141592653589793238462643383279
ll gcd(ll a, ll b){ return (!b) ? a : gcd(b, a % b); }

ll lcm(ll a, ll b) { return (((a*b) / gcd(a, b))); }

#define MAX 1000100 //53
#define INF 10
#define y1 yy1
#define eps 0
void out(){ puts("-1"); exit(0); }

#define ls (v << 1)
#define rs (v << 1)+1
using namespace std;

string s;
int k;
char a, b;
int ans = 0;
char nums[1001];

int main(){
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

	cin >> s;
	fn(i, s.length()) nums[s[i]]++;
	cin >> k;
	getchar();
	while (k--) {
		cin >> a >> b;
		nums[a] = b;
		nums[b] = a;
	}
	ans = 0;
	for (int i=0; i<s.length(); ){
		char fa = s[i];
		char fb = nums[a];
		int counta = 0, countb = 0;
		while (s[i] == fa || s[i] == fb) {
			if (s[i] == fa) counta++; else countb++;
			i++;
		}
		ans += min(counta, countb);
	}
	cout << ans << endl;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
//#pragma comment(linker, "/STACK:100000000")
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <stdio.h>
#include <vector>
#include <map>

#define ll long long
#define ld long double
#define f(i, a, b) for(int i=(int)(a); i<(int)(b); ++i)
#define fn(i, b) for(int i=0; i<(int) (b); i++)
#define fz(i, a, b) for (int i=(int)(a); i>=(int)(b); --i)
#define mp make_pair
#define pii pair<ll, ll>
#define pb push_back
#define bg begin()
#define ed end()
#define pi 3.141592653589793238462643383279
ll gcd(ll a, ll b){ return (!b) ? a : gcd(b, a % b); }

ll lcm(ll a, ll b) { return (((a*b) / gcd(a, b))); }

#define MAX 1000100 //53
#define INF 10
#define y1 yy1
#define eps 0
void out(){ puts("-1"); exit(0); }

#define ls (v << 1)
#define rs (v << 1)+1
using namespace std;

string s;
int k;
char a, b;
int ans = 0;
char nums[1001];

int main(){
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

	cin >> s;
	fn(i, s.length()) nums[s[i]]++;
	cin >> k;
	fn(i, 700) nums[i] = (char)(700);

	getchar();
	while (k--) {
		cin >> a >> b;
		getchar();
		nums[a] = b;
		nums[b] = a;
	}
	ans = 0;
	for (int i=0; i<s.length()-1; ){
		char fa = s[i];
		char fb = nums[a];
		int counta = 0, countb = 0;
		while (s[i] == fa || s[i] == fb) {
			if (s[i] == fa) counta++; else countb++;
			i++;
		}
		ans += min(counta, countb);
	}
	cout << ans << endl;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
//#pragma comment(linker, "/STACK:100000000")
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <stdio.h>
#include <vector>
#include <map>

#define ll long long
#define ld long double
#define f(i, a, b) for(int i=(int)(a); i<(int)(b); ++i)
#define fn(i, b) for(int i=0; i<(int) (b); i++)
#define fz(i, a, b) for (int i=(int)(a); i>=(int)(b); --i)
#define mp make_pair
#define pii pair<ll, ll>
#define pb push_back
#define bg begin()
#define ed end()
#define pi 3.141592653589793238462643383279
ll gcd(ll a, ll b){ return (!b) ? a : gcd(b, a % b); }

ll lcm(ll a, ll b) { return (((a*b) / gcd(a, b))); }

#define MAX 1000100 //53
#define INF 10
#define y1 yy1
#define eps 0
void out(){ puts("-1"); exit(0); }

#define ls (v << 1)
#define rs (v << 1)+1
using namespace std;

string s;
int k;
char a, b;
int ans = 0;
char nums[1001];

int main(){
//	freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

	cin >> s;
	fn(i, s.length()) nums[s[i]]++;
	cin >> k;
	fn(i, 700) nums[i] = (char)(700);

	getchar();
	while (k--) {
		cin >> a >> b;
		nums[a] = b;
		nums[b] = a;
	}
	ans = 0;
	for (int i=0; i<s.length(); ){
		char fa = s[i];
		char fb = nums[a];
		int counta = 0, countb = 0;
		while (s[i] == fa || s[i] == fb) {
			if (s[i] == fa) counta++; else countb++;
			i++;
		}
		ans += min(counta, countb);
	}
	cout << ans << endl;

	return 0;
}