#include <bits/stdc++.h>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)
#define INF 1000000000
#define MOD 1000000009
#define PI 3.14159265359

class Mod{
public:
    long long a;
    Mod(){a = 0;}
    Mod(long long x){a = (x % MOD + MOD) % MOD;}
    const Mod operator+(const Mod& x) const{return Mod(a + x.a);}
    const Mod operator-(const Mod& x) const{return Mod(a - x.a);}
    const Mod operator*(const Mod& x) const{return Mod(a * x.a);}
    const Mod operator/(const Mod& x) const{ //�t�F���}�[�̏��藝�Aa��MOD���݂��ɑf�̎��̂�
        if(x.a == 0)return 0;
		if(a % x.a == 0)return Mod(a/x.a);
		int b = MOD - 2;long long c = x.a;long long ret = 1;
        while(b > 0){if(b & 1){ret *= c;ret %= MOD;}
            c *= c;c %= MOD;b >>= 1;
        }
        return Mod(a * ret);
    }
    long long getValue(){return a;}
};

typedef long long ll;
typedef pair<ll,ll> P;

ll po(ll num,ll sisu){
	ll ret = 1;
	ll kake = num;
	for(int i = 0;i < 60;i++){
		if(sisu & (1LL << i)){
			ret = (ret * kake) % MOD;
		}
		kake = (kake * kake) % MOD;
	}
	return ret;
}

int main()
{
	ll n,a,b,k;cin >> n >> a >> b >> k;
	string str;cin >> str;

	ll ans = 0;
	ll temp = 0;
	for(ll i = 0;i < str.size();i++){
		ll A = po(a,str.size() - i - 1);
		ll B = po(b,i);
//		cout << "!!" <<  A << ' ' << B << endl;
		if(str[i] == '+'){
			temp += (A * B) % MOD;
		}
		else{
			temp -= (A * B) % MOD;
		}
		temp %= MOD;
		while(temp < 0)temp += MOD;
	}
	if(n <= k){
		cout << temp << endl;
		return 0;
	}
	if(a == b){
		ll A = po(a,k);
		ll AA = po(A,((n+1)/k)-1);
		//cout << temp << endl;
		//cout << AA << endl;
		AA = (AA * ((n+1)/k)) % MOD;
		//cout << AA << endl;
		AA = (AA * temp) % MOD;
		cout << AA << endl;
		return 0;
	}

	ll A = po(a,k);
	ll B = po(b,k);
	ll AA = po(A,((n+1)/k));
	ll BB = po(B,((n+1)/k));
	ll hoge = (AA - BB + MOD) % MOD;
	Mod ue(hoge);
	hoge = (a - b + MOD) % MOD;
	ue = ue / hoge;
	//cout << ue.a << endl;
	//cout << temp << endl;
	ue = ue * temp;
	cout << ue.getValue() << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)
#define INF 1000000000
#define MOD 1000000009
#define PI 3.14159265359

typedef long long ll;
typedef pair<ll,ll> P;

ll po(ll num,ll sisu){
	ll ret = 1;
	ll kake = num;
	for(ll i = 0;i < 60;i++){
		if(sisu & (1LL << i)){
			ret = (ret * kake) % MOD;
		}
		kake = (kake * kake) % MOD;
	}
	return ret % MOD;
}

long long divMod(long long a,long long b){
	b = po(b,MOD-2);
	return (a * b) % MOD;
}

int main()
{
	ll n,a,b,k;cin >> n >> a >> b >> k;
	string str;cin >> str;
	n++;
	ll temp = 0;
	for(ll i = 0;i < str.size();i++){
		ll A = po(a,n - i - 1);
		ll B = po(b,i);
	//	cout << "!!" <<  A << ' ' << B << endl;
	//	cout << (A * B)%MOD << endl;
		if(str[i] == '+'){
			temp += (A * B) % MOD;
		}
		else{
			temp -= (A * B) % MOD;
		}
		temp %= MOD;
		while(temp < 0)temp += MOD;
	//	cout << temp << endl;
	}
	//cout << temp << endl;
	ll di = po(divMod(b,a),k);
	if(di == 1){
		cout << (temp * ((n) / k)) % MOD << endl;
		return 0;
	}
	ll ans = po(di,n/k) + MOD - 1;
	ans = divMod(ans,di+MOD-1);

	cout << (ans*temp)%MOD << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)
#define INF 1000000000
#define MOD 1000000009
#define PI 3.14159265359

typedef long long ll;
typedef pair<ll,ll> P;

long long divMod(long long a,long long b){
	if(b == 0)return -1;
	if(a % b == 0)return a / b;
	long long tmp = MOD - 2;long long c = b;long long ret = 1;
	while(tmp > 0){
		if(tmp & 1){ret *= c;ret %= MOD;}
		c *= c;c %= MOD;tmp >>= 1;
	}
	return (a * ret) % MOD;
}

ll po(ll num,ll sisu){
	ll ret = 1;
	ll kake = num;
	for(ll i = 0;i < 60;i++){
		if(sisu & (1LL << i)){
			ret = (ret * kake) % MOD;
		}
		kake = (kake * kake) % MOD;
	}
	return ret % MOD;
}

int main()
{
	ll n,a,b,k;cin >> n >> a >> b >> k;
	string str;cin >> str;
	n++;
	ll temp = 0;
	for(ll i = 0;i < str.size();i++){
		ll A = po(a,str.size() - i - 1);
		ll B = po(b,i);
//		cout << "!!" <<  A << ' ' << B << endl;
		if(str[i] == '+'){
			temp += (A * B) % MOD;
		}
		else{
			temp -= (A * B) % MOD;
		}
		temp %= MOD;
		while(temp < 0)temp += MOD;
	}

	ll di = po(divMod(b,a),k);
	if(di == 1){
		cout << (temp * ((n) / k)) % MOD << endl;
		return 0;
	}
	ll ans = po(di,n/k) - 1;
	if(ans < 0)ans += MOD;
	ans = divMod(ans,di-1);

	cout << (ans*temp)%MOD << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)
#define INF 1000000000
#define MOD 1000000009
#define PI 3.14159265359

class Mod{
public:
    long long a;
    Mod(){a = 0;}
    Mod(long long x){a = (x % MOD + MOD) % MOD;}
    const Mod operator+(const Mod& x) const{return Mod(a + x.a);}
    const Mod operator-(const Mod& x) const{return Mod(a - x.a);}
    const Mod operator*(const Mod& x) const{return Mod(a * x.a);}
    const Mod operator/(const Mod& x) const{ //�t�F���}�[�̏��藝�Aa��MOD���݂��ɑf�̎��̂�
        if(x.a == 0)return 0;
		if(a % x.a == 0)return Mod(a/x.a);
		int b = MOD - 2;long long c = x.a;long long ret = 1;
        while(b > 0){if(b & 1){ret *= c;ret %= MOD;}
            c *= c;c %= MOD;b >>= 1;
        }
        return Mod(a * ret);
    }
    long long getValue(){return a;}
};

typedef long long ll;
typedef pair<ll,ll> P;

ll po(ll num,ll sisu){
	ll ret = 1;
	ll kake = num;
	for(int i = 0;i < 60;i++){
		if(sisu & (1LL << i)){
			ret = (ret * kake) % MOD;
		}
		kake = (kake * kake) % MOD;
	}
	return ret;
}

int main()
{
	ll n,a,b,k;cin >> n >> a >> b >> k;
	string str;cin >> str;

	ll ans = 0;
	ll temp = 0;
	for(ll i = 0;i < str.size();i++){
		ll A = po(a,str.size() - i - 1);
		ll B = po(b,i);
	//	cout << A << ' ' << B << endl;
		if(str[i] == '+'){
			temp += (A * B) % MOD;
		}
		else{
			temp -= (A * B) % MOD;
		}
		temp %= MOD;
		while(temp < 0)temp += MOD;
	}
	if(n <= k){
		cout << temp << endl;
		return 0;
	}

	ll A = po(a,k);
	ll B = po(b,k);
	ll AA = po(A,((n+1)/k));
	ll BB = po(B,((n+1)/k));
	ll hoge = (AA - BB + MOD) % MOD;
	Mod ue(hoge);
	hoge = (a - b + MOD) % MOD;
	ue = ue / hoge;
	//cout << ue.a << endl;
	//cout << temp << endl;
	ue = ue * temp;
	cout << ue.getValue() << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)
#define INF 1000000000
#define MOD 1000000009
#define PI 3.14159265359

typedef long long ll;
typedef pair<ll,ll> P;

long long divMod(long long a,long long b){
	if(b == 0)return -1;
	if(a % b == 0)return a / b;
	long long tmp = MOD - 2;long long c = b;long long ret = 1;
	while(tmp > 0){
		if(tmp & 1){ret *= c;ret %= MOD;}
		c *= c;c %= MOD;tmp >>= 1;
	}
	return (a * ret) % MOD;
}

ll po(ll num,ll sisu){
	ll ret = 1;
	ll kake = num;
	for(int i = 0;i < 60;i++){
		if(sisu & (1LL << i)){
			ret = (ret * kake) % MOD;
		}
		kake = (kake * kake) % MOD;
	}
	return ret % MOD;
}

int main()
{
	ll n,a,b,k;cin >> n >> a >> b >> k;
	string str;cin >> str;

	ll temp = 0;
	for(ll i = 0;i < str.size();i++){
		ll A = po(a,str.size() - i - 1);
		ll B = po(b,i);
//		cout << "!!" <<  A << ' ' << B << endl;
		if(str[i] == '+'){
			temp += (A * B) % MOD;
		}
		else{
			temp -= (A * B) % MOD;
		}
		temp %= MOD;
		while(temp < 0)temp += MOD;
	}

	ll A = po(a,k);
	ll B = po(b,k);
	ll di = divMod(b,a);
	if(di == 1){
		cout << (temp * ((n+1) / k)) % MOD << endl;
		return 0;
	}
	//cout << temp << endl;
	//cout << A << ' ' << B << ' ' << di << endl;
	ll ans = divMod((temp * (po(di,(n+1)/k) - 1)) % MOD,di - 1);
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)
#define INF 1000000000
#define MOD 1000000009
#define PI 3.14159265359

class Mod{
public:
    long long a;
    Mod(){a = 0;}
    Mod(long long x){a = (x % MOD + MOD) % MOD;}
    const Mod operator+(const Mod& x) const{return Mod(a + x.a);}
    const Mod operator-(const Mod& x) const{return Mod(a - x.a);}
    const Mod operator*(const Mod& x) const{return Mod(a * x.a);}
    const Mod operator/(const Mod& x) const{ //�t�F���}�[�̏��藝�Aa��MOD���݂��ɑf�̎��̂�
        if(x.a == 0)return 0;
		if(a % x.a == 0)return Mod(a/x.a);
		int b = MOD - 2;long long c = x.a;long long ret = 1;
        while(b > 0){if(b & 1){ret *= c;ret %= MOD;}
            c *= c;c %= MOD;b >>= 1;
        }
        return Mod(a * ret);
    }
    long long getValue(){return a;}
};

typedef long long ll;
typedef pair<ll,ll> P;

ll po(ll num,ll sisu){
	ll ret = 1;
	ll kake = num;
	for(int i = 0;i < 60;i++){
		if(sisu & (1LL << i)){
			ret = (ret * kake) % MOD;
		}
		kake = (kake * kake) % MOD;
	}
	return ret;
}

int main()
{
	ll n,a,b,k;cin >> n >> a >> b >> k;
	string str;cin >> str;

	ll ans = 0;
	ll temp = 0;
	for(ll i = 0;i < str.size();i++){
		ll A = po(a,str.size() - i - 1);
		ll B = po(b,i);
//		cout << "!!" <<  A << ' ' << B << endl;
		if(str[i] == '+'){
			temp += (A * B) % MOD;
		}
		else{
			temp -= (A * B) % MOD;
		}
		temp %= MOD;
		while(temp < 0)temp += MOD;
	}
	if((n+1) == k){
		cout << temp << endl;
		return 0;
	}
	if(a == b){
		ll A = po(a,k);
		ll AA = po(A,((n+1)/k)-1);
		//cout << temp << endl;
		//cout << AA << endl;
		AA = (AA * ((n+1)/k)) % MOD;
		//cout << AA << endl;
		AA = (AA * temp) % MOD;
		cout << AA << endl;
		return 0;
	}

	ll A = po(a,k);
	ll B = po(b,k);
	ll AA = po(A,((n+1)/k));
	ll BB = po(B,((n+1)/k));
	ll hoge = (AA - BB + MOD) % MOD;
	Mod ue(hoge);
	hoge = (a - b + MOD) % MOD;
	ue = ue / hoge;
	//cout << ue.a << endl;
	//cout << temp << endl;
	ue = ue * temp;
	cout << ((ue.getValue()+MOD)%MOD) << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)
#define INF 1000000000
#define MOD 1000000009
#define PI 3.14159265359

typedef long long ll;
typedef pair<ll,ll> P;

long long divMod(long long a,long long b){
	if(b == 0)return -1;
	if(a % b == 0)return a / b;
	long long tmp = MOD - 2;long long c = b;long long ret = 1;
	while(tmp > 0){
		if(tmp & 1){ret *= c;ret %= MOD;}
		c *= c;c %= MOD;tmp >>= 1;
	}
	return (a * ret) % MOD;
}

ll po(ll num,ll sisu){
	ll ret = 1;
	ll kake = num;
	for(int i = 0;i < 60;i++){
		if(sisu & (1LL << i)){
			ret = (ret * kake) % MOD;
		}
		kake = (kake * kake) % MOD;
	}
	return ret;
}

int main()
{
	ll n,a,b,k;cin >> n >> a >> b >> k;
	string str;cin >> str;

	ll temp = 0;
	for(ll i = 0;i < str.size();i++){
		ll A = po(a,str.size() - i - 1);
		ll B = po(b,i);
//		cout << "!!" <<  A << ' ' << B << endl;
		if(str[i] == '+'){
			temp += (A * B) % MOD;
		}
		else{
			temp -= (A * B) % MOD;
		}
		temp %= MOD;
		while(temp < 0)temp += MOD;
	}

	ll A = po(a,k);
	ll B = po(b,k);
	ll di = divMod(b,a);
	if(di == 1){
		cout << (temp * ((n+1) / k)) % MOD << endl;
		return 0;
	}
	//cout << temp << endl;
	//cout << A << ' ' << B << ' ' << di << endl;
	ll ans = divMod((temp * (po(di,(n+1)/k) - 1)) % MOD,di - 1);
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)
#define INF 1000000000
#define MOD 1000000009
#define PI 3.14159265359

typedef long long ll;
typedef pair<ll,ll> P;

long long divMod(long long a,long long b){
	if(b == 0)return -1;
	if(a % b == 0)return a / b;
	long long tmp = MOD - 2;long long c = b;long long ret = 1;
	while(tmp > 0){
		if(tmp & 1){ret *= c;ret %= MOD;}
		c *= c;c %= MOD;tmp >>= 1;
	}
	return (a * ret) % MOD;
}

ll po(ll num,ll sisu){
	ll ret = 1;
	ll kake = num;
	for(int i = 0;i < 60;i++){
		if(sisu & (1LL << i)){
			ret = (ret * kake) % MOD;
		}
		kake = (kake * kake) % MOD;
	}
	return ret % MOD;
}

int main()
{
	ll n,a,b,k;cin >> n >> a >> b >> k;
	string str;cin >> str;

	ll temp = 0;
	for(ll i = 0;i < str.size();i++){
		ll A = po(a,str.size() - i - 1);
		ll B = po(b,i);
//		cout << "!!" <<  A << ' ' << B << endl;
		if(str[i] == '+'){
			temp += (A * B) % MOD;
		}
		else{
			temp -= (A * B) % MOD;
		}
		temp %= MOD;
		while(temp < 0)temp += MOD;
	}

	ll A = po(a,k);
	ll B = po(b,k);
	ll di = divMod(B,A);
	if(di == 1){
		cout << (temp * ((n+1) / k)) % MOD << endl;
		return 0;
	}
	//cout << temp << endl;
	//cout << A << ' ' << B << ' ' << di << endl;
	ll ans = divMod((temp * (po(di,(n+1)/k) - 1)) % MOD,di - 1);
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)
#define INF 1000000000
#define MOD 1000000009
#define PI 3.14159265359

class Mod{
public:
    long long a;
    Mod(){a = 0;}
    Mod(long long x){a = (x % MOD + MOD) % MOD;}
    const Mod operator+(const Mod& x) const{return Mod(a + x.a);}
    const Mod operator-(const Mod& x) const{return Mod(a - x.a);}
    const Mod operator*(const Mod& x) const{return Mod(a * x.a);}
    const Mod operator/(const Mod& x) const{ //�t�F���}�[�̏��藝�Aa��MOD���݂��ɑf�̎��̂�
        if(x.a == 0)return 0;
		if(a % x.a == 0)return Mod(a/x.a);
		int b = MOD - 2;long long c = x.a;long long ret = 1;
        while(b > 0){if(b & 1){ret *= c;ret %= MOD;}
            c *= c;c %= MOD;b >>= 1;
        }
        return Mod(a * ret);
    }
    long long getValue(){return a;}
};

typedef long long ll;
typedef pair<ll,ll> P;

ll po(ll num,ll sisu){
	ll ret = 1;
	ll kake = num;
	for(int i = 0;i < 60;i++){
		if(sisu & (1LL << i)){
			ret = (ret * kake) % MOD;
		}
		kake = (kake * kake) % MOD;
	}
	return ret;
}

int main()
{
	ll n,a,b,k;cin >> n >> a >> b >> k;
	string str;cin >> str;

	ll ans = 0;
	ll temp = 0;
	for(ll i = 0;i < str.size();i++){
		ll A = po(a,str.size() - i - 1);
		ll B = po(b,i);
	//	cout << A << ' ' << B << endl;
		if(str[i] == '+'){
			temp += (A * B) % MOD;
		}
		else{
			temp -= (A * B) % MOD;
		}
		temp %= MOD;
		while(temp < 0)temp += MOD;
	}
	if(n <= k){
		cout << temp << endl;
		return 0;
	}
	if(a == b){
		ll A = po(a,k);
		ll AA = po(A,((n+1)/k)-1);
		AA = (AA * ((n+1)/k)) % MOD;
		AA = (AA * temp) & MOD;
		cout << AA << endl;
		return 0;
	}

	ll A = po(a,k);
	ll B = po(b,k);
	ll AA = po(A,((n+1)/k));
	ll BB = po(B,((n+1)/k));
	ll hoge = (AA - BB + MOD) % MOD;
	Mod ue(hoge);
	hoge = (a - b + MOD) % MOD;
	ue = ue / hoge;
	//cout << ue.a << endl;
	//cout << temp << endl;
	ue = ue * temp;
	cout << ue.getValue() << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)
#define INF 1000000000
#define MOD 1000000009
#define PI 3.14159265359

typedef long long ll;
typedef pair<ll,ll> P;

ll po(ll num,ll sisu){
	ll ret = 1;
	ll kake = num;
	for(int i = 0;i < 60;i++){
		if(sisu & (1LL << i)){
			ret = (ret * kake) % MOD;
		}
		kake = (kake * kake) % MOD;
	}
	return ret;
}

int main()
{
	ll n,a,b,k;cin >> n >> a >> b >> k;
	string str;cin >> str;

	ll ans = 0;
	ll temp = 0;
	for(int i = 0;i < str.size();i++){
		ll A = po(a,str.size() - i - 1);
		ll B = po(b,i);
	//	cout << A << ' ' << B << endl;
		if(str[i] == '+'){
			temp += (A * B) % MOD;
		}
		else{
			temp -= (A * B) % MOD;
		}
		temp %= MOD;
		while(temp < 0)temp += MOD;
	}
	//cout << temp << endl;

	for(int i = 0;i < n+1;i+=k){
		ll A = po(a,n - i - (str.size() - 1));
		ll B = po(b,i);
		ans = (ans + (((A * B) % MOD) * temp) % MOD) % MOD;
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)
#define INF 1000000000
#define MOD 1000000009
#define PI 3.14159265359

class Mod{
    long long a;
public:
    Mod(){a = 0;}
    Mod(long long x){a = (x % MOD + MOD) % MOD;}
    const Mod operator+(const Mod& x) const{return Mod(a + x.a);}
    const Mod operator-(const Mod& x) const{return Mod(a - x.a);}
    const Mod operator*(const Mod& x) const{return Mod(a * x.a);}
    const Mod operator/(const Mod& x) const{ //�t�F���}�[�̏��藝�Aa��MOD���݂��ɑf�̎��̂�
        if(x.a == 0)return 0;
		if(a % x.a == 0)return Mod(a/x.a);
		int b = MOD - 2;long long c = x.a;long long ret = 1;
        while(b > 0){if(b & 1){ret *= c;ret %= MOD;}
            c *= c;c %= MOD;b >>= 1;
        }
        return Mod(a * ret);
    }
    long long getValue(){return a;}
};

typedef long long ll;
typedef pair<ll,ll> P;

ll po(ll num,ll sisu){
	ll ret = 1;
	ll kake = num;
	for(int i = 0;i < 40;i++){
		if(sisu & (1LL << i)){
			ret = (ret * kake) % MOD;
		}
		kake = (kake * kake) % MOD;
	}
	return ret;
}

int main()
{
	ll n,a,b,k;cin >> n >> a >> b >> k;
	string str;cin >> str;

	ll ans = 0;
	ll temp = 0;
	for(ll i = 0;i < str.size();i++){
		ll A = po(a,str.size() - i - 1);
		ll B = po(b,i);
	//	cout << A << ' ' << B << endl;
		if(str[i] == '+'){
			temp += (A * B) % MOD;
		}
		else{
			temp -= (A * B) % MOD;
		}
		temp %= MOD;
		while(temp < 0)temp += MOD;
	}
	if((n+1) == k){
		cout << temp << endl;
		return 0;
	}

	ll A = po(a,k);
	ll B = po(b,k);
	ll AA = po(A,((n+1)/k));
	ll BB = po(B,((n+1)/k));
	ll hoge = (AA - BB) % MOD;
	while(hoge < 0)hoge += MOD;
	Mod ue(hoge);
	hoge = (a - b) % MOD;
	while(hoge < 0)hoge += MOD;
	ue = ue / hoge;
	ue = ue * temp;
	cout << ue.getValue() << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)
#define INF 1000000000
#define MOD 1000000009
#define PI 3.14159265359

typedef long long ll;
typedef pair<ll,ll> P;

ll po(ll num,ll sisu){
	ll ret = 1;
	ll kake = num;
	for(int i = 0;i < 40;i++){
		if(sisu & (1LL << i)){
			ret = (ret * kake) % MOD;
		}
		kake = (kake * kake) % MOD;
	}
	return ret;
}

int main()
{
	ll n,a,b,k;cin >> n >> a >> b >> k;
	string str;cin >> str;

	ll ans = 0;
	ll temp = 0;
	for(ll i = 0;i < str.size();i++){
		ll A = po(a,str.size() - i - 1);
		ll B = po(b,i);
	//	cout << A << ' ' << B << endl;
		if(str[i] == '+'){
			temp += (A * B) % MOD;
		}
		else{
			temp -= (A * B) % MOD;
		}
		temp %= MOD;
		while(temp < 0)temp += MOD;
	}
	//cout << temp << endl;

	for(ll i = 0;i < n+1;i+=k){
		ll A = po(a,n - i - (str.size() - 1));
		ll B = po(b,i);
		ans = (ans + ((A * B) % MOD))% MOD;
	}
	ans = (ans * temp) % MOD;

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)
#define INF 1000000000
#define MOD 1000000009
#define PI 3.14159265359

class Mod{
public:
    long long a;
    Mod(){a = 0;}
    Mod(long long x){a = (x % MOD + MOD) % MOD;}
    const Mod operator+(const Mod& x) const{return Mod(a + x.a);}
    const Mod operator-(const Mod& x) const{return Mod(a - x.a);}
    const Mod operator*(const Mod& x) const{return Mod(a * x.a);}
    const Mod operator/(const Mod& x) const{ //�t�F���}�[�̏��藝�Aa��MOD���݂��ɑf�̎��̂�
        if(x.a == 0)return 0;
		if(a % x.a == 0)return Mod(a/x.a);
		int b = MOD - 2;long long c = x.a;long long ret = 1;
        while(b > 0){if(b & 1){ret *= c;ret %= MOD;}
            c *= c;c %= MOD;b >>= 1;
        }
        return Mod(a * ret);
    }
    long long getValue(){return a;}
};

typedef long long ll;
typedef pair<ll,ll> P;

ll po(ll num,ll sisu){
	ll ret = 1;
	ll kake = num;
	for(int i = 0;i < 60;i++){
		if(sisu & (1LL << i)){
			ret = (ret * kake) % MOD;
		}
		kake = (kake * kake) % MOD;
	}
	return ret;
}

int main()
{
	ll n,a,b,k;cin >> n >> a >> b >> k;
	string str;cin >> str;

	ll ans = 0;
	ll temp = 0;
	for(ll i = 0;i < str.size();i++){
		ll A = po(a,str.size() - i - 1);
		ll B = po(b,i);
//		cout << "!!" <<  A << ' ' << B << endl;
		if(str[i] == '+'){
			temp += (A * B) % MOD;
		}
		else{
			temp -= (A * B) % MOD;
		}
		temp %= MOD;
		while(temp < 0)temp += MOD;
	}
	if(n <= k){
		cout << temp << endl;
		return 0;
	}
	if(a == b){
		ll A = po(a,k);
		ll AA = po(A,((n+1)/k)-1);
		//cout << temp << endl;
		//cout << AA << endl;
		AA = (AA * ((n+1)/k)) % MOD;
		//cout << AA << endl;
		AA = (AA * temp) % MOD;
		cout << AA << endl;
		return 0;
	}

	ll A = po(a,k);
	ll B = po(b,k);
	ll AA = po(A,((n+1)/k));
	ll BB = po(B,((n+1)/k));
	ll hoge = (AA - BB + MOD) % MOD;
	Mod ue(hoge);
	hoge = (a - b + MOD) % MOD;
	ue = ue / hoge;
	//cout << ue.a << endl;
	//cout << temp << endl;
	ue = ue * temp;
	cout << ((ue.getValue()+MOD)%MOD) << endl;

	return 0;
}


#include <iostream>
#define MAXN 100010
#define P 1000000009
using namespace std;
long long a,b,n,k;
long long powerMod(long long i,long long j){
    long long res = 1;
    while(j){
        if(j&1){
           res*=i;
           res%=P;
        }
        i*=i;
        i%=P;
        j>>=1;
    }
    return res;
}

int main(){
    cin>>n>>a>>b>>k;
   // cout<<powerMod(a,b);
    long long sum=0;
    for(int i = 0; i<k; i++){
        char x;
        cin>>x;
        if(x=='+') sum+=powerMod(a,n-i)*powerMod(b,i);
        else sum-=powerMod(a,n-i)*powerMod(b,i);
        sum%=P;
    }
    n = (n+1)/k;
    int q = (powerMod(b,k)*powerMod(powerMod(a,k),P-2))%P;
    //cout<<n<<endl;
    //cout<<(1-powerMod(q,n))*powerMod(1-q,P-2)<<endl;
    int r = ((1-powerMod(q,n))*powerMod(1-q,P-2))%P;
    //cout<<q<<endl<<r<<endl;
    int  ans = sum*r%P;
    if(ans<0){
        ans+=P;
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>

#define E 1000000009

typedef long long ll;

long long  powMod(long long a,long long i){
    if(i==0) return 1%E;
    if(i&1){
        return (powMod(a,i-1)%E*a)%E;
    }
    else{
        long long t = powMod(a,i/2)%E;
        return (t*t)%E;
    }
}

using namespace std;
ll sum;
int main() {
    ll n, a, b, k;
    cin >> n >> a >> b >> k;
    vector<int> sign;
    int tmp; getchar();
    for (int i = 0; i < k; i++) {
        tmp = getchar();
        tmp == '+' ? sign.push_back(1) : sign.push_back(-1);
    }
    for (int i = 0, j = 0; i <= n; i++, j++) {
        if(j == k) j = 0;
        sum += ll(sign[j] * powMod(a, n-i) * powMod(b, i)) % E;
    }
    cout << (sum + E) % E;
    return 0;
}


#include <iostream>
#define MAXN 100010
#define P 1000000009
using namespace std;
long long a,b,n,k;
long long powerMod(long long i,long long j){
    long long res = 1;
    while(j){
        if(j&1){
           res*=i;
           res%=P;
        }
        i*=i;
        i%=P;
        j>>=1;
    }
    return res;
}

int main(){
    cin>>n>>a>>b>>k;
    long long sum=0;
    for(int i = 0; i<k; i++){
        char x;
        cin>>x;
        if(x=='+') sum+=powerMod(a,n-i)*powerMod(b,i)%P;
        else sum-=powerMod(a,n-i)*powerMod(b,i)%P;
        sum%=P;
    }
    n = (n+1)/k;
    long long  q = ((powerMod(b,k)*powerMod(powerMod(a,k),P-2)))%P;
    long long  r = (((1-powerMod(q,n))*powerMod(1-q,P-2)))%P;
    int  ans = (sum*r)%P;
    if(ans<0){
        ans+=P;
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#define MAXN 100010
#define P 1000000009
using namespace std;
long long a,b,n,k;
long long powerMod(long long i,long long j){
    long long res = 1;
    while(j){
        if(j&1){
           res*=i;
           res%=P;
        }
        i*=i;
        i%=P;
        j>>=1;
    }
    return res;
}

int main(){
    cin>>n>>a>>b>>k;
   // cout<<powerMod(a,b);
    long long sum=0;
    for(int i = 0; i<k; i++){
        char x;
        cin>>x;
        if(x=='+') sum+=powerMod(a,n-i)*powerMod(b,i);
        else sum-=powerMod(a,n-i)*powerMod(b,i);
        sum%=P;
    }
    n = (n+1)/k;
    int q = (powerMod(b,k)*powerMod(powerMod(a,k),P-2))%P;
    cout<<n<<endl;
    cout<<(1-powerMod(q,n))*powerMod(1-q,P-2)<<endl;
    int r = ((1-powerMod(q,n))*powerMod(1-q,P-2))%P;
    cout<<q<<endl<<r<<endl;
    int  ans = sum*r%P;
    if(ans<0){
        ans+=P;
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>

#define E 1000000009

typedef long long ll;

using namespace std;
ll sum;
int main() {
    ll n, a, b, k;
    cin >> n >> a >> b >> k;
    vector<int> sign;
    int tmp; getchar();
    for (int i = 0; i < k; i++) {
        tmp = getchar();
        tmp == '+' ? sign.push_back(1) : sign.push_back(-1);
    }
    for (int i = 0, j = 0; i <= n; i++, j++) {
        if(j == k) j = 0;
        sum += ll(sign[j] * pow(a, n-i) * pow(b, i)) % E;
    }
    cout << (sum + E) % E;
    return 0;
}


#include <iostream>
#define MAXN 100010
#define P 1000000009
using namespace std;
long long a,b,n,k;
long long powerMod(long long i,long long j){
    long long res = 1;
    while(j){
        if(j&1){
           res*=i;
           res%=P;
        }
        i*=i;
        i%=P;
        j>>=1;
    }
    return res;
}

int main(){
    cin>>n>>a>>b>>k;
    long long sum=0;
    for(int i = 0; i<k; i++){
        char x;
        cin>>x;
        if(x=='+') sum+=powerMod(a,n-i)*powerMod(b,i)%P;
        else sum-=powerMod(a,n-i)*powerMod(b,i)%P;
        sum%=P;
    }
    n = (n+1)/k;
    long long  q = ((powerMod(b,k)*powerMod(powerMod(a,k),P-2)))%P;
    long long  r = (((1-powerMod(q,n))*powerMod(1-q,P-2)))%P;
    int  ans = (sum*r)%P;
    if(q==1){
        ans = sum;
    }
    if(ans<0){
        ans+=P;
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>

#define E 1000000009

typedef long long ll;

int powMod(long long a,long long i){
    if(i==0) return 1%E;
    if(i&1){
        return (powMod(a,i-1)%E*a)%E;
    }
    else{
        long long t = powMod(a,i/2)%E;
        return (t*t)%E;
    }
}

using namespace std;
ll sum;
int main() {
    ll n, a, b, k;
    cin >> n >> a >> b >> k;
    vector<int> sign;
    int tmp; getchar();
    for (int i = 0; i < k; i++) {
        tmp = getchar();
        tmp == '+' ? sign.push_back(1) : sign.push_back(-1);
    }
    for (int i = 0, j = 0; i <= n; i++, j++) {
        if(j == k) j = 0;
        sum += ll(sign[j] * powMod(a, n-i) * powMod(b, i)) % E;
    }
    cout << (sum + E) % E;
    return 0;
}


#include <iostream>
#define MAXN 100010
#define P 1000000009
using namespace std;
long long a,b,n,k;
long long powerMod(long long i,long long j){
    long long res = 1;
    while(j){
        if(j&1){
           res*=i;
           res%=P;
        }
        i*=i;
        i%=P;
        j>>=1;
    }
    return res;
}

int main(){
    cin>>n>>a>>b>>k;
    long long sum=0;
    for(int i = 0; i<k; i++){
        char x;
        cin>>x;
        if(x=='+') sum+=powerMod(a,n-i)*powerMod(b,i)%P;
        else sum-=powerMod(a,n-i)*powerMod(b,i)%P;
        sum%=P;
    }
    n = (n+1)/k;
    long long  q = ((powerMod(b,k)*powerMod(powerMod(a,k),P-2)))%P;
    long long  r = (((1-powerMod(q,n))*powerMod(1-q,P-2)))%P;
    int  ans = (sum*r)%P;
    if(q==1){
        ans = sum*n%P;
    }
    if(ans<0){
        ans+=P;
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#define MAXN 100010
#define P 1000000009
using namespace std;
long long a,b,n,k;
long long powerMod(long long i,long long j){
    long long res = 1;
    while(j){
        if(j&1){
           res*=i;
           res%=P;
        }
        i*=i;
        i%=P;
        j>>=1;
    }
    return res;
}

int main(){
    cin>>n>>a>>b>>k;
    long long sum=0;
    for(int i = 0; i<k; i++){
        char x;
        cin>>x;
        if(x=='+') sum+=powerMod(a,n-i)*powerMod(b,i)%P;
        else sum-=powerMod(a,n-i)*powerMod(b,i)%P;
        sum%=P;
    }
    n = (n+1)/k;
    long long  q = ((powerMod(b,k)*powerMod(powerMod(a,k),P-2)))%P;
    long long  r = (((1-powerMod(q,n))*powerMod(1-q,P-2)))%P;
    int  ans = (sum*r)%P;
    if(q==1){
        ans = sum*n;
    }
    if(ans<0){
        ans+=P;
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>

#define E 1000000009

typedef long long ll;

long long  powMod(long long a,long long i){
    if(i==0) return 1%E;
    if(i&1){
        return (powMod(a,i-1)%E*a)%E;
    }
    else{
        long long t = powMod(a,i/2)%E;
        return (t*t)%E;
    }
}

using namespace std;
ll sum;
int main() {
    ll n, a, b, k;
    cin >> n >> a >> b >> k;
    vector<int> sign;
    int tmp; getchar();
    for (int i = 0; i < k; i++) {
        tmp = getchar();
        tmp == '+' ? sign.push_back(1) : sign.push_back(-1);
    }
    for (long long  i = 0, j = 0; i <= n; i++, j++) {
        if(j == k) j = 0;
        sum += ll(sign[j] * powMod(a, n-i) * powMod(b, i)) % E;
    }
    cout << (sum + E) % E;
    return 0;
}


#include <iostream>
#define MAXN 100010
#define P 1000000009
using namespace std;
long long a,b,n,k;
long long powerMod(long long i,long long j){
    long long res = 1;
    while(j){
        if(j&1){
           res*=i;
           res%=P;
        }
        i*=i;
        i%=P;
        j>>=1;
    }
    return res;
}

int main(){
    cin>>n>>a>>b>>k;
   // cout<<powerMod(a,b);
    long long sum=0;
    for(int i = 0; i<k; i++){
        char x;
        cin>>x;
        if(x=='+') sum+=powerMod(a,n-i)*powerMod(b,i)%P;
        else sum-=powerMod(a,n-i)*powerMod(b,i)%P;
        sum%=P;
    }
    n = (n+1)/k;
    long long  q = (powerMod(b,k)*powerMod(powerMod(a,k),P-2))%P;
    //cout<<n<<endl;
    //cout<<(1-powerMod(q,n))*powerMod(1-q,P-2)<<endl;
    long long  r = ((1-powerMod(q,n))*powerMod(1-q,P-2))%P;
    //cout<<q<<endl<<r<<endl;
    int  ans = (sum*r)%P;
    if(ans<0){
        ans+=P;
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#define MAXN 100010
#define P 1000000009
using namespace std;
long long a,b,n,k;
long long powerMod(long long i,long long j){
    long long res = 1;
    while(j){
        if(j&1){
           res*=i;
           res%=P;
        }
        i*=i;
        i%=P;
        j>>=1;
    }
    return res;
}

int main(){
    cin>>n>>a>>b>>k;
    long long sum=0;
    for(int i = 0; i<k; i++){
        char x;
        cin>>x;
        if(x=='+') sum+=powerMod(a,n-i)*powerMod(b,i)%P;
        else sum-=powerMod(a,n-i)*powerMod(b,i)%P;
        sum%=P;
    }
    n = (n+1)/k;
    long long  q = ((powerMod(b,k)*powerMod(a,k*P-2*k)))%P;
    long long  r = (((1-powerMod(q,n))*powerMod(1-q,P-2)))%P;
    int  ans = (sum*r)%P;
    if(q==1){
        ans = sum*n%P;
    }
    if(ans<0){
        ans+=P;
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>

#define E 1000000009

typedef long long ll;

long long  powMod(long long a,long long i){
    if(i==0) return 1%E;
    if(i&1){
        return (powMod(a,i-1)%E*a)%E;
    }
    else{
        long long t = powMod(a,i/2)%E;
        return (t*t)%E;
    }
}

using namespace std;
ll sum;
int main() {
    ll n, a, b, k;
    cin >> n >> a >> b >> k;
    vector<int> sign;
    int tmp; getchar();
    for (int i = 0; i < k; i++) {
        tmp = getchar();
        tmp == '+' ? sign.push_back(1) : sign.push_back(-1);
    }
    for (long long  i = 0, j = 0; i <= n; i++, j++) {
        if(j == k) j = 0;
        sum += ll(sign[j] * powMod(a, n-i) * powMod(b, i)) % E;
        sum%=E;
    }
    cout << (sum + E) % E;
    return 0;
}


#include <iostream>
#define MAXN 100010
#define P 1000000009
using namespace std;
long long a,b,n,k;
long long powerMod(long long i,long long j){
    long long res = 1;
    while(j){
        if(j&1){
           res*=i;
           res%=P;
        }
        i*=i;
        i%=P;
        j>>=1;
    }
    return res;
}

int main(){
    cin>>n>>a>>b>>k;
   // cout<<powerMod(a,b);
    long long sum=0;
    for(int i = 0; i<k; i++){
        char x;
        cin>>x;
        if(x=='+') sum+=powerMod(a,n-i)*powerMod(b,i)%P;
        else sum-=powerMod(a,n-i)*powerMod(b,i)%P;
        sum%=P;
    }
    n = (n+1)/k;
    long long  q = (powerMod(b,k)*powerMod(powerMod(a,k),P-2))%P;
    //cout<<n<<endl;
    //cout<<(1-powerMod(q,n))*powerMod(1-q,P-2)<<endl;
    long long  r = ((1-powerMod(q,n))*powerMod(1-q,P-2))%P;
    //cout<<q<<endl<<r<<endl;
    int  ans = sum*r%P;
    if(ans<0){
        ans+=P;
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#define MAXN 100010
#define P 1000000009
using namespace std;
long long a,b,n,k;
long long powerMod(long long i,long long j){
    long long res = 1;
    while(j){
        if(j&1){
           res*=i;
           res%=P;
        }
        i*=i;
        i%=P;
        j>>=1;
    }
    return res;
}

int main(){
    cin>>n>>a>>b>>k;
   // cout<<powerMod(a,b);
    long long sum=0;
    for(int i = 0; i<k; i++){
        char x;
        cin>>x;
        if(x=='+') sum+=powerMod(a,n-i)*powerMod(b,i)%P;
        else sum-=powerMod(a,n-i)*powerMod(b,i)%P;
        sum%=P;
    }
    n = (n+1)/k;
    int q = (powerMod(b,k)*powerMod(powerMod(a,k),P-2))%P;
    //cout<<n<<endl;
    //cout<<(1-powerMod(q,n))*powerMod(1-q,P-2)<<endl;
    int r = ((1-powerMod(q,n))*powerMod(1-q,P-2))%P;
    //cout<<q<<endl<<r<<endl;
    int  ans = sum*r%P;
    if(ans<0){
        ans+=P;
    }
    cout<<ans;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <map>
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
#define maxn 101000
using namespace std;
typedef long long ll;
const ll mod=1e9+9;
ll knum[maxn];
ll fun(ll x){
    x=x%mod;
    if(x<0){
        x=x+mod;
    }
    return x;
}
ll poww(ll x, ll n){
    if(n==1){
        return x;
    }
    if(n==0){
        return 1;
    }
    ll  p = poww(x,n/2);
    return (((p*p)%mod)*poww(x,n%2))%mod;
}
char s[maxn];
int main()
{
    ll n,a,b,k;
    cin>>n>>a>>b>>k;
    scanf("%s",s);
    for(int i = 0 ; i<k ; i++){
        if(s[i]=='+'){
            knum[i]=poww(a,n-i)*poww(b,i);
            knum[i]=knum[i]%mod;
        }
        else{
            knum[i]=-poww(a,n-i)*poww(b,i);
            knum[i]=knum[i]%mod;
        }
        if(i>0)
            knum[i]+=knum[i-1];
            knum[i]=knum[i]%mod;
    }
    ll num = (n+1)/k;
    if(num==0){
        printf("%I64d\n",knum[n]);
        return 0;
    }
    if(a!=b){
        ll ans =(knum[k-1]*poww(a,k))%mod-(((knum[k-1]*poww(a,k)%mod)*poww(b,k*num))%mod)/poww(a,k*num);
        ans = ans%mod;
        ans = ans/(poww(a,k)-poww(b,k));
        ans = ans%mod;
        if((n+1)%k==0)
            ans += 0;
        else{
            ans+=((knum[(n+1)%k-1]*poww(b,k*(num)))%mod)/poww(a,k*(num));
        }
        ans = ans%mod;
        if(ans<0){
            ans=ans+mod;
        }
        printf("%I64d",ans);
    }
    else{
        ll ans = knum[k-1]*num;
        ans = ans%mod;
        if((n+1)%k==0)
            ans += 0;
        else
            ans+=knum[(n+1)%k-1];
            ans=ans%mod;
        if(ans<0){
            ans=ans+mod;
        }
        printf("%I64d",ans);
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <map>
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
#define maxn 101000
using namespace std;
typedef long long ll;
const ll mod=1e9+9;
ll knum[maxn];
ll poww(ll x, ll n){
    if(n==1){
        return x;
    }
    if(n==0){
        return 1;
    }
    ll  p = poww(x,n/2);
    return (((p*p)%mod)*poww(x,n%2))%mod;
}
char s[maxn];
int main()
{
    ll n,a,b,k;
    cin>>n>>a>>b>>k;
    scanf("%s",s);
        for(int i = 0 ; i<k ; i++){
            if(s[i]=='+'){
                knum[i]=poww(a,n-i)*poww(b,i);
                knum[i]=knum[i]%mod;
            }
            else{
                knum[i]=-poww(a,n-i)*poww(b,i);
                knum[i]=knum[i]%mod;
            }
            if(i>0)
            knum[i]+=knum[i-1];
            knum[i]=knum[i]%mod;
        }
    ll num = (n+1)/k;
    if(num==0){
        printf("%I64d\n",knum[n]);
        return 0;
    }
    if(a!=b){
        ll ans =(knum[k-1]*poww(a,k))-(((knum[k-1]*poww(a,k)%mod)*poww(b,k*num))%mod)/poww(a,k*num);
        ans = ans%mod;
        ans = ans/(poww(a,k)-poww(b,k));
        ans = ans%mod;
        if((n+1)%k==0)
            ans += 0;
        else{
            ans+=((knum[(n+1)%k-1]*poww(b,k*(num+1)))%mod)/poww(a,k*(num+1));
        }
        ans = ans%mod;
        if(ans<0){
            ans=ans+mod;
        }
        printf("%I64d",ans);
    }
    else{
        ll ans = knum[k-1]*num;
        ans = ans%mod;
        if((n+1)%k==0)
        ans += 0;
        else
        ans+=knum[(n+1)%k-1];
        ans=ans%mod;
        if(ans<0){
            ans=ans+mod;
        }
        printf("%I64d",ans);
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <map>
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
#define maxn 101000
using namespace std;
typedef long long ll;
const ll mod=1e9+9;
ll knum[maxn];
ll fun(ll x){
    x=x%mod;
    if(x<0){
        x=x+mod;
    }
    return x;
}
ll poww(ll x, ll n){
    if(n==1){
        return x;
    }
    if(n==0){
        return 1;
    }
    ll  p = poww(x,n/2);
    return (((p*p)%mod)*poww(x,n%2))%mod;
}
char s[maxn];
int main()
{
    ll n,a,b,k;
    cin>>n>>a>>b>>k;
    scanf("%s",s);
    for(int i = 0 ; i<k ; i++){
        if(s[i]=='+'){
            knum[i]=poww(a,n-i)*poww(b,i);
            knum[i]=knum[i]%mod;
        }
        else{
            knum[i]=-poww(a,n-i)*poww(b,i);
            knum[i]=knum[i]%mod;
        }
        if(i>0)
            knum[i]+=knum[i-1];
            knum[i]=knum[i]%mod;
    }
    ll num = (n+1)/k;
    if(num==0){
        printf("%I64d\n",knum[n]);
        return 0;
    }
    if(a!=b){
        ll ans =(((knum[k-1]*poww(a,k))%mod)*poww(a,k*num))%mod-(((knum[k-1]*poww(a,k)%mod)*poww(b,k*num))%mod);
        ans = ans%mod;
        ans = ans/poww(a,k*num);
        ans = ans/(poww(a,k)-poww(b,k));
        ans = ans%mod;
        if((n+1)%k==0)
            ans += 0;
        else{
            ans+=((knum[(n+1)%k-1]*poww(b,k*(num)))%mod)/poww(a,k*(num));
        }
        ans = ans%mod;
        if(ans<0){
            ans=ans+mod;
        }
        printf("%I64d",ans);
    }
    else{
        ll ans = knum[k-1]*num;
        ans = ans%mod;
        if((n+1)%k==0)
            ans += 0;
        else
            ans+=knum[(n+1)%k-1];
            ans=ans%mod;
        if(ans<0){
            ans=ans+mod;
        }
        printf("%I64d",ans);
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <map>
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
#define maxn 101000
using namespace std;
typedef long long ll;
const ll mod=1e9+9;
ll knum[maxn];
ll poww(ll x, ll n){
    if(n==1){
        return x;
    }
    if(n==0){
        return 1;
    }
    ll  p = poww(x,n/2);
    return (((p*p)%mod)*poww(x,n%2))%mod;
}
char s[maxn];
int main()
{
    ll n,a,b,k;
    cin>>n>>a>>b>>k;
    scanf("%s",s);
        for(int i = 0 ; i<k ; i++){
            if(s[i]=='+'){
                knum[i]=poww(a,n-i)*poww(b,i);
                knum[i]=knum[i]%mod;
            }
            else{
                knum[i]=-poww(a,n-i)*poww(b,i);
                knum[i]=knum[i]%mod;
            }
            if(i>0)
            knum[i]+=knum[i-1];
            knum[i]=knum[i]%mod;
        }
    ll num = (n+1)/k;
    if(num==0){
        printf("%I64d\n",knum[n]);
        return 0;
    }
    if(a!=b){
        ll ans =(knum[k-1]*poww(a,k))%mod-(((knum[k-1]*poww(a,k)%mod)*poww(b,k*num))%mod)/poww(a,k*num);
        ans = ans%mod;
        ans = ans/(poww(a,k)-poww(b,k));
        ans = ans%mod;
        if((n+1)%k==0)
            ans += 0;
        else{
            ans+=((knum[(n+1)%k-1]*poww(b,k*(num+1)))%mod)/poww(a,k*(num+1));
        }
        ans = ans%mod;
        if(ans<0){
            ans=ans+mod;
        }
        printf("%I64d",ans);
    }
    else{
        ll ans = knum[k-1]*num;
        ans = ans%mod;
        if((n+1)%k==0)
            ans += 0;
        else
            ans+=knum[(n+1)%k-1];
            ans=ans%mod;
        if(ans<0){
            ans=ans+mod;
        }
        printf("%I64d",ans);
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <map>
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
#define maxn 101000
using namespace std;
typedef long long ll;
const ll mod=1e9+9;
ll knum[maxn];
ll poww(ll x, ll n){
    if(n==1){
        return x;
    }
    if(n==0){
        return 1;
    }
    ll  p = poww(x,n/2);
    return ((p*p)%mod*poww(x,n%2))%mod;
}
ll fun(ll x){
    x=(x)%mod;
    if(x<0){
        x=x+mod;
    }
    return x;
}
char s[maxn];
int main()
{
    ll n,a,b,k;
    cin>>n>>a>>b>>k;
    scanf("%s",s);
    for(int i = 0 ; i<k ; i++){
        if(s[i]=='+'){
            knum[i]=poww(a,n-i)*poww(b,i);
            knum[i]=knum[i]%mod;
        }
        else{
            knum[i]=-poww(a,n-i)*poww(b,i);
            knum[i]=knum[i]%mod;
        }
        if(i>0)
        knum[i]+=knum[i-1];
        knum[i]=knum[i]%mod;
    }
    ll q = (b*(poww(a,mod-2)))%mod;
    ll a0 = knum[k-1];
    ll num = (n+1)/k;
    ll ans = 0;
    if(q==1){
        ans=a0*(num);
    }
    else{
        ans = poww(q,k*num)-1;
        ans = ans%mod;
        ans = ans*poww(fun((poww(q,k)-1)),mod-2);
        ans = ans%mod;
        ans = ans*a0;
    }
    ans = fun(ans);
    printf("%I64d",ans);
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <map>
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
#define maxn 101000
using namespace std;
typedef long long ll;
const ll mod=1e9+9;
ll knum[maxn];
ll poww(ll x, ll n){
    if(n==1){
        return x;
    }
    if(n==0){
        return 1;
    }
    ll  p = poww(x,n/2);
    return (((p*p)%mod)*poww(x,n%2))%mod;
}
char s[maxn];
int main()
{
    ll n,a,b,k;
    cin>>n>>a>>b>>k;
    scanf("%s",s);
        for(int i = 0 ; i<k ; i++){
            if(s[i]=='+'){
                knum[i]=poww(a,n-i)*poww(b,i);
                knum[i]=knum[i]%mod;
            }
            else{
                knum[i]=-poww(a,n-i)*poww(b,i);
                knum[i]=knum[i]%mod;
            }
            if(i>0)
            knum[i]+=knum[i-1];
        }
    ll num = (n+1)/k;
    if(num==0){
        printf("%I64d\n",knum[n]);
        return 0;
    }
    if(a!=b){
    ll ans =(knum[k-1]*poww(a,k))-(((knum[k-1]*poww(a,k)%mod)*poww(b,k*num))%mod)/poww(a,k*num);
    ans = ans%mod;
    ans = ans/(poww(a,k)-poww(b,k));
    if((n+1)%k==0)
        ans += 0;
    else{
        ans+=((knum[(n+1)%k-1]*poww(b,k*(num+1)))%mod)/poww(a,k*(num+1));
    }
    ans = ans%mod;
    if(ans<0){
        ans=ans+mod;
    }
    printf("%I64d",ans);
    }else{
        ll ans = knum[k-1]*num;
        ans = ans%mod;
        if((n+1)%k==0)
        ans += 0;
        else
        ans+=knum[(n+1)%k-1];
        ans=ans%mod;
        if(ans<0){
            ans=ans+mod;
        }
        printf("%I64d",ans);
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <map>
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
#define maxn 101000
using namespace std;
typedef long long ll;
const ll mod=1e9+9;
ll knum[maxn];
ll poww(ll x, ll n){
    if(n==1){
        return x;
    }
    if(n==0){
        return 1;
    }
    ll  p = poww(x,n/2);
    return (((p*p)%mod)*poww(x,n%2))%mod;
}
char s[maxn];
int main()
{
    ll n,a,b,k;
    cin>>n>>a>>b>>k;
    scanf("%s",s);
        for(int i = 0 ; i<k ; i++){
            if(s[i]=='+'){
                knum[i]=poww(a,n-i)*poww(b,i);
                knum[i]=knum[i]%mod;
            }
            else{
                knum[i]=-poww(a,n-i)*poww(b,i);
                knum[i]=knum[i]%mod;
            }
            if(i>0)
            knum[i]+=knum[i-1];
        }
    ll num = (n+1)/k;
    if(num==0){
        printf("%I64d\n",knum[n]);
        return 0;
    }
    ll ans =(knum[k-1]*poww(a,k))-(((knum[k-1]*poww(a,k)%mod)*poww(b,k*num))%mod)/poww(a,k*num);
    ans = ans%mod;
    ans = ans/(poww(a,k)-poww(b,k));
    if((n+1)%k==0)
        ans += 0;
    else{
        ans+=((knum[(n+1)%k-1]*poww(b,k*(num+1)))%mod)/poww(a,k*(num+1));
    }
    ans = ans%mod;
    if(ans<0){
        ans=ans+mod;
    }
    printf("%I64d",ans);
    return 0;
}


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

const int N = 101000;

int a[N];
vector <int> adj[N];

int dist[N], dist2[N];
int pass[N];

void dfs(int u, int v) {
	for (int x: adj[u]) {
		if(x == v) continue;
		dist[x] = dist[u] + 1;
		dfs(x, u);
	}
}

void dfs2(int u, int v) {
	for (int x: adj[u]) {
		if(x == v) continue;
		dist2[x] = dist2[u] + 1;
		dfs2(x, u);
	}
}

int main() {
	int n, k;
	scanf("%d%d", &n, &k);
	for (int i = 0; i < n-1; ++i) {
		int u, v;
		scanf("%d%d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dist[1] = 0;
	dfs(1, -1);
	int best = 0, id = 0;
	for (int i = 1; i <= n; ++i) {
		if (best < dist[i]) {
			id = i;
			best = dist[i];
		}
	}
	dist[id] = 0;
	dfs(id, -1);
	best = 0, id = 0;
	for (int i = 1; i <= n; ++i) {
		if (best < dist[i]) {
			id = i;
			best = dist[i];
		}
	}
	dist2[id] = 0;
	dfs2(id, -1);

	int qtd = 0, cen = 0;
	int ok = 1;
	if(best % 2 == 1) {
		ok = 0;
	}
	if (ok) {
		for (int i = 1; i <= n; ++i) {
			if(dist[i] == best/2 && dist2[i] == best/2) {
				qtd++;
				cen = i;
				//printf("%d %d\n", dist[i], cen);
			}
		}
	}
	if (qtd != 1) {
		ok = 0;
	}
	if (ok) {
		queue <pair<int, int> > q;
		q.push(make_pair(cen, 1));
		pass[cen] = 1;
		while(!q.empty()) {

			int f = q.front().first;
			int s = q.front().second;
			//printf("print %d %d\n", f, s);
			if (s > k) {
				while (!q.empty()) {
					int f = q.front().first;
					//printf("%d\n", f);
					q.pop();
					int qtd2 = 0;
					for (int x: adj[f]) {
						if (pass[x] == 0) {
							qtd2++;
						}
					}
					if(qtd2 != 0) {
						//printf("qtd2 %d\n", qtd2);
						ok = 0;
						break;
					}
				}
				break;
			}

			q.pop();
			int qtd = 0;
			for (int x: adj[f]) {
				if(pass[x] == 0) {
					qtd++;
				}
			}
			if(qtd >= 3) {
				for (int x: adj[f]) {
					if (pass[x] == 0) {
						pass[x] = 1;
						q.push(make_pair(x, s+1));
					}
				}
			} else {
				ok = 0;
				break;
			}
		}

	}
	if (ok) puts("Yes");
	else puts("No");

}


//Littleboy123 Template 1.7
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

//pragmas

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")t
// #pragma GCC optimization ("unroll-loops")

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef unsigned long long int ull;
typedef long double ld;
typedef unsigned long long ull;

const long double pi = acos(-1.0);

#define FOR(a,b,c) for(ll (a)=(b);(a)<(c);++(a))
#define FORE(a,b,c) for(ll (a)=(b);(a)<=(c);++(a))
#define FORN(a,b,c) for(ll (a)=(b);(a)>(c);--(a))
#define FORNE(a,b,c) for(ll (a)=(b);(a)>=(c);--(a))
#define TC(n) cout << "Case #" << (n) << ": "
#define DEBUG(x) cout << "DEBUG " << #x << ' ' << x << '\n';
#define MEM(a,b) memset((a), (b), sizeof((a)))
#define MAX(a,b) (a) = max((a), (b))
#define MIN(a,b) (a) = min((a), (b))
#define nl '\n'
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>

void init(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

void infile(){
    freopen("fenwick.in", "r", stdin);
}

void outfile(){
    freopen("fenwick.out", "w", stdout);
}

ll modex(ll a, ll b, ll mod){
    if(b == 0)
        return 1;
    if(b == 1)
        return a;
    ll tmp = modex(a, b/2LL, mod);
    tmp = (tmp * tmp) % mod;
    if(b & 1LL)
        tmp = (tmp * a) % mod;
    return tmp;
}

//End of template

vi adjlist[100005];
int deg[100005];
queue<ii> q;
int visited[100005];
int level[100005];

int main(){
    init();

    int n, k;
    cin >> n >> k;
    int u, v;
    FOR(i, 0, 100005){
        deg[i] = 0;
        visited[i] = 0;
    }
    FOR(i, 1, n){
        cin >> u >> v;
        adjlist[u].pb(v);
        adjlist[v].pb(u);
        deg[u]++;
        deg[v]++;
    }
    int cntmaks = 0;
    FORE(i, 1, n){
        if(deg[i] == 1){
            q.push(mp(i, 1));
            level[i] = 1;
            visited[i] = 1;
            cntmaks++;
        }
    }
    int front, sz, lv;
    int maks = 1;
    while(q.empty() == false){
        front = q.front().fi;
        lv = q.front().se;
        q.pop();
        sz = adjlist[front].size();
        FOR(i, 0, sz){
            if(visited[adjlist[front][i]])
                continue;
            q.push(mp(adjlist[front][i], lv+1));
            visited[adjlist[front][i]] = 1;
            level[adjlist[front][i]] = lv+1;
            //cout << adjlist[front][i] << ' ' << lv+1 << endl;
            if(lv+1 > maks){
                maks = lv+1;
                cntmaks = 1;
            }
            else if(lv+1 == maks){
                cntmaks++;
            }
        }
    }
    if(cntmaks != 1 || n <= 3){
        cout << "No\n";
        return 0;
    }
    int flag = 0;
    int cnt = 0;
    int cnta = 0;
    FORE(i, 1, n){
        if(level[i] == 1)
            continue;
        sz = adjlist[i].size();
        cnt = cnta = 0;
        FOR(j, 0, sz){
            if(level[adjlist[i][j]] == level[i]-1){
                cnt++;
            }
            else if(level[adjlist[i][j]] == level[i]+1){
                cnta++;
            }
            else{
                flag = 1;
                break;
            }
        }
        //cout << i << ' ' << cnta << ' ' << cnt << endl;
        if(cnt >= 3){
            if(cnta != 1){
                if(level[i] != maks)
                    flag = 1;
            }
        }
        else{
            flag = 1;
        }
        if(flag){
            break;
        }
    }
    if(flag){
        cout << "No\n";
        return 0;
    }
    if(maks-1 == k){
        cout << "Yes\n";
    }
    else{
        cout << "No\n";
    }
    return 0;
}


//Littleboy123 Template 1.7
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

//pragmas

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")t
// #pragma GCC optimization ("unroll-loops")

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef unsigned long long int ull;
typedef long double ld;
typedef unsigned long long ull;

const long double pi = acos(-1.0);

#define FOR(a,b,c) for(ll (a)=(b);(a)<(c);++(a))
#define FORE(a,b,c) for(ll (a)=(b);(a)<=(c);++(a))
#define FORN(a,b,c) for(ll (a)=(b);(a)>(c);--(a))
#define FORNE(a,b,c) for(ll (a)=(b);(a)>=(c);--(a))
#define TC(n) cout << "Case #" << (n) << ": "
#define DEBUG(x) cout << "DEBUG " << #x << ' ' << x << '\n';
#define MEM(a,b) memset((a), (b), sizeof((a)))
#define MAX(a,b) (a) = max((a), (b))
#define MIN(a,b) (a) = min((a), (b))
#define nl '\n'
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>

void init(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

void infile(){
    freopen("fenwick.in", "r", stdin);
}

void outfile(){
    freopen("fenwick.out", "w", stdout);
}

ll modex(ll a, ll b, ll mod){
    if(b == 0)
        return 1;
    if(b == 1)
        return a;
    ll tmp = modex(a, b/2LL, mod);
    tmp = (tmp * tmp) % mod;
    if(b & 1LL)
        tmp = (tmp * a) % mod;
    return tmp;
}

//End of template

vi adjlist[100005];
int deg[100005];
queue<ii> q;
int visited[100005];
int level[100005];

int main(){
    init();

    int n, k;
    cin >> n >> k;
    int u, v;
    FOR(i, 0, 100005){
        deg[i] = 0;
        visited[i] = 0;
    }
    FOR(i, 1, n){
        cin >> u >> v;
        adjlist[u].pb(v);
        adjlist[v].pb(u);
        deg[u]++;
        deg[v]++;
    }
    int cntmaks = 0;
    FORE(i, 1, n){
        if(deg[i] == 1){
            q.push(mp(i, 1));
            level[i] = 1;
            visited[i] = 1;
            cntmaks++;
        }
    }
    int front, sz, lv;
    int maks = 1;
    while(q.empty() == false){
        front = q.front().fi;
        lv = q.front().se;
        q.pop();
        sz = adjlist[front].size();
        FOR(i, 0, sz){
            if(visited[adjlist[front][i]])
                continue;
            q.push(mp(adjlist[front][i], lv+1));
            visited[adjlist[front][i]] = 1;
            level[adjlist[front][i]] = lv+1;
            //cout << adjlist[front][i] << ' ' << lv+1 << endl;
            if(lv+1 > maks){
                maks = lv+1;
                cntmaks = 1;
            }
            else if(lv+1 == maks){
                cntmaks++;
            }
        }
    }
    if(cntmaks != 1 || n <= 3){
        cout << "No\n";
        return 0;
    }
    int flag = 0;
    int cnt = 0;
    int cnta = 0;
    FORE(i, 1, n){
        if(level[i] == 1)
            continue;
        sz = adjlist[i].size();
        cnt = cnta = 0;
        FOR(j, 0, sz){
            if(level[adjlist[i][j]] == level[i]-1){
                cnt++;
            }
            else if(level[adjlist[i][j]] == level[i]+1){
                cnta++;
            }
            else{
                flag = 1;
                break;
            }
        }
        //cout << i << ' ' << cnta << ' ' << cnt << endl;
        if(cnt >= 3){
            if(cnta != 1){
                if(level[i] != maks)
                    flag = 1;
            }
        }
        else{
            flag = 1;
        }
        if(flag){
            break;
        }
    }
    if(flag)
        cout << "No\n";
    else
        cout << "Yes\n";
    return 0;
}


#include <bits/stdc++.h>
#define Nmax 100005
#define MOD 998244353
#define ll long long
using namespace std;

int n,m,k,nrf[Nmax],niv;
vector<int> v[Nmax],H,H2;
bool uz[Nmax];
int fst[Nmax],lst,a,b;
int main()
{
    ios::sync_with_stdio(false);

    cin >> n >> k;
    for(int i=1;i<n;i++){
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
        nrf[a]++;nrf[b]++;
    }

    int ctr = 0;
    for (int i=1;i<=n;i++){
        if (v[i].size()==1){
            H.push_back(i);
        }
        else if (v[i].size()==3){
            if (ctr){
                cout << "No\n";
                return 0;
            }
            ctr = i;
        }
        else if (v[i].size()<=3){
            cout << "No\n";
            return 0;
        }
    }
    //for (auto it : H) cout << it << ' ';
    niv = 1;
    while (true){
        niv++;
        while (!H.empty()){
            int crt = H.back();
            H.pop_back();
            for (auto it : v[crt]){
                if (!uz[it]){
                    if (fst[it] && fst[it]!=niv){
                        cout << "No\n";
                        return 0;
                    }else
                    fst[it] = niv;
                    nrf[it]--;
                    if (nrf[it]==1){
                        H2.push_back(it);
                        uz[it] = 1;
                    }
                }
            }
            lst = crt;
        }
        if (H2.empty()){
            break;
        }
//        for (auto it : H2) cout << it << ' ';
//        cout << '\n';
        H = H2;
        H2.clear();
    }
    if (niv != k + 2){
        cout << "No\n";
        return 0;
    }
    if (ctr && lst!=ctr){
        cout << "No\n";
        return 0;
    }

    cout << "Yes\n";

    return 0;
}
/*
12 2
1 4
2 4
3 4
4 12
10 5
11 5
5 12
6 7
8 6
12 6
9 6
*/


#include <bits/stdc++.h>
#define Nmax 100005
#define MOD 998244353
#define ll long long
using namespace std;

int n,m,k,nrf[Nmax],niv;
vector<int> v[Nmax],H,H2;
bool uz[Nmax];
int fst[Nmax],lst,a,b;

void dfs(int nod,int ant,int niv){
    if (v[nod].size()==1){
        if (niv != k){
            cout << "No\n";
            exit(0);
        }
        return;
    }
    if (v[nod].size()<3 || (ant!=-1 && v[nod].size()==3)){
        cout << "No\n";
        exit(0);
    }
    for (auto it : v[nod]){
        if(it==ant) continue;
        dfs(it,nod,niv+1);
    }
}

int main()
{
    ios::sync_with_stdio(false);

    cin >> n >> k;
    for(int i=1;i<n;i++){
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
        nrf[a]++;nrf[b]++;
    }

    int ctr = 0;
    for (int i=1;i<=n;i++){
        if (v[i].size()==1){
            H.push_back(i);
        }
        else if (v[i].size()==3){
            if (ctr){
                cout << "No\n";
                return 0;
            }
            ctr = i;
        }
        else if (v[i].size()<=3){
            cout << "No\n";
            return 0;
        }
    }
    //for (auto it : H) cout << it << ' ';
    niv = 1;
    while (true){
        niv++;
        while (!H.empty()){
            int crt = H.back();
            H.pop_back();
            for (auto it : v[crt]){
                if (!uz[it]){
                    if (fst[it] && fst[it]!=niv){
                        cout << "No\n";
                        return 0;
                    }
                    fst[it] = niv;
                    nrf[it]--;
                    if (nrf[it]==1){
                        H2.push_back(it);
                        uz[it] = 1;
                    }
                }
            }
            lst = crt;
        }
        if (H2.empty()){
            break;
        }
        H = H2;
        H2.clear();
    }
    if (niv != k + 2){
        cout << "No\n";
        return 0;
    }
    if (ctr && lst!=ctr){
        cout << "No\n";
        return 0;
    }

    dfs(lst, -1, 0);

    cout << "Yes\n";

    return 0;
}
/*
17 3
1 2
2 3
2 4
1 5
5 6
5 7
1 8
1 9
1 10
3 11
3 12
3 13
4 14
4 15
4 16
5 17

7 2
1 2
1 3
1 4
2 5
2 6
2 7

16 2
1 2
1 3
1 4
4 5
4 6
4 7
3 8
3 9
3 10
2 11
2 12
2 13
13 14
13 15
13 16
*/


#include <bits/stdc++.h>
#define Nmax 100005
#define MOD 998244353
#define ll long long
using namespace std;

int n,m,k,nrf[Nmax],niv;
vector<int> v[Nmax],H,H2;
bool uz[Nmax];
int fst[Nmax],lst,a,b;
int main()
{
    ios::sync_with_stdio(false);

    cin >> n >> k;
    for(int i=1;i<n;i++){
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
        nrf[a]++;nrf[b]++;
    }

    int ctr = 0;
    for (int i=1;i<=n;i++){
        if (v[i].size()==1){
            H.push_back(i);
        }
        else if (v[i].size()==3){
            if (ctr){
                cout << "No\n";
                return 0;
            }
            ctr = i;
        }
        else if (v[i].size()<=3){
            cout << "No\n";
            return 0;
        }
    }
    //for (auto it : H) cout << it << ' ';
    niv = 1;
    while (true){
        niv++;
        while (!H.empty()){
            int crt = H.back();
            H.pop_back();
            for (auto it : v[crt]){
                if (!uz[it]){
                    if (fst[it] && fst[it]!=niv){
                        cout << "No\n";
                        return 0;
                    }else
                    fst[it] = niv;
                    nrf[it]--;
                    if (nrf[it]==1){
                        H2.push_back(it);
                        uz[it] = 1;
                    }
                }
            }
            lst = crt;
        }
        if (H2.empty()){
            break;
        }
//        for (auto it : H2) cout << it << ' ';
//        cout << '\n';
        H = H2;
        H2.clear();
    }
    if (ctr && lst!=ctr){
        cout << "No\n";
        return 0;
    }

    cout << "Yes\n";

    return 0;
}
/*
12 2
1 4
2 4
3 4
4 12
10 5
11 5
5 12
6 7
8 6
12 6
9 6
*/


#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<map>#define ll long long#define pb push_back#define mp make_pair#define dbg(x) cout<<#x<<' '<<x<<endlusing namespace std;const int maxn=3e5+5;const int inf=0x3f3f3f3f;const int mod=1e9+7;int ele[maxn];char str[maxn];int in[maxn];int vis[maxn];queue<int>qq;vector<int>ss[maxn];int main(){    //freopen(".in","r",stdin);    int n,k;    scanf("%d%d",&n,&k);    for(int i=1;i<n;i++)    {        int a,b;        scanf("%d%d",&a,&b);        in[a]++;        in[b]++;        ss[a].pb(b);        ss[b].pb(a);    }    for(int i=1;i<=n;i++)    {        if(in[i]==1)vis[i]=1,qq.push(i);    }    int mm=0;    pair<int,int>cc;    cc.first=0;    cc.second=0;    while(!qq.empty())    {        int id=qq.front();        qq.pop();        for(int i=0;i<ss[id].size();i++)        {            int v=ss[id][i];            if(!vis[v])            {                vis[v]=vis[id]+1,qq.push(v);                if(vis[v]>mm)                {                    mm=vis[v];                    cc.first=v;                    cc.second=1;                }                else if(mm==vis[v])                {                    cc.first=v;                    cc.second++;                }            }        }    }    if(cc.second>=2||mm-1!=k)printf("No\n");    else    {        int flag=1;        for(int i=1;i<=n;i++)        {            if(i==cc.first)            {                if(in[cc.first]<3)flag=0;            }            else            {                if(vis[i]>=2&&in[i]<=3)flag=0;                else if(vis[i]>=2&&in[i]>3)                {                    for(int j=0;j<ss[i].size();j++)                    {                        int vv=ss[i][j];                        if(abs(vis[i]-vis[vv])>=2||vis[i]-vis[vv]==0)//判断掉同级相连的                        {                            flag=0;                            break;                        }                    }                }            }            if(flag==0)break;        }        if(flag)printf("Yes\n");        else printf("No\n");    }    return 0;}


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#define ll long long
#define pb push_back
#define mp make_pair
#define dbg(x) cout<<#x<<' '<<x<<endl
using namespace std;
const int maxn=3e5+5;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int ele[maxn];
char str[maxn];
int in[maxn];
int vis[maxn];
queue<int>qq;
vector<int>ss[maxn];
int main()
{
    //freopen(".in","r",stdin);
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1;i<n;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        in[a]++;
        in[b]++;
        ss[a].pb(b);
        ss[b].pb(a);
    }
    for(int i=1;i<=n;i++)
    {
        if(in[i]==1)vis[i]=1,qq.push(i);
    }
    int mm=0;
    pair<int,int>cc;
    cc.first=0;
    cc.second=0;
    while(!qq.empty())
    {
        int id=qq.front();
        qq.pop();
        for(int i=0;i<ss[id].size();i++)
        {
            int v=ss[id][i];
            if(!vis[v])
            {
                vis[v]=vis[id]+1,qq.push(v);
                if(vis[v]>mm)
                {
                    mm=vis[v];
                    cc.first=v;
                    cc.second=1;
                }
                else if(mm==vis[v])
                {
                    cc.first=v;
                    cc.second++;
                }
            }
        }
    }
    if(cc.second>=2||mm-1!=k)printf("No\n");
    else
    {
        int flag=1;
        for(int i=1;i<=n;i++)
        {
            if(i==cc.first)
            {
                if(in[cc.first]<3)flag=0;
            }
            else
            {
                if(vis[i]>=2&&in[i]<=3)flag=0;
                else if(vis[i]>=2&&in[i]>3)
                {
                    for(int j=0;j<ss[i].size();j++)
                    {
                        int vv=ss[i][j];
                        if(abs(vis[i]-vis[vv])>=2||vis[i]-vis[vv]==0)//判断掉同级相连的
                        {
                            flag=0;
                            break;
                        }
                    }
                }
            }
            if(flag==0)break;
        }
        if(flag)printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#define ll long long
#define pb push_back
#define mp make_pair
#define dbg(x) cout<<#x<<' '<<x<<endl
using namespace std;
const int maxn=3e5+5;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int ele[maxn];
char str[maxn];
int in[maxn];
int vis[maxn];
queue<int>qq;
vector<int>ss[maxn];
int main()
{
    //freopen("./Input.cc","r",stdin);
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1;i<n;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        in[a]++;
        in[b]++;
        ss[a].pb(b);
        ss[b].pb(a);
    }
    for(int i=1;i<=n;i++)
    {
        if(in[i]==1)vis[i]=1,qq.push(i);
    }
    int mm=0;
    pair<int,int>cc;
    cc.first=0;
    cc.second=0;
    while(!qq.empty())
    {
        int id=qq.front();
        qq.pop();
        for(int i=0;i<ss[id].size();i++)
        {
            int v=ss[id][i];
            if(!vis[v])
            {
                vis[v]=vis[id]+1,qq.push(v);
                if(vis[v]>mm)
                {
                    mm=vis[v];
                    cc.first=v;
                    cc.second=1;
                }
                else if(mm==vis[v])
                {
                    cc.first=v;
                    cc.second++;
                }
            }
        }
    }
    if(cc.second>=2||mm-1!=k)printf("No\n");
    else
    {
        int flag=1;
        for(int i=1;i<=n;i++)
        {
            if(i==cc.first)
            {
                if(in[cc.first]<3)flag=0;
            }
            else
            {
                if(vis[i]>=2&&in[i]<=3)flag=0;
            }
            if(flag==0)break;
        }
        if(flag)printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,K;
struct edge{
	int to,next;
}e[N<<1];
int head[N],cnt,num;
int vis[N];
int in[N];
inline void add(int x,int y){
	e[++cnt].to=y;
	e[cnt].next=head[x];
	head[x]=cnt;
}
inline void solve(){
	queue<int> q[2];
	for(int i=1;i<=n;i++)
		if(in[i]==1) q[0].push(i),vis[i]=0;
	for(int	t=0;;t^=1){
		bool flag=true;
		while(!q[t].empty()){
			int x=q[t].front();q[t].pop();
			if(in[x]==0) continue;
			int u;
            for(int i=head[x];i;i=e[i].next){
                if(in[e[i].to]==0) continue;
                u=e[i].to;break;
            }
            if(vis[u]==t||in[u]<3){flag=false;break;}
			int tot=0;
			for(int i=head[u];i;i=e[i].next){
				int v=e[i].to;
				if(in[v]==1&&vis[v]==t) in[v]--,in[u]--,tot++;
			}
			if(tot<3){flag=false;break;}
			q[t^1].push(u);vis[u]=t^1;
		}
		if(!flag){num=-1;return;}
		if(q[t^1].empty()) break;
		num++;
	}
}
int main(){
	scanf("%d%d",&n,&K);
	memset(vis,-1,sizeof(vis));
	for(int i=1,x,y;i<n;i++){
		scanf("%d%d",&x,&y);
		add(x,y);add(y,x);
		in[x]++;in[y]++;
	}
	if(K>=n){printf("No\n");return 0;}
	solve();
	if(num==K) printf("Yes\n");
	else printf("No\n");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,K;
struct edge{
	int to,next;
}e[N<<1];
int head[N],cnt,num;
int vis[N];
int in[N];
inline void add(int x,int y){
	e[++cnt].to=y;
	e[cnt].next=head[x];
	head[x]=cnt;
}
inline void solve(){
	queue<int> q[2];
	for(int i=1;i<=n;i++)
		if(in[i]==1) q[0].push(i),vis[i]=0;
	for(int	t=0;;t^=1){
		while(!q[t].empty()){
			int x=q[t].front();q[t].pop();
			if(in[x]==0) continue;
			int u;
            for(int i=head[x];i;i=e[i].next){
                if(in[e[i].to]==0) continue;
                u=e[i].to;break;
            }
            if(in[u]<3){printf("No\n");exit(0);}
			int tot=0;
			for(int i=head[u];i;i=e[i].next){
				int v=e[i].to;
				if(in[v]==1&&vis[v]==t) in[v]--,in[u]--,tot++;
			}
			if(tot<3){printf("No\n");exit(0);}
			q[t^1].push(u);vis[u]=t^1;
		}
		if(q[t^1].empty()) break;
		num++;
	}
}
int main(){
	scanf("%d%d",&n,&K);
	memset(vis,-1,sizeof(vis));
	for(int i=1,x,y;i<n;i++){
		scanf("%d%d",&x,&y);
		add(x,y);add(y,x);
		in[x]++;in[y]++;
	}
	if(K>=n){printf("No\n");return 0;}
	solve();
	if(num==K) printf("Yes\n");
	else printf("No\n");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,K;
struct edge{
	int to,next;
}e[N<<1];
int head[N],cnt;
int in[N],cut[N],vis[N];
inline void add(int x,int y){
	e[++cnt].to=y;
	e[cnt].next=head[x];
	head[x]=cnt;
}
struct node{
	int point,k;
};
inline void solve(){
	queue<node> q;
	for(int i=1;i<=n;i++)
		if(in[i]==1){
			node t;
			t.k=K;
			t.point=i;
			q.push(t);
		}
	while(!q.empty()){
		node temp=q.front();
		int u=temp.point,k=temp.k;
		vis[u]=true;
		q.pop();
		for(int i=head[u];i;i=e[i].next){
			int v=e[i].to;
			if(vis[v]) continue;
			in[v]--;
			cut[v]++;
			if(in[v]==1){
				if(k>=2&&cut[v]>=3){
					temp.point=v,temp.k=k-1;
					q.push(temp);
				}
				else if(k==1&&cut[v]>=2){
					temp.point=v,temp.k=k-1;
					q.push(temp);
				}
				else{
					printf("No");
					exit(0);
				}
			}
		}
	}
}
int main(){
	scanf("%d%d",&n,&K);
	for(int i=1,x,y;i<n;i++){
		scanf("%d%d",&x,&y);
		add(x,y);add(y,x);
		in[x]++;in[y]++;
	}
	solve();
	printf("Yes");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,K;
struct edge{
	int to,next;
}e[N<<1];
int head[N],cnt;
int in[N],cut[N],vis[N];
inline void add(int x,int y){
	e[++cnt].to=y;
	e[cnt].next=head[x];
	head[x]=cnt;
}
struct node{
	int point,k;
};
inline void solve(){
	queue<node> q;
	for(int i=1;i<=n;i++)
		if(in[i]==1){
			node t;
			t.k=K;
			t.point=i;
			q.push(t);
		}
	while(!q.empty()){
		node temp=q.front();
		int u=temp.point,k=temp.k;
		vis[u]=true;
		q.pop();
		for(int i=head[u];i;i=e[i].next){
			int v=e[i].to;
			if(vis[v]) continue;
			in[v]--;
			cut[v]++;
			if(in[v]==1){
				if(k>=2&&cut[v]>=3){
					temp.point=v,temp.k=k-1;
					q.push(temp);
				}
				else if(k==1&&cut[v]>=2){
					temp.point=v,temp.k=k-1;
					q.push(temp);
				}
				else{
					printf("No");
					exit(0);
				}
			}
		}
	}
}
int main(){
	scanf("%d%d",&n,&K);
	for(int i=1,x,y;i<n;i++){
		scanf("%d%d",&x,&y);
		add(x,y);add(y,x);
		in[x]++;in[y]++;
	}
	if(n<=3){
		printf("No");
		return 0;
	}
	solve();
	printf("Yes");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,K;
struct edge{
	int to,next;
}e[N<<1];
int head[N],cnt,flag,tot;
int in[N],cut[N],vis[N];
int check[N];
inline void add(int x,int y){
	e[++cnt].to=y;
	e[cnt].next=head[x];
	head[x]=cnt;
}
inline void solve(){
	queue<int> q;
	for(int i=1;i<=n;i++)
		if(in[i]==1) q.push(i);
	while(!q.empty()){
		int u=q.front();
		q.pop();
		vis[u]=true;
		tot++;
		printf("cut%d %d\n",u,cut[u]);
		if(tot!=n){
			if(check[u]){
				printf("No");
				exit(0);
			}
		}
		else{
			if(cut[u]<3){
				printf("No");
				exit(0);
			}
		}
		for(int i=head[u];i;i=e[i].next){
			int v=e[i].to;
			if(vis[v])
				continue;
			in[v]--;
			cut[v]++;
			if(in[v]==1){
				if(cut[v]>=3){
					q.push(v);
				}
				else{
					q.push(v);
					check[v]=true;
				}
			}
		}
	}
}
int main(){
	scanf("%d%d",&n,&K);
	for(int i=1,x,y;i<n;i++){
		scanf("%d%d",&x,&y);
		add(x,y);add(y,x);
		in[x]++;in[y]++;
	}
	solve();
	printf("Yes");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,K;
struct edge{
	int to,next;
}e[N<<1];
int head[N],cnt,num;
int vis[N];
int in[N];
inline void add(int x,int y){
	e[++cnt].to=y;
	e[cnt].next=head[x];
	head[x]=cnt;
}
inline void solve(){
	queue<int> q[2];
	for(int i=1;i<=n;i++)
		if(in[i]==1) q[0].push(i),vis[i]=0;
	for(int	t=0;;t^=1){
		while(!q[t].empty()){
			int x=q[t].front();q[t].pop();
			if(in[x]==0) continue;
			int u;
            for(int i=head[x];i;i=e[i].next){
                if(in[e[i].to]==0) continue;
                u=e[i].to;break;
            }
            if(vis[u]==t||in[u]<3){printf("No\n");exit(0);}
			int tot=0;
			for(int i=head[u];i;i=e[i].next){
				int v=e[i].to;
				if(in[v]==1&&vis[v]==t) in[v]--,in[u]--,tot++;
			}
			if(tot<3){printf("No\n");exit(0);}
			q[t^1].push(u);vis[u]=t^1;
		}
		if(q[t^1].empty()) break;
		num++;
	}
}
int main(){
	scanf("%d%d",&n,&K);
	memset(vis,-1,sizeof(vis));
	for(int i=1,x,y;i<n;i++){
		scanf("%d%d",&x,&y);
		add(x,y);add(y,x);
		in[x]++;in[y]++;
	}
	if(K>=n){printf("No\n");return 0;}
	solve();
	if(num==K) printf("Yes\n");
	else printf("No\n");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,K;
struct edge{
	int to,next;
}e[N<<1];
int head[N],cnt,num;
int vis[N];
int in[N];
inline void add(int x,int y){
	e[++cnt].to=y;
	e[cnt].next=head[x];
	head[x]=cnt;
}
inline void solve(){
	queue<int> q[2];
	for(int i=1;i<=n;i++)
		if(in[i]==1) q[1].push(i),vis[i]=1;
	for(int flag=1;;flag^=1){
		while(!q[flag].empty()){
			int x=q[flag].front();q[flag].pop();
			if(in[x]==0) continue;
			int u;
            for(int i=head[x];i;i=e[i].next){
                if(in[e[i].to]==0) continue;
                u=e[i].to;break;
            }
            if(vis[u]==flag||in[u]<3){printf("No\n");exit(0);}
			int tot=0;
			for(int i=head[u];i;i=e[i].next){
				int v=e[i].to;
				if(in[v]==1&&vis[v]==flag) in[v]--,in[u]--,tot++;
			}
			if(cnt<3){printf("No\n");exit(0);}
			q[flag^1].push(u);vis[u]=flag^1;
		}
		if(q[flag^1].empty()) break;
		num++;
	}
}
int main(){
	scanf("%d%d",&n,&K);
	memset(vis,-1,sizeof(vis));
	for(int i=1,x,y;i<n;i++){
		scanf("%d%d",&x,&y);
		add(x,y);add(y,x);
		in[x]++;in[y]++;
	}
	if(K>=n){printf("No\n");return 0;}
	solve();
	if(num==K) printf("Yes\n");
	else printf("No\n");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,K;
struct edge{
	int to,next;
}e[N<<1];
int head[N],cnt,num;
int vis[N];
int in[N];
inline void add(int x,int y){
	e[++cnt].to=y;
	e[cnt].next=head[x];
	head[x]=cnt;
}
inline void solve(){
	queue<int> q[2];
	int flag=1;
	for(int i=1;i<=n;i++)
		if(in[i]==1) q[flag].push(i),vis[i]=1;
	for(int flag=1;;flag^=1){
		while(!q[flag].empty()){
			int x=q[flag].front();q[flag].pop();
			if(in[x]==0) continue;
			int u;
            for(int i=head[x];i;i=e[i].next){
                if(in[e[i].to]==0) continue;
                u=e[i].to;break;
            }
            if(vis[u]==flag||in[u]<3){
            	printf("No\n");
            	exit(0);
			}
			int tot=0;
			for(int i=head[u];i;i=e[i].next){
				int v=e[i].to;
				if(in[v]==1&&vis[v]==flag) in[v]--,in[u]--,tot++;
			}
			if(cnt<3){
				printf("No\n");
            	exit(0);
			}
			q[flag^1].push(u);vis[u]=flag^1;
		}
		if(q[flag^1].empty()) break;
		num++;
	}
}
int main(){
	scanf("%d%d",&n,&K);
	   memset(vis,-1,sizeof(vis));
	for(int i=1,x,y;i<n;i++){
		scanf("%d%d",&x,&y);
		add(x,y);add(y,x);
		in[x]++;in[y]++;
	}
	if(K>=n||n<=3){printf("No\n");return 0;}
	solve();
	if(num==K) printf("Yes\n");
	else printf("No\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
vector<int> adj[MAXN];
int dis[MAXN];
int main() {
	int n, k; cin >> n >> k;
	for(int i = 1; i < n; i++) {
		int a, b; cin >> a >> b;
		adj[a-1].push_back(b-1);
		adj[b-1].push_back(a-1);
	}
	for(int i = 0; i < n; i++) {
		dis[i] = -1;
	}
	queue<int> q;
	for(int i = 0; i < n; i++) {
		if(adj[i].size() == 0) continue;
		if(adj[i].size() == 1) {
			q.push(i);
			dis[i] = 0;
		}
		else if(adj[i].size() < 3) {
			cout << "No" << endl;
			return 0;
		}
	}
	while(!q.empty()) {
		int cur = q.front();
		q.pop();
		for(int i : adj[cur]) {
			if(dis[i] == -1) {
				q.push(i);
				dis[i] = dis[cur] + 1;
			}
		}
	}
	bool kf = 0;
	int ans = -1;
	for(int i = 0; i < n; i++) {
		if(dis[i] > k) {
			cout << "No" << endl;
			return 0;
		}
		if(dis[i] == k) {
			if(kf) {
				cout << "No" << endl;
				return 0;
			}
			kf = true;
			ans = i;
		}
	}
	if(!kf) {
		cout << "No" << endl;
	}
	for(int i = 0; i < n; i++) {
		dis[i] = -1;
	}
	q.push(ans);
	dis[ans] = 0;
	while(!q.empty()) {
		int cur = q.front();
		q.pop();
		for(int i : adj[cur]) {
			if(dis[i] == -1) {
				dis[i] = dis[cur] + 1;
				q.push(i);
			}
		}
	}
	for(int i = 0; i < n; i++) {
		if(adj[i].size() == 1) {
			if(dis[i] != k) {
				cout << "No" << endl;
				return 0;
			}
		}
	}
	cout << "Yes" << endl;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
vector<int> adj[MAXN];
int dis[MAXN];
int main() {
	int n, k; cin >> n >> k;
	for(int i = 1; i < n; i++) {
		int a, b; cin >> a >> b;
		adj[a-1].push_back(b-1);
		adj[b-1].push_back(a-1);
	}
	for(int i = 0; i < n; i++) {
		dis[i] = -1;
	}
	queue<int> q;
	for(int i = 0; i < n; i++) {
		if(adj[i].size() == 0) continue;
		if(adj[i].size() == 1) {
			q.push(i);
			dis[i] = 0;
		}
		else if(adj[i].size() < 3) {
			cout << "No" << endl;
			return 0;
		}
	}
	while(!q.empty()) {
		int cur = q.front();
		q.pop();
		for(int i : adj[cur]) {
			if(dis[i] == -1) {
				q.push(i);
				dis[i] = dis[cur] + 1;
			}
		}
	}
	bool kf = 0;
	for(int i = 0; i < n; i++) {
		if(dis[i] > k) {
			cout << "No" << endl;
			return 0;
		}
		if(dis[i] == k) {
			if(kf) {
				cout << "No" << endl;
				return 0;
			}
			kf = true;
		}
	}
	if(kf) cout << "Yes" << endl;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
vector<int> adj[MAXN];
int dis[MAXN];
int main() {
	int n, k; cin >> n >> k;
	for(int i = 1; i < n; i++) {
		int a, b; cin >> a >> b;
		adj[a-1].push_back(b-1);
		adj[b-1].push_back(a-1);
	}
	for(int i = 0; i < n; i++) {
		dis[i] = -1;
	}
	queue<int> q;
	for(int i = 0; i < n; i++) {
		if(adj[i].size() == 0) continue;
		if(adj[i].size() == 1) {
			q.push(i);
			dis[i] = 0;
		}
		else if(adj[i].size() < 3) {
			cout << "No" << endl;
			return 0;
		}
	}
	while(!q.empty()) {
		int cur = q.front();
		q.pop();
		for(int i : adj[cur]) {
			if(dis[i] == -1) {
				q.push(i);
				dis[i] = dis[cur] + 1;
			}
		}
	}
	bool kf = 0;
	int ans = -1;
	for(int i = 0; i < n; i++) {
		if(dis[i] > k) {
			cout << "No" << endl;
			return 0;
		}
		if(dis[i] == k) {
			if(kf) {
				cout << "No" << endl;
				return 0;
			}
			kf = true;
			ans = i;
		}
	}
	if(!kf) {
		cout << "No" << endl;
		return 0;
	}
	for(int i = 0; i < n; i++) {
		dis[i] = -1;
	}
	q.push(ans);
	dis[ans] = 0;
	while(!q.empty()) {
		int cur = q.front();
		q.pop();
		for(int i : adj[cur]) {
			if(dis[i] == -1) {
				dis[i] = dis[cur] + 1;
				q.push(i);
			}
		}
	}
	for(int i = 0; i < n; i++) {
		if(adj[i].size() == 1) {
			if(dis[i] != k) {
				cout << "No" << endl;
				return 0;
			}
		}
	}
	if(adj[ans].size() < 3) {
		cout << "No" << endl;
		return 0;
	}
	for(int i = 0; i < n; i++) {
		if(i != ans && adj[i].size() != 1 && adj[i].size() < 4) {
			cout << "No" << endl;
			return 0;
		}
	}
	cout << "Yes" << endl;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
vector<int> adj[MAXN];
int dis[MAXN];
int main() {
	int n, k; cin >> n >> k;
	for(int i = 1; i < n; i++) {
		int a, b; cin >> a >> b;
		adj[a-1].push_back(b-1);
		adj[b-1].push_back(a-1);
	}
	for(int i = 0; i < n; i++) {
		dis[i] = -1;
	}
	queue<int> q;
	for(int i = 0; i < n; i++) {
		if(adj[i].size() == 0) continue;
		if(adj[i].size() == 1) {
			q.push(i);
			dis[i] = 0;
		}
		else if(adj[i].size() < 3) {
			cout << "No" << endl;
			return 0;
		}
	}
	while(!q.empty()) {
		int cur = q.front();
		q.pop();
		for(int i : adj[cur]) {
			if(dis[i] == -1) {
				q.push(i);
				dis[i] = dis[cur] + 1;
			}
		}
	}
	bool kf = 0;
	int ans = -1;
	for(int i = 0; i < n; i++) {
		if(dis[i] > k) {
			cout << "No" << endl;
			return 0;
		}
		if(dis[i] == k) {
			if(kf) {
				cout << "No" << endl;
				return 0;
			}
			kf = true;
			ans = i;
		}
	}
	if(!kf) {
		cout << "No" << endl;
	}
	for(int i = 0; i < n; i++) {
		dis[i] = -1;
	}
	q.push(ans);
	dis[ans] = 0;
	while(!q.empty()) {
		int cur = q.front();
		q.pop();
		for(int i : adj[cur]) {
			if(dis[i] == -1) {
				dis[i] = dis[cur] + 1;
				q.push(i);
			}
		}
	}
	for(int i = 0; i < n; i++) {
		if(adj[i].size() == 1) {
			if(dis[i] != k) {
				cout << "No" << endl;
				return 0;
			}
		}
	}
	if(adj[ans].size() < 3) {
		cout << "No" << endl;
		return 0;
	}
	for(int i = 0; i < n; i++) {
		if(i != ans && adj[i].size() != 1 && adj[i].size() < 4) {
			cout << "No" << endl;
			return 0;
		}
	}
	cout << "Yes" << endl;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
vector<int> adj[MAXN];
int dis[MAXN];
int main() {
	int n, k; cin >> n >> k;
	for(int i = 1; i < n; i++) {
		int a, b; cin >> a >> b;
		adj[a-1].push_back(b-1);
		adj[b-1].push_back(a-1);
	}
	for(int i = 0; i < n; i++) {
		dis[i] = -1;
	}
	queue<int> q;
	for(int i = 0; i < n; i++) {
		if(adj[i].size() == 0) continue;
		if(adj[i].size() == 1) {
			q.push(i);
			dis[i] = 0;
		}
		else if(adj[i].size() < 3) {
			cout << "No" << endl;
			return 0;
		}
	}
	while(!q.empty()) {
		int cur = q.front();
		q.pop();
		for(int i : adj[cur]) {
			if(dis[i] == -1) {
				q.push(i);
				dis[i] = dis[cur] + 1;
			}
		}
	}
	bool kf = 0;
	int ans = -1;
	for(int i = 0; i < n; i++) {
		if(dis[i] > k) {
			cout << "No" << endl;
			return 0;
		}
		if(dis[i] == k) {
			if(kf) {
				cout << "No" << endl;
				return 0;
			}
			kf = true;
			ans = i;
		}
	}
	if(!kf) {
		cout << "No" << endl;
	}
	for(int i = 0; i < n; i++) {
		dis[i] = -1;
	}
	q.push(ans);
	dis[ans] = 0;
	while(!q.empty()) {
		int cur = q.front();
		q.pop();
		for(int i : adj[cur]) {
			if(dis[i] == -1) {
				dis[i] = dis[cur] + 1;
				q.push(i);
			}
		}
	}
	for(int i = 0; i < n; i++) {
		if(adj[i].size() == 1) {
			if(dis[i] != k) {
				cout << "No" << endl;
				return 0;
			}
		}
	}
	if(adj[ans].size() < 3) {
		cout << "No" << endl;
	}
	for(int i = 0; i < n; i++) {
		if(i != ans && adj[i].size() != 1 && adj[i].size() < 4) {
			cout << "No" << endl;
		}
	}
	cout << "Yes" << endl;
}


#include "bits/stdc++.h"
using namespace std;
#define all(v) (v).begin(), (v).end()
#define io ios::sync_with_stdio(0)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rson rt << 1 | 1, mid + 1, r
#define lson rt << 1, l, mid
#define lll __int128
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eps 1e-12
//#define int long long
const int mod =  998244353;
inline int ksm(int a, int b)
{
    int ans = 1;
    for (; b; b/=2, a = a * a % mod)
        if (b & 1)
            ans = ans * a % mod;
    return ans;
}

int du[200000];
vector<int> G[200000];
int n,k;
int ru[200000];
int lev[200000];
int vis[200000];
queue<int> q,q2;
signed main()
{
    io;
    int mx=0;
    cin>>n>>k;
    rep(i,1,n-1){
        int l,r;cin>>l>>r;
        G[l].pb(r);
        G[r].pb(l);
        du[l]++; du[r]++;
    }
    int fl=1;
    rep(i,1,n) if(du[i]==1)q.push(i),vis[i]=1;
    int cnt=0;
    map<int,int> in;
    while(!q.empty()){
        cnt++;
        int o=0;
        while(!q.empty()){
        int t=q.front();q.pop();
        o++;
        if(du[t] != 1 && ru[t] < 3){
            fl=0;break;
        }
        for(auto &i:G[t]){
                if(!in[i]){

                    ru[i]++;
                    if(!vis[i]){
                       q2.push(i);
                        vis[i]=1;
                    }
                }
            }
        }
        if(q2.empty()){
            fl &= (o==1);
        }
        in.clear();
        while(!q2.empty()){
            int t=q2.front();q2.pop();
            q.push(t);
            in[t]=1;
        }

        }
   // cout<<fl <<" "<<cnt<<endl;
    if(fl && cnt == k+1){
        cout<<"Yes"<<endl;
    }else cout<<"No"<<endl;

//
    getchar(); //cin>>n
    getchar();
}
/*

3
3
RRLLL

3
2
RRLL

*/


#include "bits/stdc++.h"
using namespace std;
#define all(v) (v).begin(), (v).end()
#define io ios::sync_with_stdio(0)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rson rt << 1 | 1, mid + 1, r
#define lson rt << 1, l, mid
#define lll __int128
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eps 1e-12
//#define int long long
const int mod =  998244353;
inline int ksm(int a, int b)
{
    int ans = 1;
    for (; b; b/=2, a = a * a % mod)
        if (b & 1)
            ans = ans * a % mod;
    return ans;
}

int du[200000];
vector<int> G[200000];
int n,k;
int ru[200000];
int lev[200000];
int vis[200000];
queue<int> q,q2;
signed main()
{
    io;
    int mx=0;
    cin>>n>>k;
    rep(i,1,n-1){
        int l,r;cin>>l>>r;
        G[l].pb(r);
        G[r].pb(l);
        du[l]++; du[r]++;
    }
    int fl=1;
    rep(i,1,n) if(du[i]==1)q.push(i),vis[i]=1;
    int cnt=0;
    int c=0;
    map<int,int> in;
    while(!q.empty()){
        cnt++;
        int o=0;
        while(!q.empty()){
        int t=q.front();q.pop();
        int kk=0;
         for(auto &i:G[t]){
             if(!vis[i]){
                 kk = 1;
             }
         }
         ru[t]+=kk;
        c++;
        o++;
        if(du[t] != 1 && ru[t] < 3){
            fl=0;break;
        }
        for(auto &i:G[t]){
                if(!in[i]){
                    ru[i]++;
                    if(!vis[i]){
                       q2.push(i);
                        vis[i]=1;
                    }
                }
            }
        }
        if(q2.empty()){
            fl &= (o==1);
        }
       // in.clear();
        while(!q2.empty()){
            int t=q2.front();q2.pop();
            q.push(t);
            in[t]=1;
        }

        }
   // cout<<fl <<" "<<cnt<<endl;
    if(fl && cnt == k+1 && c == n){
        cout<<"Yes"<<endl;
    }else cout<<"No"<<endl;

//
    getchar(); //cin>>n
    getchar();
}
/*

3
3
RRLLL

3
2
RRLL

*/


#include "bits/stdc++.h"
using namespace std;
#define all(v) (v).begin(), (v).end()
#define io ios::sync_with_stdio(0)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rson rt << 1 | 1, mid + 1, r
#define lson rt << 1, l, mid
#define lll __int128
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eps 1e-12
//#define int long long
const int mod =  998244353;
inline int ksm(int a, int b)
{
    int ans = 1;
    for (; b; b/=2, a = a * a % mod)
        if (b & 1)
            ans = ans * a % mod;
    return ans;
}

int du[200000];
vector<int> G[200000];
int n,k;
int ru[200000];
int lev[200000];
int vis[200000];
queue<int> q,q2;
signed main()
{
    io;
    int mx=0;
    cin>>n>>k;
    rep(i,1,n-1){
        int l,r;cin>>l>>r;
        G[l].pb(r);
        G[r].pb(l);
        du[l]++; du[r]++;
    }
    int fl=1;
    rep(i,1,n) if(du[i]==1)q.push(i),vis[i]=1;
    int cnt=0;
    int c=0;
    map<int,int> in;
    while(!q.empty()){
        cnt++;
        int o=0;
        while(!q.empty()){
        int t=q.front();q.pop();
        int kk=0;
         for(auto &i:G[t]){
             if(!vis[i]){
                 kk = 1;
             }
         }
         du[t]+=kk;
        c++;
        o++;
        if(du[t] != 1 && ru[t] < 3){
            fl=0;break;
        }
        for(auto &i:G[t]){
                if(!in[i]){
                    ru[i]++;
                    if(!vis[i]){
                       q2.push(i);
                        vis[i]=1;
                    }
                }
            }
        }
        if(q2.empty()){
            fl &= (o==1);
        }
       // in.clear();
        while(!q2.empty()){
            int t=q2.front();q2.pop();
            q.push(t);
            in[t]=1;
        }

        }
   // cout<<fl <<" "<<cnt<<endl;
    if(fl && cnt == k+1 && c == n){
        cout<<"Yes"<<endl;
    }else cout<<"No"<<endl;

//
    getchar(); //cin>>n
    getchar();
}
/*

3
3
RRLLL

3
2
RRLL

*/


#include "bits/stdc++.h"
using namespace std;
#define all(v) (v).begin(), (v).end()
#define io ios::sync_with_stdio(0)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rson rt << 1 | 1, mid + 1, r
#define lson rt << 1, l, mid
#define lll __int128
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eps 1e-12
//#define int long long
const int mod =  998244353;
inline int ksm(int a, int b)
{
    int ans = 1;
    for (; b; b/=2, a = a * a % mod)
        if (b & 1)
            ans = ans * a % mod;
    return ans;
}

int du[200000];
vector<int> G[200000];
int n,k;
int ru[200000];
int lev[200000];
int vis[200000];
queue<int> q,q2;
signed main()
{
    io;
    int mx=0;
    cin>>n>>k;
    rep(i,1,n-1){
        int l,r;cin>>l>>r;
        G[l].pb(r);
        G[r].pb(l);
        du[l]++; du[r]++;
    }
    int fl=1;
    map<int,int> in;
    rep(i,1,n) if(du[i]==1)q.push(i),vis[i]=1,in[i]=1;
    int cnt=0;
    int c=0;
    while(!q.empty()){
        cnt++;
        int o=0;
        while(!q.empty()){
        int t=q.front();q.pop();
        int kk=0;
         for(auto &i:G[t]){
             if(!vis[i]){
                 kk = 1;
             }
         }
        // ru[t]+=kk;
        c++;
        o++;
        if(du[t] != 1 && ru[t] < 3){
            fl=0;break;
        }
        for(auto &i:G[t]){
                if(!in[i]){
                    ru[i]++;
                    if(!vis[i]){
                       q2.push(i);
                        vis[i]=1;
                    }
                }
            }
        }
        if(q2.empty()){
            fl &= (o==1);
        }
       // in.clear();
        while(!q2.empty()){
            int t=q2.front();q2.pop();
            q.push(t);
            in[t]=1;
        }

        }
   // cout<<fl <<" "<<cnt<<endl;
    if(fl && cnt == k+1 && c == n){
        cout<<"Yes"<<endl;
    }else cout<<"No"<<endl;

//
    getchar(); //cin>>n
    getchar();
}
/*

3
3
RRLLL

3
2
RRLL

*/


#include "bits/stdc++.h"
using namespace std;
#define all(v) (v).begin(), (v).end()
#define io ios::sync_with_stdio(0)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rson rt << 1 | 1, mid + 1, r
#define lson rt << 1, l, mid
#define lll __int128
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eps 1e-12
//#define int long long
const int mod =  998244353;
inline int ksm(int a, int b)
{
    int ans = 1;
    for (; b; b/=2, a = a * a % mod)
        if (b & 1)
            ans = ans * a % mod;
    return ans;
}

int du[200000];
vector<int> G[200000];
int n,k;
int ru[200000];
int lev[200000];
int vis[200000];
queue<int> q,q2;
signed main()
{
    io;
    int mx=0;
    cin>>n>>k;
    rep(i,1,n-1){
        int l,r;cin>>l>>r;
        G[l].pb(r);
        G[r].pb(l);
        du[l]++; du[r]++;
    }
    int fl=1;
    map<int,int> in;
    rep(i,1,n) if(du[i]==1)q.push(i),vis[i]=1,in[i]=1,lev[i]=1;
    int cnt=0;
    int c=0;
    while(!q.empty()){
        cnt++;
        int o=0;
        while(!q.empty()){
        int t=q.front();q.pop();
        // ru[t]+=kk;
        int kk=-1;
        for (auto &i : G[t]){
            if(vis[i]){
                if(k==-1){
                    lev[t] = lev[i] - 1;

                }
                else {
                    if(lev[t] != lev[i] -1)fl=0;
                }
            }
        }
            o++;
        if(du[t] != 1 && ru[t] < 3){
            fl=0;break;
        }
        for(auto &i:G[t]){
                if(!in[i]){
                    ru[i]++;
                    if(!vis[i]){

                       q2.push(i);
                        vis[i]=1;
                    }
                }
            }
        }
        if(q2.empty()){
            fl &= (o==1);
        }
        if(c==1 && !q2.empty()){
            fl =0;
        }
       // in.clear();
        while(!q2.empty()){
            int t=q2.front();q2.pop();
            q.push(t);
            in[t]=1;
        }

        }
   // cout<<fl <<" "<<cnt<<endl;
    if(fl && cnt == k+1 && c == n){
        cout<<"Yes"<<endl;
    }else cout<<"No"<<endl;

//
    getchar(); //cin>>n
    getchar();
}
/*

3
3
RRLLL

3
2
RRLL

*/