/*input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U

6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
*/

/*
ID: aayushg1
LANG: C++
PROG: 310C
*/

#include <bits/stdc++.h>  //includes everything, supported in CF, USACO

#define DEBUG
#define f first
#define s second
#define pb push_back
#define ll long long
#define mp make_pair
#define sp << ' ' <<
#define inf 2000000000
#define maxn 100010
#define bitSize 262144*2
#define bitSize2 bitSize*2
using namespace std;

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vii;

ll tree[bitSize2];
ll lazy[bitSize2];   //represents the value that each element is being decreased by
ll values[bitSize];
ll n, q;

map<pii, int> treeMap;  //large index -> small index
map<pii, int> lazyMap;
map<int, int> seen;

int c = 0;
ll getTree(ll dir, ll node){
    pii obj = mp(dir, node);
    if(treeMap.count(obj)==0){
        treeMap[obj] = c++;
    }
    return tree[treeMap[obj]];
}

ll getLazy(ll dir, ll node){
    pii obj = mp(dir, node);
    if(lazyMap.count(obj)==0){
        lazyMap[obj] = c++;
    }
    return lazy[lazyMap[obj]];
}

ll setTree(ll dir, ll node, ll val){
    pii obj = mp(dir, node);
    tree[treeMap[obj]] = val;
}

ll setLazy(ll dir, ll node, ll val){
    pii obj = mp(dir, node);
    lazy[lazyMap[obj]] = val;
}

void lazyShiftMax(ll node, ll start, ll finish, ll dir){
    ll mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    setTree(dir, node, max(getTree(dir, node), getLazy(dir, node)));
    if(node < bitSize){
        setLazy(dir, node*2, max(getLazy(dir, node * 2), getLazy(dir, node)));
        setLazy(dir, node*2+1, max(getLazy(dir, node * 2 + 1), getLazy(dir, node)));
    }
    setLazy(dir, node, 0);
}

void changeValueMax(ll from, ll to, ll value, ll dir, ll node = 1, ll start = 1, ll finish = bitSize){          //range start, range end, node #, range of node start, range of node end
    ll mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    if(from<=start&&to>=finish){ //put lazy
        setLazy(dir, node, max(getLazy(dir, node), value));
    }
    lazyShiftMax(node, start, finish, dir);
    if((from<=start&&to>=finish) || to < start || from > finish)          //aded last 2 cases in case out of range
        return;
    // cout << from sp to sp value sp start sp finish << " change\n";
    changeValueMax(from, to, value, dir, node*2, start, mid);
    changeValueMax(from, to, value, dir, node*2+1, mid+1, finish);
    setTree(dir, node, max(getTree(dir, 2 * node), getTree(dir, 2 * node + 1)));                       //b/c of lazy shift
}

ll findMax(ll from, ll to, ll dir, ll node = 1, ll start = 1, ll finish = bitSize){          //range start, range end, node #, range of node start, range of node end
    lazyShiftMax(node, start, finish, dir);
    if(from > finish || to < start)return 0;
    ll mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    if(from<=start&&to>=finish)
        return getTree(dir, node);
    // cout << from sp to sp dir sp node sp getTree(dir, node) sp endl;
    return max(findMax(from, to, dir, node*2, start, mid), findMax(from, to, dir, node*2+1, mid+1, finish));
}

ll initNodeMax(ll node = 1){        //when adapting, make sure to ensure initial values are inf/0
    // fill(tree[0], tree[0] + bitSize2, 0);    //map, defailt return above is just 0.
    // fill(tree[1], tree[1] + bitSize2, 0);
    // fill(lazy[0], lazy[0] + bitSize2, 0);
    // fill(lazy[1], lazy[1] + bitSize2, 0);
}

// ll out(){
//     for(ll i = 0; i < n; ++i) cout << tree[0][bitSize+i] << " ";
//     cout << "\n";
//     for(ll i = 0; i < n; ++i) cout << tree[1][bitSize+i] << " ";
//     cout << "\n";
// }

void check(ll a, ll b, char dir){
    if(seen.count(a) > 0){
        cout << "0\n";
        return;
    }
    seen[a] = 1;
	ll c;
	if(dir=='L')c = 0;
	else c = 1;
	// cerr << a sp b sp c sp endl;
	if(c==0){								//CHECK THAT NOT PREVIOUSLY USED AS A L/U
		ll upto = findMax(b, b, 1-c);
		changeValueMax(upto, a, b, c);
		changeValueMax(a, a, b, c);
		cout << a-upto << endl;
	}
	else if(c==1){
		ll upto = findMax(a, a, 1-c);
		changeValueMax(upto, b, a, c);
		changeValueMax(b, b, a, c);
		cout << b-upto << endl;
	}
	return;
}

int main(){					  						//CHECK THAT NOT PREVIOUSLY USED AS A L/U
	// freopen ("haybales.in","r",stdin);
	// ios_base::sync_with_stdio(false);
	// ios::sync_with_stdio(false);
	// freopen ("haybales.out","w",stdout);
	cin >> n >> q;
	initNodeMax();
	for(ll i = 0; i < q; ++i){
		ll a, b;
		char c;
		cin >> a >> b >> c;
		check(a, b, c);
	}
}


/*input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U

9
1
10
6
0
2
*/

/*
ID: aayushg1
LANG: C++
PROG: 310C
*/

#include <bits/stdc++.h>  //includes everything, supported in CF, USACO

#define DEBUG
#define f first
#define s second
#define pb push_back
#define ll long long
#define mp make_pair
#define sp << ' ' <<
#define inf 1073741824
#define maxn 100010
#define bitSize2 16000500
using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;

int tree[bitSize2];   //make vectors to save space
int lazy[bitSize2];   //represents the value that each element is being decreased by
pii kids[bitSize2];
int n, q;

map<int, int> seen;
int avail = 2;

int getKids(int at, int which){
    if(kids[at].f == -1){
        kids[at] = mp(avail, avail + 1);
        avail += 2;
    }
    if(which%2 == 0)return kids[at].f;
    else return kids[at].s;
}

int getTree(int node){
    return tree[node];
}

int getLazy(int node){
    return lazy[node];
}

int setTree(int node, int val){
    tree[node] = val;
}

int setLazy(int node, int val){
    lazy[node] = val;
}

void lazyShiftMax(int node, int start, int finish){
    int mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    setTree(node, max(getTree(node), getLazy(node)));
    if(start != finish){    //not a bottommost node
        setLazy(getKids(node, 0), max(getLazy(getKids(node, 0)), getLazy(node)));
        setLazy(getKids(node, 1), max(getLazy(getKids(node, 1)), getLazy(node)));
    }
    setLazy(node, 0);
}

void changeValueMax(int from, int to, int value, int node, int start = 1, int finish = inf){          //range start, range end, node #, range of node start, range of node end
    int mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    if(from<=start&&to>=finish){ //put lazy
        setLazy(node, max(getLazy(node), value));
    }
    // cout << from sp to sp value sp start sp finish << " change\n";
    lazyShiftMax(node, start, finish);
    if((from<=start&&to>=finish) || to < start || from > finish)          //aded last 2 cases in case out of range
        return;
    changeValueMax(from, to, value, getKids(node, 0), start, mid);
    changeValueMax(from, to, value, getKids(node, 1), mid+1, finish);
    setTree(node, max(getTree(getKids(node, 0)), getTree(getKids(node, 1))));                       //b/c of lazy shift
}

int findMax(int from, int to, int node, int start = 1, int finish = inf){          //range start, range end, node #, range of node start, range of node end
    lazyShiftMax(node, start, finish);
    // cout << from sp to sp dir sp node sp getTree(node) sp endl;
    if(from > finish || to < start)return 0;
    int mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    if(from<=start&&to>=finish)
        return getTree(node);
    return max(findMax(from, to, getKids(node, 0), start, mid), findMax(from, to, getKids(node, 1), mid+1, finish));
}

int initNodeMax(){        //when adapting, make sure to ensure initial values are inf/0
    // fiint(tree[0], tree[0] + bitSize2, 0);    //map, defailt return above is just 0.
    // fiint(tree[1], tree[1] + bitSize2, 0);
    // fiint(lazy[0], lazy[0] + bitSize2, 0);
    // fiint(lazy[1], lazy[1] + bitSize2, 0);
    fill(kids, kids + bitSize2, mp(-1, -1));
}

void check(int a, int b, char dir){
    // if(seen.count(a) > 0){
    //     cout << "0\n";
    //     return;
    // }
    // seen[a] = 1;
	int c;
	if(dir=='L')c = 0;
	else c = 1;
	// cerr << a sp b sp c sp endl;
	if(c==0){								//CHECK THAT NOT PREVIOUSLY USED AS A L/U
		int upto = findMax(b, b, 1-c);
		changeValueMax(upto, a, b, c);
        // changeValueMax(a, a, b, c);
        changeValueMax(b, b, a, 1-c);
		cout << a-upto << endl;
	}
	else if(c==1){
		int upto = findMax(a, a, 1-c);
		changeValueMax(upto, b, a, c);
        // changeValueMax(b, b, a, c);
        changeValueMax(a, a, b, 1-c);
		cout << b-upto << endl;
	}
	return;
}

int main(){					//more correctish, but TLE since (n log^2 n), not (n log n) on max case.
	// freopen ("haybales.in","r",stdin);
	ios_base::sync_with_stdio(false);
	ios::sync_with_stdio(false);
	// freopen ("haybales.out","w",stdout);
	cin >> n >> q;
	initNodeMax();
	for(int i = 0; i < q; ++i){
		int a, b;
		char c;
		cin >> a >> b >> c;
		check(a, b, c);
        // if(avail > bitSize2){
        //     cerr << "";
        //     cout << i;
        //     cout << "BAD\n";
        //     cerr << "";
        //     return 0;
        // }
	}
}

//wrong answer 163918th numbers differ - expected: '3292', found: '-973001281'


/*input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U

9
1
10
6
0
2
*/

/*
ID: aayushg1
LANG: C++
PROG: 310C
*/

#include <bits/stdc++.h>  //includes everything, supported in CF, USACO

#define DEBUG
#define f first
#define s second
#define pb push_back
#define ll long long
#define mp make_pair
#define sp << ' ' <<
#define inf 1073741824
#define maxn 200010
#define bitSize2 16005000
using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;

int tree[bitSize2];   //make vectors to save space
int lazy[bitSize2];   //represents the value that each element is being decreased by
pii kids[bitSize2];
int n, q;

map<int, int> seen;
int avail = 2;

int getKids(int at, int which){
    if(kids[at].f == -1){
        kids[at] = mp(avail, avail + 1);
        avail += 2;
    }
    if(which%2 == 0)return kids[at].f;
    else return kids[at].s;
}

int getTree(int node){
    return tree[node];
}

int getLazy(int node){
    return lazy[node];
}

int setTree(int node, int val){
    tree[node] = val;
}

int setLazy(int node, int val){
    lazy[node] = val;
}

void lazyShiftMax(int node, int start, int finish){
    int mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    setTree(node, max(getTree(node), getLazy(node)));
    if(start != finish){    //not a bottommost node
        setLazy(getKids(node, 0), max(getLazy(getKids(node, 0)), getLazy(node)));
        setLazy(getKids(node, 1), max(getLazy(getKids(node, 1)), getLazy(node)));
    }
    setLazy(node, 0);
}

void changeValueMax(int from, int to, int value, int node, int start = 1, int finish = inf){          //range start, range end, node #, range of node start, range of node end
    int mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    if(from<=start&&to>=finish){ //put lazy
        setLazy(node, max(getLazy(node), value));
    }
    // cout << from sp to sp value sp start sp finish << " change\n";
    lazyShiftMax(node, start, finish);
    if((from<=start&&to>=finish) || to < start || from > finish)          //aded last 2 cases in case out of range
        return;
    changeValueMax(from, to, value, getKids(node, 0), start, mid);
    changeValueMax(from, to, value, getKids(node, 1), mid+1, finish);
    setTree(node, max(getTree(getKids(node, 0)), getTree(getKids(node, 1))));                       //b/c of lazy shift
}

int findMax(int from, int to, int node, int start = 1, int finish = inf){          //range start, range end, node #, range of node start, range of node end
    lazyShiftMax(node, start, finish);
    // cout << from sp to sp dir sp node sp getTree(node) sp endl;
    if(from > finish || to < start)return 0;
    int mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    if(from<=start&&to>=finish)
        return getTree(node);
    return max(findMax(from, to, getKids(node, 0), start, mid), findMax(from, to, getKids(node, 1), mid+1, finish));
}

int initNodeMax(){        //when adapting, make sure to ensure initial values are inf/0
    // fiint(tree[0], tree[0] + bitSize2, 0);    //map, defailt return above is just 0.
    // fiint(tree[1], tree[1] + bitSize2, 0);
    // fiint(lazy[0], lazy[0] + bitSize2, 0);
    // fiint(lazy[1], lazy[1] + bitSize2, 0);
    fill(kids, kids + bitSize2, mp(-1, -1));
}

void check(int a, int b, char dir){
    if(seen.count(a) > 0){
        cout << "0\n";
        return;
    }
    seen[a] = 1;
	int c;
	if(dir=='L')c = 0;
	else c = 1;
	// cerr << a sp b sp c sp endl;
	if(c==0){								//CHECK THAT NOT PREVIOUSLY USED AS A L/U
		int upto = findMax(b, b, 1-c);
		changeValueMax(upto, a, b, c);
        // changeValueMax(a, a, b, c); useless
        // changeValueMax(b, b, a, 1-c);
		cout << a-upto << endl;
	}
	else if(c==1){
		int upto = findMax(a, a, 1-c);
		changeValueMax(upto, b, a, c);
        // changeValueMax(b, b, a, c); useless
        // changeValueMax(a, a, b, 1-c);
		cout << b-upto << endl;
	}
	return;
}

int main(){					//more correctish, but TLE since (n log^2 n), not (n log n) on max case.
	// freopen ("haybales.in","r",stdin);
	ios_base::sync_with_stdio(false);
	ios::sync_with_stdio(false);
	// freopen ("haybales.out","w",stdout);
	cin >> n >> q;
	initNodeMax();
	for(int i = 0; i < q; ++i){
		int a, b;
		char c;
		cin >> a >> b >> c;
		check(a, b, c);
        if(avail > bitSize2){
            cerr << "";
            cout << i;
            cout << "BAD\n";
            cerr << "";
            return 0;
        }
	}
}

//wrong answer 163918th numbers differ - expected: '3292', found: '-973001281'


/*input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U

9
1
10
6
0
2
*/

/*
ID: aayushg1
LANG: C++
PROG: 310C
*/

#include <bits/stdc++.h>  //includes everything, supported in CF, USACO

#define DEBUG
#define f first
#define s second
#define pb push_back
#define ll long long
#define mp make_pair
#define sp << ' ' <<
#define inf 1073741824
#define maxn 100010
#define bitSize2 15000000
using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;

int tree[bitSize2];   //make vectors to save space
int lazy[bitSize2];   //represents the value that each element is being decreased by
pii kids[bitSize2];
int n, q;

map<int, int> seen;
int avail = 2;

int getKids(int at, int which){
    if(kids[at].f == -1){
        kids[at] = mp(avail, avail + 1);
        avail += 2;
    }
    if(which%2 == 0)return kids[at].f;
    else return kids[at].s;
}

int getTree(int node){
    return tree[node];
}

int getLazy(int node){
    return lazy[node];
}

int setTree(int node, int val){
    tree[node] = val;
}

int setLazy(int node, int val){
    lazy[node] = val;
}

void lazyShiftMax(int node, int start, int finish){
    int mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    setTree(node, max(getTree(node), getLazy(node)));
    if(start != finish){    //not a bottommost node
        setLazy(getKids(node, 0), max(getLazy(getKids(node, 0)), getLazy(node)));
        setLazy(getKids(node, 1), max(getLazy(getKids(node, 1)), getLazy(node)));
    }
    setLazy(node, 0);
}

void changeValueMax(int from, int to, int value, int node, int start = 1, int finish = inf){          //range start, range end, node #, range of node start, range of node end
    int mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    if(from<=start&&to>=finish){ //put lazy
        setLazy(node, max(getLazy(node), value));
    }
    // cout << from sp to sp value sp start sp finish << " change\n";
    lazyShiftMax(node, start, finish);
    if((from<=start&&to>=finish) || to < start || from > finish)          //aded last 2 cases in case out of range
        return;
    changeValueMax(from, to, value, getKids(node, 0), start, mid);
    changeValueMax(from, to, value, getKids(node, 1), mid+1, finish);
    setTree(node, max(getTree(getKids(node, 0)), getTree(getKids(node, 1))));                       //b/c of lazy shift
}

int findMax(int from, int to, int node, int start = 1, int finish = inf){          //range start, range end, node #, range of node start, range of node end
    lazyShiftMax(node, start, finish);
    // cout << from sp to sp dir sp node sp getTree(node) sp endl;
    if(from > finish || to < start)return 0;
    int mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    if(from<=start&&to>=finish)
        return getTree(node);
    return max(findMax(from, to, getKids(node, 0), start, mid), findMax(from, to, getKids(node, 1), mid+1, finish));
}

int initNodeMax(){        //when adapting, make sure to ensure initial values are inf/0
    // fiint(tree[0], tree[0] + bitSize2, 0);    //map, defailt return above is just 0.
    // fiint(tree[1], tree[1] + bitSize2, 0);
    // fiint(lazy[0], lazy[0] + bitSize2, 0);
    // fiint(lazy[1], lazy[1] + bitSize2, 0);
    fill(kids, kids + bitSize2, mp(-1, -1));
}

void check(int a, int b, char dir){
    // if(seen.count(a) > 0){
    //     cout << "0\n";
    //     return;
    // }
    // seen[a] = 1;
	int c;
	if(dir=='L')c = 0;
	else c = 1;
	// cerr << a sp b sp c sp endl;
	if(c==0){								//CHECK THAT NOT PREVIOUSLY USED AS A L/U
		int upto = findMax(b, b, 1-c);
		changeValueMax(upto, a, b, c);
        // changeValueMax(a, a, b, c);
        changeValueMax(b, b, a, 1-c);
		cout << a-upto << endl;
	}
	else if(c==1){
		int upto = findMax(a, a, 1-c);
		changeValueMax(upto, b, a, c);
        // changeValueMax(b, b, a, c);
        changeValueMax(a, a, b, 1-c);
		cout << b-upto << endl;
	}
	return;
}

int main(){					//more correctish, but TLE since (n log^2 n), not (n log n) on max case.
	// freopen ("haybales.in","r",stdin);
	ios_base::sync_with_stdio(false);
	ios::sync_with_stdio(false);
	// freopen ("haybales.out","w",stdout);
	cin >> n >> q;
	initNodeMax();
	for(int i = 0; i < q; ++i){
		int a, b;
		char c;
		cin >> a >> b >> c;
		check(a, b, c);
        if(avail > bitSize2){
            cerr << "";
            cout << i;
            cout << "BAD\n";
            cerr << "";
            return 0;
        }
	}
}


/*input
1000000000 2
500000000 500000001 U
750000000 250000001 L

10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U

6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
*/

/*
ID: aayushg1
LANG: C++
PROG: 310C
*/

#include <bits/stdc++.h>  //includes everything, supported in CF, USACO

#define DEBUG
#define f first
#define s second
#define pb push_back
#define ll long long
#define mp make_pair
#define sp << ' ' <<
#define inf (ll)2147483648
#define maxn 100010
#define bitSize 4194304
#define bitSize2 bitSize*2
using namespace std;

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vii;

ll tree[2][bitSize2];
ll lazy[2][bitSize2];   //represents the value that each element is being decreased by
pii kids[bitSize2];
ll n, q;

// map<pii, ll> treeMap;  //large index -> small index
// map<pii, ll> lazyMap;
map<ll, ll> seen;

ll c = 2;

ll getKids(ll at, ll which){
    if(kids[at].f == -1){
        kids[at] = mp(c, c + 1);
        c += 2;
    }
    if(which%2 == 0)return kids[at].f;
    else return kids[at].s;
}

ll getTree(ll dir, ll node){
    return tree[dir][node];
}

ll getLazy(ll dir, ll node){
    return lazy[dir][node];
}

ll setTree(ll dir, ll node, ll val){
    tree[dir][node] = val;
}

ll setLazy(ll dir, ll node, ll val){
    lazy[dir][node] = val;
}

void lazyShiftMax(ll node, ll start, ll finish, ll dir){
    ll mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    setTree(dir, node, max(getTree(dir, node), getLazy(dir, node)));
    if(node < bitSize){
        setLazy(dir, getKids(node, 0), max(getLazy(dir, getKids(node, 0)), getLazy(dir, node)));
        setLazy(dir, getKids(node, 1), max(getLazy(dir, getKids(node, 1)), getLazy(dir, node)));
    }
    setLazy(dir, node, 0);
}

void changeValueMax(ll from, ll to, ll value, ll dir, ll node = 1, ll start = 1, ll finish = inf){          //range start, range end, node #, range of node start, range of node end
    ll mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    if(from<=start&&to>=finish){ //put lazy
        setLazy(dir, node, max(getLazy(dir, node), value));
    }
    // cout << from sp to sp value sp start sp finish << " change\n";
    lazyShiftMax(node, start, finish, dir);
    if((from<=start&&to>=finish) || to < start || from > finish)          //aded last 2 cases in case out of range
        return;
    changeValueMax(from, to, value, dir, getKids(node, 0), start, mid);
    changeValueMax(from, to, value, dir, getKids(node, 1), mid+1, finish);
    setTree(dir, node, max(getTree(dir, getKids(node, 0)), getTree(dir, getKids(node, 1))));                       //b/c of lazy shift
}

ll findMax(ll from, ll to, ll dir, ll node = 1, ll start = 1, ll finish = inf){          //range start, range end, node #, range of node start, range of node end
    lazyShiftMax(node, start, finish, dir);
    // cout << from sp to sp dir sp node sp getTree(dir, node) sp endl;
    if(from > finish || to < start)return 0;
    ll mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    if(from<=start&&to>=finish)
        return getTree(dir, node);
    return max(findMax(from, to, dir, getKids(node, 0), start, mid), findMax(from, to, dir, getKids(node, 1), mid+1, finish));
}

ll initNodeMax(ll node = 1){        //when adapting, make sure to ensure initial values are inf/0
    // fill(tree[0], tree[0] + bitSize2, 0);    //map, defailt return above is just 0.
    // fill(tree[1], tree[1] + bitSize2, 0);
    // fill(lazy[0], lazy[0] + bitSize2, 0);
    // fill(lazy[1], lazy[1] + bitSize2, 0);
    fill(kids, kids + bitSize2, mp(-1, -1));
}

void check(ll a, ll b, char dir){
    if(seen.count(a) > 0){
        cout << "0\n";
        return;
    }
    seen[a] = 1;
	ll c;
	if(dir=='L')c = 0;
	else c = 1;
	// cerr << a sp b sp c sp endl;
	if(c==0){								//CHECK THAT NOT PREVIOUSLY USED AS A L/U
		ll upto = findMax(b, b, 1-c);
		changeValueMax(upto, a, b, c);
		cout << a-upto << endl;
	}
	else if(c==1){
		ll upto = findMax(a, a, 1-c);
		changeValueMax(upto, b, a, c);
		cout << b-upto << endl;
	}
	return;
}

int main(){					//more correctish, but TLE since (n log^2 n), not (n log n) on max case.
	// freopen ("haybales.in","r",stdin);
	// ios_base::sync_with_stdio(false);
	// ios::sync_with_stdio(false);
	// freopen ("haybales.out","w",stdout);
	cin >> n >> q;
	initNodeMax();
	for(ll i = 0; i < q; ++i){
		ll a, b;
		char c;
		cin >> a >> b >> c;
		check(a, b, c);
	}
}


/*input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U

9
1
10
6
0
2
*/

/*
ID: aayushg1
LANG: C++
PROG: 310C
*/

#include <bits/stdc++.h>  //includes everything, supported in CF, USACO

#define DEBUG
#define f first
#define s second
#define pb push_back
#define ll long long
#define mp make_pair
#define sp << ' ' <<
#define inf 1073741824
#define maxn 200010
#define bitSize2 15800000
using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;

int tree[bitSize2];   //make vectors to save space
int lazy[bitSize2];   //represents the value that each element is being decreased by
int kids[bitSize2][2];
int n, q;

map<int, int> seen;
int avail = 2;

int getKids(int at, int which){
    if(kids[at][which] == -1){
        kids[at][which] = avail;
        ++avail;
    }
    return kids[at][which];
}

int getTree(int node){return tree[node];}   //legacy methods
int getLazy(int node){return lazy[node];}
int setTree(int node, int val){tree[node] = val;}
int setLazy(int node, int val){lazy[node] = val;}

void lazyShiftMax(int node, int start, int finish){
    int mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    setTree(node, max(getTree(node), getLazy(node)));
    if(start != finish && getLazy(node) > 0){    //not a bottommost node
        setLazy(getKids(node, 0), max(getLazy(getKids(node, 0)), getLazy(node)));
        setLazy(getKids(node, 1), max(getLazy(getKids(node, 1)), getLazy(node)));
    }
    setLazy(node, 0);
}

void changeValueMax(int from, int to, int value, int node, int start = 1, int finish = inf){          //range start, range end, node #, range of node start, range of node end
    int mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    if(from<=start&&to>=finish){ //put lazy
        setLazy(node, max(getLazy(node), value));
        return;
    }
    // cout << from sp to sp value sp start sp finish << " change\n";
    if(to < start || from > finish)          //aded last 2 cases in case out of range
        return;
    lazyShiftMax(node, start, finish);
    changeValueMax(from, to, value, getKids(node, 0), start, mid);
    changeValueMax(from, to, value, getKids(node, 1), mid+1, finish);
    setTree(node, max(getTree(getKids(node, 0)), getTree(getKids(node, 1))));                       //b/c of lazy shift
}

int findMax(int from, int to, int node, int start = 1, int finish = inf){          //range start, range end, node #, range of node start, range of node end
    // cout << from sp to sp dir sp node sp getTree(node) sp endl;
    if(from > finish || to < start)return 0;
    lazyShiftMax(node, start, finish);
    int mid = (start + finish ) / 2;            //takes floor, results in the endof the first range and one less than the start of the second range
    if(from<=start&&to>=finish)
        return getTree(node);
    return max(findMax(from, to, getKids(node, 0), start, mid), findMax(from, to, getKids(node, 1), mid+1, finish));
}

int initNodeMax(){        //when adapting, make sure to ensure initial values are inf/0
    // fiint(tree[0], tree[0] + bitSize2, 0);    //map, defailt return above is just 0.
    // fiint(tree[1], tree[1] + bitSize2, 0);
    // fiint(lazy[0], lazy[0] + bitSize2, 0);
    // fiint(lazy[1], lazy[1] + bitSize2, 0);
    fill(kids[0], kids[0] + bitSize2, -1);
    fill(kids[1], kids[1] + bitSize2, -1);
}

void check(int a, int b, char dir){
    if(seen.count(a) > 0){
        cout << "0\n";
        return;
    }
    seen[a] = 1;
	int c;
	if(dir=='L')c = 0;
	else c = 1;
	// cerr << a sp b sp c sp endl;
	if(c==0){								//CHECK THAT NOT PREVIOUSLY USED AS A L/U
		int upto = findMax(b, b, 1-c);
		changeValueMax(upto, a, b, c);
        // changeValueMax(a, a, b, c); useless
        // changeValueMax(b, b, a, 1-c);    //do this, or seen map
		cout << a-upto << endl;
	}
	else if(c==1){
		int upto = findMax(a, a, 1-c);
		changeValueMax(upto, b, a, c);
        // changeValueMax(b, b, a, c); useless
        // changeValueMax(a, a, b, 1-c);
		cout << b-upto << endl;
	}
	return;
}

int main(){					//more correctish, but RTE, O(n log n), case 28 runs out of memory on 16M nodes.
	// freopen ("segtree.in","r",stdin);
	ios_base::sync_with_stdio(false);
	ios::sync_with_stdio(false);
	// freopen ("segtree.out","w",stdout);
	cin >> n >> q;
	initNodeMax();
	for(int i = 0; i < q; ++i){
		int a, b;
		char c;
		cin >> a >> b >> c;
		check(a, b, c);
        if(avail >= bitSize2){
            cerr << "";
            cout << i;
            cout << "BAD\n";
            cerr << "";
            return 0;
        }
	}
}

//wrong answer 163918th numbers differ - expected: '3292', found: '-973001281'
//wrong output format Expected integer, but "176516BAD" found
//fixed - should only assign one kid array index at a time, not 2, and shift after return
//wrong answer 164178th numbers differ - expected: '16861', found: '-488893609'
//wrong answer 164120th numbers differ - expected: '503', found: '-521253971'


#include <iostream>
#include <vector>
using namespace std;
int n,k;
vector<vector<int>> g(2e5 + 1);
vector<long long> svok(2e5 + 1);
vector<long long> kol(2e5 + 1, 1);
vector<long long> obs(2e5 + 1);
vector<vector<long long>> vh(2e5 + 1, vector<long long>(5));
vector<vector<long long>> kLen(2e5 + 1, vector<long long>(5));
void dfs(int v, int par) {
	long long sumpr = 0;
	long long sumk = 0;
	for (int i = 0; i < g[v].size(); i++)
	{
		if (g[v][i] == par)
			continue;
		dfs(g[v][i], v);
		kol[v] += kol[g[v][i]];
		svok[v] += svok[g[v][i]] + kol[g[v][i]];
		obs[v] += obs[g[v][i]];
		sumpr += kol[g[v][i]] * svok[g[v][i]];
		sumk += kol[g[v][i]] * kol[g[v][i]];
	}
	obs[v] += (kol[v] - 1) * (svok[v] - kol[v] + 1) - sumpr + ((kol[v] - 1) * (kol[v] - 1) - sumk) + svok[v];
}
void dfs2(int v, int par) {
	vh[v][0] = 1;
	vector<long long> kk(k);
	for (int i = 0; i < g[v].size(); i++)
	{
		if (g[v][i] == par)
			continue;
		dfs2(g[v][i], v);
		for (int j = 0; j < k; j++)
		{
			kk[j] += vh[g[v][i]][j];;
			vh[v][((j - 1) + k) % k] += vh[g[v][i]][j];

		}
	}
	for (int i = 0; i < g[v].size(); i++)
	{
		if (g[v][i] == par)
			continue;
		for (int j = 0; j < k; j++)
		{
			for (int l = 0; l < k; l++)
			{
				kLen[v][((l + j - 2)%k+k)%k] += (kk[l] - vh[g[v][i]][l]) * vh[g[v][i]][j];
			}
		}
	}
	for (int i = 0; i < k; i++)
	{
		kLen[v][i] /= 2;
		kLen[v][i] += vh[v][i];
	}
	for (int i = 0; i < g[v].size(); i++)
	{
		if (g[v][i] == par)
			continue;
		for (int j = 0; j < k; j++)
		{
			kLen[v][j] += kLen[g[v][i]][j];
		}
	}
}
int main()
{
	cin >> n>>k;
	for (int i = 0; i < n - 1; i++)
	{
		int a, b;
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	dfs(1, -1);
	dfs2(1, -1);
	long long ans = obs[1];
	for (int i = 0; i < k; i++)
	{
		ans += kLen[1][i] * i;
	}
	cout << ans/k;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long lli;
typedef pair<lli,lli> ii;
typedef vector<lli> vi;
typedef vector<vector<lli> > vvi;
typedef vector<ii> vii;

#define EB emplace_back
#define PB push_back
#define MP make_pair
#define FF first
#define SS second

const lli mod = 1e9 +7;
lli n, a, b, ans, k, temp[6];
vector<lli> elist[200001];

struct node{
	lli val, dist[6];
	node(){
		val=0;
		for(int i=0; i<=k; i++)dist[i]=0;
	}
}dp[200001];

void dfs1(lli cur, lli par)
{
	dp[cur].dist[0]=1;
	lli child;
	for(lli i=0; i<elist[cur].size(); i++)
	{
		child=elist[cur][i];
		if(child!=par)
		{
			dfs1(child, cur);
			dp[cur].val += dp[child].val + dp[child].dist[0];
			for(int i=0; i<=k; i++)
				dp[cur].dist[(i+1)%k] += dp[child].dist[i];
		}
	}
}

void dfs2(lli cur, lli par)
{
	dp[cur].val = (dp[par].val - dp[cur].dist[0]) + dp[par].dist[0] - dp[cur].dist[k-1];
	for(int i=k; i>-1; i--)
		temp[i] = dp[cur].dist[i];
	for(int i=k; i>-1; i--)
		dp[cur].dist[i] += dp[par].dist[(i-1+k)%k] - temp[(i-2+k)%k];
	lli child;
	for(lli i=0; i<elist[cur].size(); i++)
	{
		child=elist[cur][i];
		if(child!=par)
			dfs2(child, cur);
	}
}

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin >> n >> k;
	for(lli i=1; i<n; i++)
	{
		cin >> a >> b;
		elist[a].PB(b);
		elist[b].PB(a);
	}
	dfs1(1, 0);
	for(int i=0; i<elist[1].size(); i++)
		dfs2(elist[1][i], 1);
	for(lli i=1; i<=n; i++)
		ans+=dp[i].val;
	cout << ans/2;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N=200005;
vector<int> v[N];
vector<int> depth(N,0);
int k;
ll res=0;

int f(int a)
{
    if(a>=0) return (a+k-1)/k;
    else return (a/k);
}

vector<array<ll,2>> dfs(int a,int p=0)
{
    depth[a]=depth[p]+1;
    vector<array<ll,2>> x(k,{0,0});
    x[depth[a]%k]={depth[a]/k,1};
    for(int to:v[a])
    {
        if(to==p) continue;
        vector<array<ll,2>> y=dfs(to,a);
        for(int i=0;i<k;i++)
        {
            for(int j=0;j<k;j++)
            {
                ll now=(x[i][0]*y[j][1]+y[j][0]*x[i][1]-2*(depth[a]/k)*x[i][1]*y[j][1]);
                int d=i+j-2*(depth[a]%k);
                now+=(f(d)*x[i][1]*y[j][1]);
                res+=now;
                //cout << "adding " << now << "(" << a << ")" << endl;
            }
        }
        for(int j=0;j<k;j++)
        {
            x[j][0]+=y[j][0];
            x[j][1]+=y[j][1];
        }
    }
    //cout << "out " << a << endl;
    //for(int i=0;i<k;i++) cout << i << ": sum=" << x[i][0] << " cnt=" << x[i][1] << endl;
    //cout << endl;
    return x;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n >> k;
    for(int i=0;i<n-1;i++)
    {
        int a,b;
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(1);
    cout << res << "\n";
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#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;
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define e1 first
#define e2 second
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define float long double
#define size(x) (int)x.size()
#define satori int testCases; cin>>testCases; while(testCases--)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define all(r) begin(r),end(r)
#define time chrono::high_resolution_clock().now().time_since_epoch().count()
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

const int MAXN=2e5+10;

vector<int> g[MAXN];
ll cnt[MAXN][5],len[MAXN][5],res[MAXN];
int k;

void dfs(int u,int p=-1){
	for(auto v:g[u])
		if(v!=p){
			dfs(v,u);
			res[u]+=res[v];
			for(int i=0;i<k;i++){
				for(int j=0;j<k;j++){
					if(i+j+1>k)
						res[u]+=cnt[u][i]*cnt[v][j];
					if(i+j+1>0)
						res[u]+=cnt[u][i]*cnt[v][j];
					res[u]+=len[u][i]*cnt[v][j]+len[v][j]*cnt[u][i];
					if(u==-2&&v==5){
						cout<<4<<": "<<i<<' '<<5<<": "<<j<<'\n';
						int sum=0;
						if(i+j+1>k)
							sum+=cnt[u][i]*cnt[v][j];
						if(i+j+1>0)
							sum+=cnt[u][i]*cnt[v][j],cout<<"XD";
						sum+=len[u][i]*cnt[v][j]+len[v][j]*cnt[u][i];
						cout<<sum<<'\n';
						cout<<len[u][i]<<'\n';
					}
				}
				res[u]+=len[v][i]+cnt[v][i];
			}
			for(int i=0;i<k-1;i++)
				cnt[u][i+1]+=cnt[v][i],len[u][i+1]+=len[v][i];
			cnt[u][0]+=cnt[v][k-1],len[u][0]+=len[v][k-1];
			len[u][0]+=cnt[v][k-1];
		}
	cnt[u][0]++;
}

int32_t main()
{
	fastio;
	int n,u,v;
	cin>>n>>k;
	for(int i=1;i<n;i++){
		cin>>u>>v;
		g[u].eb(v);
		g[v].eb(u);
	}
	dfs(1);
	/*for(int i=1;i<=n;i++)
		cout<<i<<' ';
	cout<<'\n';
	for(int i=1;i<=n;i++)
		cout<<res[i]<<' ';
	cout<<'\n';*/
	cout<<res[1]<<'\n';
}


// luogu-judger-enable-o2

#include<string>
#include<vector>
#include<regex>
#include<algorithm>
#include<random>
#include<map>
#include<set>
#include<deque>
#include<iomanip>
#include<sstream>
#include<stack>
#include<iostream>
#include<limits>
#include<bitset>
#include<list>
#include<queue>
#include<array>
#include<memory>
#include<functional>
#include<unordered_map>
#include<complex>
using namespace std;
namespace tree_split2
{
	const int N = 310000;
	int k;
	struct graph
	{
		int Begin[N * 2], to[N * 2], Next[N * 2], e, weight[N * 2];
		void add(int u, int v)
		{
			to[++e] = v;
			Next[e] = Begin[u];
			Begin[u] = e;
		}
		void add(int u, int v, int w)
		{
			to[++e] = v;
			Next[e] = Begin[u];
			Begin[u] = e;
			weight[e] = w;
		}
		void init_graph()
		{
			e = 0;
			memset(Begin, -1, sizeof(Begin));
		}
	};
	graph p;
	bool vis[N];
	int root, size1[N], size2[N], allnode;
	void getroot(int now, int fa)  //得到根
	{
		size1[now] = 1;
		size2[now] = 0;
		for (int i = p.Begin[now]; i != -1; i = p.Next[i])
		{
			int t = p.to[i];
			if (t == fa || vis[t]) continue;
			getroot(t, now);
			size1[now] += size1[t];
			size2[now] = max(size2[now], size1[t]);
		}
		size2[now] = max(allnode - size1[now], size2[now]);
		if (size2[now] < size2[root]) root = now;
	}
	long long ans;
	int deep[2 * N];
	unordered_map<int, int> sum;
	long long sum_0,sz;
	void getdeep(int r, int fa) //当前轮处理数据
	{
		if (r != root)
		{
			sum_0 += deep[r] / k;
			sum[deep[r] % k]++;
			sz++;
		}
		for (int i = p.Begin[r]; i != -1; i = p.Next[i])
		{
			if (p.to[i] != fa && vis[p.to[i]] == false)
			{
				deep[p.to[i]] = deep[r] + p.weight[i];
				getdeep(p.to[i], r);
			}
		}
	}

	void calc2(int a, int fa)//计算当前子树
	{
		ans += (deep[a] / k)*(sz+1);
		int kkk = deep[a] % k;
		if (kkk != 0)
			ans++;
		ans += sum_0;
		for (int i = 0; i < 5; ++i)
			if (kkk + i > 0 && kkk + i <= k)
				ans += sum[i];
			else if (kkk + i > k)
				ans += sum[i] * 2;
		//cout << a << " " << root << " " << ans << " " << sum_0 <<" "<<deep[a]/k<<" "<<sz<< endl;
		for (int i = p.Begin[a]; i != -1; i = p.Next[i])
		{
			if (p.to[i] != fa && vis[p.to[i]] == false)
				calc2(p.to[i], a);
		}
	}
	int sumnode;
	void del_(int a, int fa, int now) //清除当前子树贡献
	{
		sumnode++;
		sum_0 -= deep[a] / k;
		sum[deep[a] % k]--;
		sz--;
		for (int i = p.Begin[a]; i != -1; i = p.Next[i])
		{
			if (p.to[i] != fa && vis[p.to[i]] == false)
				del_(p.to[i], a, now);
		}
	}
	void ins_(int a, int fa)//回退当前子树贡献
	{
		sum_0 += deep[a] / k;
		sum[deep[a] % k]++;
		sz++;
		for (int i = p.Begin[a]; i != -1; i = p.Next[i])
		{
			if (p.to[i] != fa && vis[p.to[i]] == false)
				ins_(p.to[i], a);
		}
	}
	int all_[N * 2];
	void calc(int r)
	{
		getroot(r, r);
		vis[root] = true;
		deep[root] = 0;
		sum_0 = 0;
		sz = 0;
		sum.clear();
		getdeep(root, root);
		for (int i = p.Begin[root]; i != -1; i = p.Next[i])
		{
			int L = p.to[i];
			if (vis[L] == false)
			{
				del_(L, root, L);
				calc2(L, root);
				//ins_(L, root);
				all_[L] = size1[L];
			}
		}
		for (int i = p.Begin[root]; i != -1; i = p.Next[i])
			if (vis[p.to[i]] == false)
			{
				root = 0;
				allnode = all_[p.to[i]];
				calc(p.to[i]);
			}
	}
	void init(int n)
	{
		memset(vis, 0, sizeof(vis));
		memset(size1, 0, sizeof(size1));
		memset(size2, 0, sizeof(size2));
		root = 0;
		p.init_graph();
		allnode = n;
		size2[0] = n + 10;
	}
}
using namespace tree_split2;
int32_t main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n, u, v;
	cin >> n >> k;
	p.init_graph();
	init(n);
	ans = 0;
	for (int i = 0; i < n - 1; ++i)
	{
		cin >> u >> v;
		p.add(u, v, 1), p.add(v, u, 1);
	}
	calc(1);
	cout << ans << endl;
	return 0;
}


//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")

//# include <x86intrin.h>
# 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;

template<typename T> using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(),x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i,x,y)  for (ll i = x; i <= y; i ++)
#define FOr(i,x,y)  for (ll i = x; i >= y; i --)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to...                                                                                Red

void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
	// cin.exceptions(cin.failbit);
	// throws exception when do smth illegal
	// ex. try to read letter into int
	if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5+5;
const int M = 22;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());

int rnd (int l, int r) {
	return uniform_int_distribution<int> (l, r)(gen);
}

int dp[N][6];
int cnt[N][6];
int n, k;
vector < int > g[N];

ll ans;

void dfs (int v, int par = 0) {
	if(par) g[v].erase(find(every(g[v]), par));
	++cnt[v][0];

	for (auto to : g[v]) {
		dfs(to, v);

		for (int d1 = 0; d1 < k; ++d1) if(cnt[v][d1]) {
			for (int d2 = 0; d2 < k; ++d2) if(cnt[to][d2]) {
				int d = (d1+d2+1);
				ll cur = cnt[v][d1] * (ll)dp[to][d2] + dp[v][d1] * (ll)cnt[to][d2] + ((d+k-1)/k) * (ll)cnt[v][d1] * (ll)cnt[to][d2];
				ans += cur;
			}
		}

		for (int d2 = 0; d2 < k; ++d2) {
			cnt[v][(d2+1)%k] += cnt[to][d2];
			dp[v][(d2+1)%k] += dp[to][d2] + (d2 == k-1 ? cnt[to][d2] : 0);

		}
	}
}

void solve() {
	cin >> n >> k;

	for (int i = 2; i <= n; ++i) {
		int a, b;
		cin >> a >> b;
		g[a].pb(b);
		g[b].pb(a);
	}
	ans = 0;
	dfs(1);

	cout << ans << '\n';
}

int main () {
	SpeedForce;

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

	return Accepted;
}

// B...a


//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")

//# include <x86intrin.h>
# 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;

template<typename T> using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(),x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i,x,y)  for (ll i = x; i <= y; i ++)
#define FOr(i,x,y)  for (ll i = x; i >= y; i --)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to...                                                                                Red

void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
	// cin.exceptions(cin.failbit);
	// throws exception when do smth illegal
	// ex. try to read letter into int
	if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5+5;
const int M = 22;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());

int rnd (int l, int r) {
	return uniform_int_distribution<int> (l, r)(gen);
}

ll dp[N][6];
int cnt[N][6];
int n, k;
vector < int > g[N];

ll ans;

void dfs (int v, int par = 0) {
	if(par) g[v].erase(find(every(g[v]), par));
	++cnt[v][0];

	for (auto to : g[v]) {
		dfs(to, v);

		for (int d1 = 0; d1 < k; ++d1) if(cnt[v][d1]) {
			for (int d2 = 0; d2 < k; ++d2) if(cnt[to][d2]) {
				int d = (d1+d2+1);
				ll cur = cnt[v][d1] * (ll)dp[to][d2] + dp[v][d1] * (ll)cnt[to][d2] + ((d+k-1)/k) * (ll)cnt[v][d1] * (ll)cnt[to][d2];
				ans += cur;
			}
		}

		for (int d2 = 0; d2 < k; ++d2) {
			cnt[v][(d2+1)%k] += cnt[to][d2];
			dp[v][(d2+1)%k] += dp[to][d2] + (d2 == k-1 ? cnt[to][d2] : 0);

		}
	}
}

void solve() {
	cin >> n >> k;

	for (int i = 2; i <= n; ++i) {
		int a, b;
		cin >> a >> b;
		g[a].pb(b);
		g[b].pb(a);
	}
	ans = 0;
	dfs(1);

	cout << ans << '\n';
}

int main () {
	SpeedForce;

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

	return Accepted;
}

// B...a


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;

const int MAXN = 200010;

int n, k;

int prof[MAXN];
int freq[MAXN][5];

lli ans;

lli sumProf[MAXN];

vector< int > adj[MAXN];

void change(int cur, int viz, int c)
{
	sumProf[cur] += c*sumProf[viz];
	sumProf[cur] += c*freq[viz][k - 1];

	for(int j = 0 ; j < k ; j++)
		freq[cur][ (j + 1)%k ] += c*freq[viz][ j ];
}

void DFSCalculate(int cur, int p)
{
	freq[ cur ][0] = 1;

	for(int i = 0 ; i < adj[cur].size() ; i++)
	{
		int viz = adj[cur][i];

		if( viz == p ) continue;

		prof[viz] = prof[cur] + 1;
		DFSCalculate( viz , cur );

		change( cur , viz , 1 );
	}
}

void DFSRotate(int cur, int p)
{
	ans += sumProf[cur];
	ans += n - freq[cur][ 0 ];

	for(int i = 0 ; i < adj[cur].size() ; i++)
	{
		int viz = adj[cur][i];

		if( viz == p ) continue;

		vector< lli > oldCur;
		vector< lli > oldViz;

		for(int j = 0 ; j < k ; j++)
		{
			oldCur.push_back( freq[cur][j] );
			oldViz.push_back( freq[viz][j] );
		}

		oldCur.push_back( sumProf[cur] );
		oldViz.push_back( sumProf[viz] );

		change( cur , viz , -1 );
		change( viz , cur , 1 );

		DFSRotate( viz , cur );

		sumProf[cur] = oldCur.back();
		sumProf[viz] = oldViz.back();

		for(int j = 0 ; j < k ; j++)
		{
			freq[cur][j] = oldCur[j];
			freq[viz][j] = oldViz[j];
		}
	}
}

int main()
{
	scanf("%d %d",&n,&k);

	for(int i = 1 ; i < n ; i++)
	{
		int U, V;
		scanf("%d %d",&U,&V);

		adj[ U ].push_back( V );
		adj[ V ].push_back( U );
	}

	DFSCalculate( 1 , 1 );
	DFSRotate( 1 , 1 );

	printf("%lld\n",ans/2);
}


#include <iostream>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <cstdint>
#include <cassert>
#include <cmath>
#include <bitset>
#include <type_traits>
#include <iomanip>
#include <random>
#include <chrono>
#include <functional>
#include <utility>
// avoid TLE due to I/O bottleneck
#define __IO_HANDLE std::ios_base::sync_with_stdio(false), \
                  std::cin.tie(nullptr), \
                  std::cout.tie(nullptr), std::cerr << std::boolalpha;
// load any type of variables from std::cin for -std=c++17
// template<typename ...Args>
// void load_vars(Args&&... args) {
//     (std::cin >> ... >> args);
// }
// load any type of variables from std::cin for -std=c++11
template <typename T>
void q_load(T& t) {
    std::cin >> t;
}
template <typename Head, typename... Tail>
void q_load(Head& H, Tail&&... T) {
  std::cin >> H;
  q_load(T...);
}
// load 1d vector
template<typename T>
void load(std::vector<T>& v) {
    for (T& x: v) std::cin >> x;
}
// load 2d unweighted graph, shift is subtracted, both is for direction
template<typename T>
void load(std::vector< std::vector<T> >& v, int n_lines,
                    int shift=0, bool both=false) {
    for (int i = 0; i < n_lines; ++i) {
        T x, y; std::cin >> x >> y;
        x -= shift; y -= shift;
        v[x].push_back(y);
        if (both) { v[y].push_back(x); }
    }
}
// load 2d weighted graph, shift is subtracted, both is for direction
template<typename T, typename S>
void load(std::vector< std::vector< std::pair<T, S> > >& v,
                      int n_lines, int shift=0, bool both=false) {
    for (int i = 0; i < n_lines; ++i) {
        T x; S y; int z; std::cin >> x >> y >> z;
        x -= shift; y -= shift;
        v[x].push_back({y, z});
        if (both) { v[y].push_back({x, z}); }
    }
}
// load 2 parallel vectors
template<typename T, typename S>
void load(std::vector<T>& v1, std::vector<S>& v2, int n_lines) {
    for (int i = 0; i < n_lines; ++i) {
        T x; S y; std::cin >> x >> y;
        v1[i] = x;
        v2[i] = y;
    }
}
// load bit string to a bit vector
void load(std::vector<bool>& v) {
    char ch;
    for (int i = 0; i < (int)v.size(); ++i) {
        std::cin >> ch;
        v[i] = bool(ch - '0');
    }
}
// load a line to a string
std::istream& load(std::string& s) {
    return std::getline(std::cin, s);
}

// char* load(char* s) {
//     return gets(s);
// }

// random number generator - 32 bit int
std::mt19937 rng(
    (unsigned int)(
        std::chrono::steady_clock::now().time_since_epoch().count()
    )
);
// random number generator 64 bit int
std::mt19937_64 rng_64(
    (unsigned long long)(
        std::chrono::steady_clock::now().time_since_epoch().count()
    )
);
// std::uniform_int_distribution<int\int64_t> distrib(a, b);
// distrib(rng) to get the number from [a, b]

// helpful functions
namespace ds {
// some constants, not global, use ds::
const int32_t INF_INT32 = 1e9 + 42;
const int64_t INF_INT64 = 1e18 + 300;
const int MOD = 1e9 + 7;
const long double EPS = 1e-8;

inline int gcd(int a, int b) {
    return std::__gcd(a, b);
}

int topbit(signed t){
    return t ==0 ? -1 : (31 - __builtin_clz(t));
}
int topbit(long long t){
    return t ==0 ? -1 : (63-__builtin_clzll(t));
}
int botbit(signed a){
    return a == 0? 32 : __builtin_ctz(a);
}
int botbit(long long a){
    return a == 0 ? 64 : __builtin_ctzll(a);
}
int popcount(signed t){
    return __builtin_popcount(t);
}
int popcount(long long t){
    return __builtin_popcountll(t);
}
bool ispow2(int i){
    return i && ( i&-i ) == i;
}
long long mask(int i){
    return (1LL << i) - 1;
}

} // end namespace ds
// #include "codebase/debugging.hpp"

int n, k;
std::vector<std::vector<int> > graph;
long long ans = 0;
std::vector<std::vector<int> > subtree_count;
std::vector<int> total_subtree;
const int K_MAX = 5;

inline int subtract(int a, int b) {
    return ((a - b) % k + k) % k;
}

void dfs(int v, int p, int depth) {
    total_subtree[v] = 1;
    subtree_count[v][depth % k] = 1;
    for (int child: graph[v]) {
        if (child == p) continue;
        dfs(child, v, depth + 1);
        for (int i = 0; i < k; ++i) {
            for (int j = 0; j < k; ++j) {
                int remainder = subtract(i + j, 2 * depth);
                int needs = subtract(k, remainder);
                ans += (
                    (long long) needs *
                    subtree_count[v][i] *
                    subtree_count[child][j]
                );
            }
        }
        for (int i = 0; i < k; ++i) {
            subtree_count[v][i] += subtree_count[child][i];
        }
        total_subtree[v] += total_subtree[child];
    }
    ans += (
        (long long) total_subtree[v] * (n - total_subtree[v])
    );
}

int main()
{
    // turn_off_de_bug();
    __IO_HANDLE;
    q_load(n, k);
    graph.assign(n, std::vector<int>());
    subtree_count.assign(n, std::vector<int>(K_MAX, 0));
    total_subtree.assign(n, 0);

    for (int i = 0; i < n - 1; ++i) {
        int u, v; q_load(u, v);
        --u, --v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
//     debug_(graph)
    dfs(0, -1, 0);
    std::cout << (ans / k) << '\n';

    return 0;
}

/*
g++ cf_771_c.cpp && a < input\cf_771_c
submit cf_771_c.cpp
*/


#include <iostream>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <cstdint>
#include <cassert>
#include <cmath>
#include <bitset>
#include <type_traits>
#include <iomanip>
#include <random>
#include <chrono>
#include <functional>
#include <utility>
// avoid TLE due to I/O bottleneck
#define __IO_HANDLE std::ios_base::sync_with_stdio(false), \
                  std::cin.tie(nullptr), \
                  std::cout.tie(nullptr), std::cerr << std::boolalpha;
// load any type of variables from std::cin for -std=c++17
// template<typename ...Args>
// void load_vars(Args&&... args) {
//     (std::cin >> ... >> args);
// }
// load any type of variables from std::cin for -std=c++11
template <typename T>
void q_load(T& t) {
    std::cin >> t;
}
template <typename Head, typename... Tail>
void q_load(Head& H, Tail&&... T) {
  std::cin >> H;
  q_load(T...);
}
// load 1d vector
template<typename T>
void load(std::vector<T>& v) {
    for (T& x: v) std::cin >> x;
}
// load 2d unweighted graph, shift is subtracted, both is for direction
template<typename T>
void load(std::vector< std::vector<T> >& v, int n_lines,
                    int shift=0, bool both=false) {
    for (int i = 0; i < n_lines; ++i) {
        T x, y; std::cin >> x >> y;
        x -= shift; y -= shift;
        v[x].push_back(y);
        if (both) { v[y].push_back(x); }
    }
}
// load 2d weighted graph, shift is subtracted, both is for direction
template<typename T, typename S>
void load(std::vector< std::vector< std::pair<T, S> > >& v,
                      int n_lines, int shift=0, bool both=false) {
    for (int i = 0; i < n_lines; ++i) {
        T x; S y; int z; std::cin >> x >> y >> z;
        x -= shift; y -= shift;
        v[x].push_back({y, z});
        if (both) { v[y].push_back({x, z}); }
    }
}
// load 2 parallel vectors
template<typename T, typename S>
void load(std::vector<T>& v1, std::vector<S>& v2, int n_lines) {
    for (int i = 0; i < n_lines; ++i) {
        T x; S y; std::cin >> x >> y;
        v1[i] = x;
        v2[i] = y;
    }
}
// load bit string to a bit vector
void load(std::vector<bool>& v) {
    char ch;
    for (int i = 0; i < (int)v.size(); ++i) {
        std::cin >> ch;
        v[i] = bool(ch - '0');
    }
}
// load a line to a string
std::istream& load(std::string& s) {
    return std::getline(std::cin, s);
}

// char* load(char* s) {
//     return gets(s);
// }

// random number generator - 32 bit int
std::mt19937 rng(
    (unsigned int)(
        std::chrono::steady_clock::now().time_since_epoch().count()
    )
);
// random number generator 64 bit int
std::mt19937_64 rng_64(
    (unsigned long long)(
        std::chrono::steady_clock::now().time_since_epoch().count()
    )
);
// std::uniform_int_distribution<int\int64_t> distrib(a, b);
// distrib(rng) to get the number from [a, b]

// helpful functions
namespace ds {
// some constants, not global, use ds::
const int32_t INF_INT32 = 1e9 + 42;
const int64_t INF_INT64 = 1e18 + 300;
const int MOD = 1e9 + 7;
const long double EPS = 1e-8;

inline int gcd(int a, int b) {
    return std::__gcd(a, b);
}

int topbit(signed t){
	return t ==0 ? -1 : (31 - __builtin_clz(t));
}
int topbit(long long t){
	return t ==0 ? -1 : (63-__builtin_clzll(t));
}
int botbit(signed a){
	return a == 0? 32 : __builtin_ctz(a);
}
int botbit(long long a){
	return a == 0 ? 64 : __builtin_ctzll(a);
}
int popcount(signed t){
	return __builtin_popcount(t);
}
int popcount(long long t){
	return __builtin_popcountll(t);
}
bool ispow2(int i){
	return i && ( i&-i ) == i;
}
long long mask(int i){
	return (1LL << i) - 1;
}

} // end namespace ds
// #include "codebase/debugging.hpp"

int n, k;
std::vector<std::vector<int> > graph;
long long ans = 0;
std::vector<std::vector<int> > subtree_count;
std::vector<int> total_subtree;
const int K_MAX = 5;

inline int subtract(int a, int b) {
    return ((a - b) % k + k) % k;
}

void dfs(int v, int p, int depth) {
    total_subtree[v] = 1;
    subtree_count[v][depth % k] = 1;
    for (int child: graph[v]) {
        if (child == p) continue;
        dfs(child, v, depth + 1);
        for (int i = 0; i < k; ++i) {
            for (int j = 0; j < k; ++j) {
                int remainder = subtract(i + j, 2 * depth);
                int needs = subtract(k, remainder);
                ans += (
                    (long long) needs *
                    subtree_count[v][i] *
                    subtree_count[child][j]
                );
            }
        }
        for (int i = 0; i < k; ++i) {
            subtree_count[v][i] += subtree_count[child][i];
        }
        total_subtree[v] += total_subtree[child];
    }
    ans += (
        (long long) total_subtree[v] * (n - total_subtree[v])
    );
}

int main()
{
    // turn_off_de_bug();
    __IO_HANDLE;
    q_load(n, k);
    graph.assign(n, std::vector<int>());
    subtree_count.assign(n, std::vector<int>(K_MAX, 0));
    total_subtree.assign(n, 0);

    for (int i = 0; i < n - 1; ++i) {
        int u, v; q_load(u, v);
        --u, --v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
//     debug_(graph)
    dfs(0, -1, 0);
    std::cout << (ans / k) << '\n';

    return 0;
}

/*
g++ cf_771_c.cpp && a < input\cf_771_c
submit cf_771_c.cpp
*/


//g++  5.4.0

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define pii pair< int,int >
#define fast ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;

const int nax = 2e5+10;
vector<int> g[nax] , sz(nax,0) ,dp(nax,0) ;
int cnt[nax][5] , f[5];

signed main()
{
    fast;
    int n , k;
    cin >> n >> k;
    for(int i=1 ; i<n ; i++ )
    {
        int u,v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    function<void(int,int)> dfs_dp1 = [&](int v,int p)
    {
        for( auto &u : g[v] )
        {
            if( u != p )
            {
                dfs_dp1(u,v);
                sz[v] += sz[u];
                dp[v] += dp[u];
            }
        }
        dp[v] += sz[v];
        sz[v]++;
    };
    dfs_dp1(1,0);
    int total = 0;

    function< void(int,int) > dfs_dp2 = [&](int v,int p)
    {
        total += dp[v];
        //cout << v << ' ' << dp[v] << '\n';
        for( auto &u : g[v] )
        {
            if( u!=p )
            {
                dp[v] -= (sz[u] + dp[u] );
                sz[v] -= sz[u];
                dp[u] += (sz[v] + dp[v] );
                sz[u] += sz[v];
                dfs_dp2(u,v);
                sz[u] -= sz[v];
                dp[u] -= (sz[v] + dp[v] );
                sz[v] += sz[u];
                dp[v] += (sz[u] + dp[u] );
            }
        }
    };
    dfs_dp2(1,0);
    total >>= 1;

    function< void(int,int) > dfs_dp3 =[&](int v,int p)
    {
        cnt[v][0] = 1;
        for( auto &u : g[v] )
        {
            if( u!=p )
            {
                dfs_dp3(u,v);
                for(int i=0 ; i<k ; i++ )
                {
                    for(int j=0 ; j<k ; j++ )
                        f[ (i+j+1)%k ] += ( cnt[v][i] * cnt[u][j] );
                }
                for(int i=0 ; i<k ; i++ )
                    cnt[v][(i+1)%k] += cnt[u][i];
            }
        }
    };
    dfs_dp3(1,0);
    for(int i=1 ; i<k ; i++ )
        total += ( f[i]*(k-i) );
    cout << total/k;

}


#include <bits/stdc++.h>
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pb push_back
#define MP make_pair
#define lowbit(x) x&-x
#define clr(a) memset(a,0,sizeof(a))
#define _INF(a) memset(a,0x3f,sizeof(a))
#define FIN freopen("in.txt","r",stdin)
#define IOS ios::sync_with_stdio(false)
#define debug(x) cout<<"["<<x<<"]"<<endl
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int>pii;
const int MX = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int n, m;
int sz[MX];
vector<int>E[MX];
ll dp[MX][10], ans;

void dfs1(int u, int fa, int dep) {
    sz[u] = dp[u][dep % m] = 1;
    for (auto v : E[u]) {
        if (v == fa) continue;
        dfs1(v, u, dep + 1);
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                int dis = ((i + j) % m - (dep * 2) % m + m) % m;
                ans += ((m - dis) % m) * dp[u][i] * dp[v][j];
            }
        }
        for (int i = 0; i < m; i++) dp[u][i] += dp[v][i];
        sz[u] += sz[v];
        ans += sz[v] * (n - sz[v]);
    }
}

int main() {
    //FIN;
    scanf("%d%d", &n, &m);
    for (int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        E[u].pb(v); E[v].pb(u);
    }
    dfs1(1, 1, 0);
    cout << ans / m << endl;
    return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pb push_back
#define MP make_pair
#define lowbit(x) x&-x
#define clr(a) memset(a,0,sizeof(a))
#define _INF(a) memset(a,0x3f,sizeof(a))
#define FIN freopen("in.txt","r",stdin)
#define IOS ios::sync_with_stdio(false)
#define debug(x) cout<<"["<<x<<"]"<<endl
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int>pii;
const int MX = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int n, m;
int sz[MX];
vector<int>E[MX];
ll dp[MX][10], ans;

void dfs1(int u, int fa, int dep) {
    sz[u] = dp[u][dep % m] = 1;
    for (auto v : E[u]) {
        if (v == fa) continue;
        dfs1(v, u, dep + 1);
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                int dis = (ll)((i + j) % m - (dep * 2) % m + m) % m;
                ans += (ll)((m - dis) % m) * dp[u][i] * dp[v][j];
            }
        }
        for (int i = 0; i < m; i++) dp[u][i] += dp[v][i];
        sz[u] += sz[v];
        ans += (ll)sz[v] * (n - sz[v]);
    }
}

int main() {
    //FIN;
    scanf("%d%d", &n, &m);
    for (int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        E[u].pb(v); E[v].pb(u);
    }
    dfs1(1, 1, 0);
    cout << ans / m << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long n,k,ans,dp1[200001][6],dp2[200001][6],dpup1[200001][6],dpup2[200001][6];
vector<int> graph[200001];

void add(int curr , int i , int tag){
	for(int j = 2 ; j <= k ; j += 1){
		dp1[curr][j] += tag*dp1[i][j-1];
		dp2[curr][j] += tag*dp2[i][j-1];
	}
	dp1[curr][1] += tag*dp1[i][k];
	dp2[curr][1] += tag*(dp2[i][k]+dp1[i][k]);
	dp1[curr][1] += tag*1;
	dp2[curr][1] += tag*1;
}

void add2(int curr , int i){
	for(int j = 2 ; j <= k ; j += 1){
		dpup1[curr][j] += dpup1[i][j-1]+dp1[i][j-1];
		dpup2[curr][j] += dpup2[i][j-1]+dp2[i][j-1];;
	}
	dpup1[curr][1] += dpup1[i][k]+dp1[i][k];
	dpup2[curr][1] += dpup2[i][k]+dpup1[i][k]+dp2[i][k]+dp1[i][k];
	dpup1[curr][1] += 1;
	dpup2[curr][1] += 1;
}

void dfs(int curr , int par){
	for(int i : graph[curr]){
		if(i == par){
			continue;
		}
		dfs(i,curr);
		add(curr,i,1);
	}
	for(int i = 1 ; i <= k ; i += 1){
		ans += dp2[curr][i];
	}
}

void dfs2(int curr , int par){
	for(int i : graph[curr]){
		if(i == par){
			continue;
		}
		add(curr,i,-1);
		add2(i,curr);
		dfs2(i,curr);
		add(curr,i,1);
	}
	for(int i = 1 ; i <= k ; i += 1){
		ans += dpup2[curr][i];
	}
}

int main(){
	cin >> n >> k;
	for(int i = 0 ; i < n-1 ; i += 1){
		int a,b;
		cin >> a >> b;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	dfs(1,1);
	dfs2(1,1);
	cout << ans/2 << endl;
}


#define ll long long
#define dd long double
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mp make_pair
#define mt make_tuple
#define fo(i , n) for(ll i = 0 ; i < n ; i++)
//#include<bits/stdc++.h>
#include<iomanip>
#include<cmath>
#include<cstdio>
#include<utility>
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<bitset>
dd pi = acos(-1) ;
ll z =  1000000007 ;
ll inf = 100000000000000000 ;
ll p1 = 37 ;
ll p2 = 53 ;
ll mod1 =  202976689 ;
ll mod2 =  203034253 ;
ll fact[100] ;
ll gdp(ll a , ll b){return (a - (a%b)) ;}
ll ld(ll a , ll b){if(a < 0) return -1*gdp(abs(a) , b) ; if(a%b == 0) return a ; return (a + (b - a%b)) ;} // least number >=a divisible by b
ll gd(ll a , ll b){if(a < 0) return(-1 * ld(abs(a) , b)) ;    return (a - (a%b)) ;} // greatest number <= a divisible by b
ll gcd(ll a , ll b){ if(b > a) return gcd(b , a) ; if(b == 0) return a ; return gcd(b , a%b) ;}
ll e_gcd(ll a , ll b , ll &x , ll &y){ if(b > a) return e_gcd(b , a , y , x) ; if(b == 0){x = 1 ; y = 0 ; return a ;}
ll x1 , y1 ; e_gcd(b , a%b , x1 , y1) ; x = y1 ; y = (x1 - ((a/b) * y1)) ; return e_gcd(b , a%b , x1 , y1) ;}
ll power(ll a ,ll b , ll p){if(b == 0) return 1 ; ll c = power(a , b/2 , p) ; if(b%2 == 0) return ((c*c)%p) ; else return ((((c*c)%p)*a)%p) ;}
ll inverse(ll a ,ll n){return power(a , n-2 , n) ;}
ll max(ll a , ll b){if(a > b) return a ; return b ;}
ll min(ll a , ll b){if(a < b) return a ; return b ;}
ll left(ll i){return ((2*i)+1) ;}
ll right(ll i){return ((2*i) + 2) ;}
ll ncr(ll n , ll r){if(n < r|| (n < 0) || (r < 0)) return 0 ; return ((((fact[n] * inverse(fact[r] , z)) % z) * inverse(fact[n-r] , z)) % z);}
void swap(ll&a , ll&b){ll c = a ; a = b ; b = c ; return ;}
//ios_base::sync_with_stdio(0);
//cin.tie(0); cout.tie(0);
using namespace std ;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
// s.find_by_order(i)  itertor to ith element (0 indexed)
//__builtin_popcount(n) -> returns number of set bits in n
// ll seed;
//mt19937 rnd(seed=chrono::steady_clock::now().time_since_epoch().count()); // include bits
ll cross_at[200005][5] , end_at[200005][5] ;
ll sub_tree[200005] ;
ll n , k ;
void DFS(vector<ll> adj[] , ll u , ll par)
{
    if(adj[u].size() == 1 && u != 0)
    {
        sub_tree[u] = 1 ;
        end_at[u][0] = 1 ;
        return ;
    }
    ll tot[k] ;
    fo(i , k)
        tot[i] = 0 ;
    fo(i , adj[u].size())
    {
        ll v = adj[u][i] ;
        if(v == par)
            continue;
        DFS(adj , v , u) ;
        fo(i , k)
        {
            tot[i] += end_at[v][i] ;
            end_at[u][(i+1)%k] += end_at[v][i] ;
        }
        sub_tree[u] += sub_tree[v] ;
    }
    end_at[u][0]++ ;
    sub_tree[u]++ ;
    fo(i , adj[u].size())
    {
        ll v = adj[u][i] ;
        if(v == par)
            continue ;
        fo(x , k)
        {
            fo(y , k)
            {
                ll no_x = end_at[v][x] ;
                ll no_y = tot[y] - end_at[v][y] ;
                cross_at[u][(x + y + 2)%k] += (no_x * no_y) ;
            }
        }
    }
}

void solve()
{

    cin >> n >> k ;
    vector<ll> adj[n] ;
    fo(i , n-1)
    {
        ll u , v ;
        cin >> u >> v ;
        u-- ;
        v--  ;
        adj[u].pub(v) ;
        adj[v].pub(u) ;
    }
    fo(i , n+5)
    {
        fo(j , 5)
        {
            cross_at[i][j] = end_at[i][j] = 0 ;
        }
        sub_tree[i] = 0 ;
    }

    DFS(adj , 0 , -1) ;
    ll ans = 0 ;
    fo(i , n)
    {
        ans += (sub_tree[i]*(n-sub_tree[i])) ;
        fo(j , k)
        {
            ll mult = (k-j) ;
            if(j == 0)
                mult = 0 ;
            ans += (((cross_at[i][j]/2) + end_at[i][j]) * (mult)) ;
        }
    }
    /*fo(i , n)
    {
        cout << i+1 << " -> " ;
        fo(j , k)
        {
            cout << end_at[i][j] << ' ';
        }
        cout << '\n' ;
    }*/
    cout << ans/k << '\n' ;
    return ;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("inputf.txt" , "r" , stdin) ;
    freopen("outputf.txt" , "w" , stdout) ;
    freopen("error.txt" , "w" , stderr) ;
    #endif

    ll t ;
    //cin >> t ;
    t = 1 ;

    while(t--)
    {
        solve() ;
    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <stack>
#include <deque>
#include <iterator>
#include <tuple>
#include <iomanip>
#include <valarray>
#include <limits>
#include <sstream>
#include <cassert>
#include <functional>
#include <numeric>
#define in insert
#define pb push_back
#define mp make_pair
#define int long long
#define fi first
#define double long double
#define se second
#define endl '\n'
#define pii pair<int,int>
#define pdd pair<double,double>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
//#define mod 1000000007
#define MAXN  2
using namespace std;
int spf[MAXN];
int fac[MAXN];
void sieve()//sieve
{
    spf[1] = 1;
    for (int i=2; i<MAXN; i++)
        spf[i] = i;
    for (int i=4; i<MAXN; i+=2)
        spf[i] = 2;
    for (int i=3; i*i<MAXN; i++)
    {
        if (spf[i]==i)
        {
            for (int j=i*i; j<MAXN; j+=i)
                if (spf[j]==j)
                    spf[j]=i;
        }
    }
}//sieve ends
map<int,int> getfactor(int a)//factor prime
{
    map<int,int> m;
    while(a>1)
    {
        m[spf[a]]++;
        a/=spf[a];
    }
    return m;
}//prime factor ends
int power(int x,int y,int p)
{
    int res = 1;
    x = x ;//% p;
    if (x == 0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x);//% p;
        y = y>>1;
        x = (x*x) ;//%p;
    }
    return res;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int inverse(int a, int p)
{
    return power(a, p-2, p);
}
int ncr(int n, int r, int p)
{
    if (r==0)
        return 1;
    return (fac[n]*inverse(fac[r], p) % p *
            inverse(fac[n-r], p) % p) % p;
}
int dp[200001][6]={0};
int val[200001][6]={0};
int d[200001][6]={0};
vector<int> v[200001];
int ans1=0,n,k;
void dfs(int cur=1,int par=0)
{
    int ans=0;
    for(auto it:v[cur])
    {
        if(it!=par)
        {
            dfs(it,cur);
            for(int i=1;i<=k;i++)
            {
                for(int j=1;j<=k;j++)
                {
                    if(i+j>=k&&i+j<2*k)
                        ans+=dp[it][i]*val[cur][j]+val[it][i]*dp[cur][j];
                    else if(i+j==2*k)
                    {
                        ans+=dp[it][i]*val[cur][j]+val[it][i]*dp[cur][j]+val[it][i]*val[cur][j];
                    }
                    else{
                        ans+=dp[it][i]*val[cur][j]+val[it][i]*(dp[cur][j]-val[cur][j]);
                    }
                }
                if(i!=k)
                    ans+=dp[cur][i];
                else ans+=dp[cur][i]+val[cur][i];
            }
            for(int i=1;i<=k;i++)
            {
                if(i==1)
                {
                    dp[cur][1]+=dp[it][k]+val[it][k];
                    val[cur][1]+=val[it][k];
                }
                else{
                    dp[cur][i]+=dp[it][i-1];
                    val[cur][i]+=val[it][i-1];
                    d[cur][i]=d[it][i-1];
                }
            }
            dp[cur][1]++;
            val[cur][1]++;
            d[cur][1]++;
        }
    }
    for(int i=1;i<=k;i++)
        ans+=dp[cur][i];
    //if(cur==10)
        //cout<<ans<<endl;
    ans1+=ans;
}
void solve()
{
    cin>>n>>k;
    for(int i=0;i<n-1;i++)
    {
        int x,y;
        cin>>x>>y;
        v[x].pb(y);
        v[y].pb(x);
    }
    dfs();
    cout<<ans1<<endl;
}
signed main()
{
    fast
    int t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}


#include <bits/stdc++.h>
using namespace std;
const int Maxn=200005;
int n,head[Maxn],cnt,siz[Maxn],ans[Maxn],sum[Maxn],k,mod[Maxn][7],sum_mod[Maxn][7];
vector <int> Ve[Maxn];
struct edg
{
	int nxt,to;
}edge[2*Maxn];
void add(int x,int y)
{
	edge[++cnt]=(edg){head[x],y};
	head[x]=cnt;
}
void dfs(int u,int fa)
{
	siz[u]=1;
	mod[u][0]=1;
	for(int i=head[u];i;i=edge[i].nxt)
	{
		int to=edge[i].to;
		if(to!=fa)
		{
			dfs(to,u);
			ans[u]+=sum[u]*siz[to]+siz[u]*(siz[to]+sum[to])+ans[to];
			siz[u]+=siz[to];
			sum[u]+=sum[to]+siz[to];
			for(int i=0;i<k;i++)
				mod[u][i]+=mod[to][(i-1+k)%k];
			for(vector <int>::iterator it=Ve[u].begin();it!=Ve[u].end();it++)
			{
				for(int i=0;i<k;i++)
					for(int j=0;j<k;j++)
						sum_mod[u][(i+j)%k]+=mod[to][(i-1+k)%k]*mod[*it][(j-1+k)%k];
			}
			Ve[u].push_back(to);
		}
	}
	for(int i=0;i<k;i++)
		sum_mod[u][i]+=mod[u][i];
}
int main()
{
//	freopen("C.in","r",stdin);
	scanf("%d%d",&n,&k);
	for(int i=1;i<n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
	}
	dfs(1,0);
	long long all=0;
	for(int j=1;j<=n;j++)
		for(int i=1;i<k;i++)
			all+=sum_mod[j][i]*(k-i);
	printf("%lld",(ans[1]+all)/k);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int Maxn=200005;
int n,head[Maxn],cnt,siz[Maxn],ans[Maxn],sum[Maxn],k,mod[Maxn][7],sum_mod[Maxn][7];
vector <int> Ve[Maxn];
struct edg
{
	int nxt,to;
}edge[2*Maxn];
void add(int x,int y)
{
	edge[++cnt]=(edg){head[x],y};
	head[x]=cnt;
}
void dfs(int u,int fa)
{
	siz[u]=1;
	mod[u][0]=1;
	for(int i=head[u];i;i=edge[i].nxt)
	{
		int to=edge[i].to;
		if(to!=fa)
		{
			dfs(to,u);
			ans[u]+=sum[u]*siz[to]+siz[u]*(siz[to]+sum[to])+ans[to];
			siz[u]+=siz[to];
			sum[u]+=sum[to]+siz[to];
			for(int i=0;i<k;i++)
				for(int j=0;j<k;j++)
					sum_mod[u][(i+j)%k]+=mod[to][(i-1+k)%k]*mod[u][j];
			for(int i=0;i<k;i++)
				mod[u][i]+=mod[to][(i-1+k)%k];
		}
	}
//	for(int i=0;i<k;i++)
//		sum_mod[u][i]+=mod[u][i];
}
int main()
{
//	freopen("C.in","r",stdin);
	scanf("%d%d",&n,&k);
	for(int i=1;i<n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
	}
	dfs(1,0);
	long long all=0;
	for(int j=1;j<=n;j++)
		for(int i=1;i<k;i++)
			all+=sum_mod[j][i]*(k-i);
	printf("%lld",(ans[1]+all)/k);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int Maxn=200005;
int n,head[Maxn],cnt,siz[Maxn];
long long ans[Maxn],sum[Maxn],k,mod[Maxn][7],sum_mod[Maxn][7];
struct edg
{
	int nxt,to;
}edge[2*Maxn];
void add(int x,int y)
{
	edge[++cnt]=(edg){head[x],y};
	head[x]=cnt;
}
void dfs(int u,int fa)
{
	siz[u]=1;
	mod[u][0]=1;
	for(int i=head[u];i;i=edge[i].nxt)
	{
		int to=edge[i].to;
		if(to!=fa)
		{
			dfs(to,u);
			ans[u]+=sum[u]*siz[to]+siz[u]*(siz[to]+sum[to])+ans[to];
			siz[u]+=siz[to];
			sum[u]+=sum[to]+siz[to];
			for(int i=0;i<k;i++)
				for(int j=0;j<k;j++)
					sum_mod[u][(i+j)%k]+=mod[to][(i-1+k)%k]*mod[u][j];
			for(int i=0;i<k;i++)
				mod[u][i]+=mod[to][(i-1+k)%k];
		}
	}
//	for(int i=0;i<k;i++)
//		sum_mod[u][i]+=mod[u][i];
}
int main()
{
//	freopen("C.in","r",stdin);
	scanf("%d%d",&n,&k);
	for(int i=1;i<n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
	}
	dfs(1,0);
	long long all=0;
	for(int j=1;j<=n;j++)
		for(int i=1;i<k;i++)
			all+=sum_mod[j][i]*(k-i);
	printf("%lld",(ans[1]+all)/k);
	return 0;
}


//#include "stdc++.h"
#include <bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define flush fflush(stdout)
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,v) memset(x,v,sizeof(x))
//#define sz(x) (int)(x.size())
#define ff first
#define ss second
#define endl "\n"
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0)return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p;}
const ll MOD =1000000007 ;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
#define pi 3.141592653589793238462
#define int  long long
vi adj[200000+1];int d[200001][6];int k;int ans[200001];int tl,distk;
void dfs(int u,int p,int level)
{
    d[u][0]=1;int v;
    ans[1]+=ceil((double)level/k);
    for(int i=0;i<adj[u].size();i++)
    {
        v=adj[u][i];
        if(p!=v)
        {
            dfs(v,u,level+1);
            for(int j=0;j<k;j++)
            {
                d[u][j]+=d[v][(j-1+k)%k];
            }
        }
    }

}
void dfs1(int u,int p)
{
    if(p!=0)
    {
        distk=d[p][0]-d[u][k-1];
        ans[u]=ans[p]+distk;
        ans[u]-=(d[u][0]);
        int d1[6];
        for(int j=0;j<k;j++)
            d1[j]=d[u][j];
        for(int j=0;j<k;j++)
        d[u][j]+=(d[p][(j-1+k)%k]-d1[(j-2+k)%k]);
    }
    int v; tl+=ans[u];
   // cout<<u<<" "<<d[u][0]<<endl;
    for(int i=0;i<adj[u].size();i++)
    {
        v=adj[u][i];
        if(v!=p)
        {
            dfs1(v,u);
        }
    }
}
int32_t main()
{

    IOS;
    int n,u,v;
    cin>>n>>k;
    for(int i=0;i<n-1;i++)
    {
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(1,0,0);
    //tl=ans[1];
    //cout<<ans[1]<<endl;

    dfs1(1,0);
    //for(int i=1;i<=13;i++)
      //  cout<<ans[i]<<" ";
    //cout<<endl;
    cout<<tl/2<<endl;
    return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef map<ll,ll> mll;
typedef vector<bool> vb;

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define endl "\n"
#define prina(arr,dau,cuoi) FTB(mmczmp,dau,cuoi)cout<<arr[mmczmp]<<" ";cout<<endl;
#define debug cout<<"debug\n"
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define pb push_back
#define pbg(ke,u,v) ke[u].pb(v);ke[v].pb(u);
#define fi first
#define se second
#define pi acos(-1.0)
#define lbnd(f,s,gt) lower_bound(f,s,gt)
#define ubnd(f,s,gt) upper_bound(f,s,gt)
#define all(s) s.begin(),s.end()
#define bitcount(n) __builtin_popcountll(n)
#define REP(N) for(ll asgardhaha=1;asgardhaha<=N;asgardhaha++)
#define FTB(i,a,b) for(ll i=a,_b=b;i<=_b;i++)
#define FT(i,a,b) for(ll i=a,_b=b;i<_b;i++)
#define FGB(i,a,b) for(ll i=a,_b=b;i>=_b;i--)
#define FG(i,a,b) for(ll i=a,_b=b;i>_b;i--)
/////////////////////////////////////////////

ll n,k,rem[6],total,re[200005][6],con[200005];
vi ke[200005];

void dfs(ll u,ll p=0){
    con[u]=1;
    re[u][0]=1;
    for(auto v:ke[u]){
        if(v==p) continue;
        dfs(v,u);
        con[u]+=con[v];
        FT(i,0,k) FT(j,0,k){
            rem[(i+j+1)%k]+=re[u][i]*re[v][j];
        }
        FT(i,0,k){
            re[u][(i+1)%k]+=re[v][i];
        }
    }
    total+=con[u]*(n-con[u]);
}

void Solve(){
    cin>>n>>k;
    FT(i,1,n){
        ll u,v;cin>>u>>v;
        pbg(ke,u,v);
    }
    dfs(1);
    FT(i,1,k) total+=(k-i)*rem[i];
    cout<<total/k;
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);

//    freopen("abc.in","r",stdin);
//    freopen("abc.out","w",stdout);

    int Test_numbers=1;
    //cin>>Test_numbers;

    while(Test_numbers--) Solve();

    //cerr << "\nTime run: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";

	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef vector<int> vi;

template<class T> inline T re(){
	T N = 0; char c = getchar(); bool neg = 0;
	for (; c < '0' || c > '9'; c = getchar()) neg |= c == '-';
	for (; c >= '0' && c <= '9'; c = getchar())
		N = (N<<3)+(N<<1) + c - '0';
	return neg ? -N : N;
}

const int MX = 2e5;
int n, k;
vi adj[MX + 5];
LL sz[MX + 5], ans = 0;
LL rem[MX + 5][5];

void dfs(int u, int prv = 0, int d = 0){
	sz[u] = rem[u][d % k] = 1;
	for (int nx : adj[u])
		if (nx != prv){
			dfs(nx, u, d + 1);
			for (int i = 0; i < k; i++)
				for (int j = 0; j < k; j++){
					int paths = (((i + j - 2 * d) % k) + k) % k;
					int x = (k - paths) % k;
					ans += x * rem[u][i] * rem[nx][j];
				}
			for (int i = 0; i < k; i++)
				rem[u][i] += rem[nx][i];
			sz[u] += sz[nx];
		}
	ans += sz[u] * (n - sz[u]);
}

int main(){
	n = re<int>(); k = re<int>();
	for (int i = 1, a, b; i < n; i++){
		a = re<int>(); b = re<int>();
		adj[a].pb(b); adj[b].pb(a);
	}
	dfs(1);
	printf("%lld\n", ans / k);
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef vector<int> vi;

template<class T> inline T re(){
	T N = 0; char c = getchar(); bool neg = 0;
	for (; c < '0' || c > '9'; c = getchar()) neg |= c == '-';
	for (; c >= '0' && c <= '9'; c = getchar())
		N = (N<<3)+(N<<1) + c - '0';
	return neg ? -N : N;
}

const int MX = 1e5;
int n, k;
vi adj[MX + 5];
LL sz[MX + 5], ans = 0;
LL rem[MX + 5][5];

void dfs(int u, int prv = 0, int d = 0){
	sz[u] = rem[u][d % k] = 1;
	for (int nx : adj[u])
		if (nx != prv){
			dfs(nx, u, d + 1);
			for (int i = 0; i < k; i++)
				for (int j = 0; j < k; j++){
					int paths = (((i + j - 2 * d) % k) + k) % k;
					int x = (k - paths) % k;
					ans += x * rem[u][i] * rem[nx][j];
				}
			for (int i = 0; i < k; i++)
				rem[u][i] += rem[nx][i];
			sz[u] += sz[nx];
		}
	ans += sz[u] * (n - sz[u]);
}

int main(){
	n = re<int>(); k = re<int>();
	for (int i = 1, a, b; i < n; i++){
		a = re<int>(); b = re<int>();
		adj[a].pb(b); adj[b].pb(a);
	}
	dfs(1);
	assert(ans % k == 0);
	printf("%lld\n", ans / k);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
/* Policy Based Data Structure
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<
    int,
    null_type,
    less<int>,
    rb_tree_tag,
    tree_order_statistics_node_update > pbds;
*/
#define print_time {cout << flush; cerr << endl << "Time taken: " << fixed << setprecision(3) << 1.0 * clock() / CLOCKS_PER_SEC << " secs\n";}
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//#define int long long
#define LD long double
#define testcases int test; cin >> test; for (int tc = 1; tc <= test; tc++)
#define db(...) ZZ(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>void ZZ(const char* name, Arg1&& arg1){std::cout << name << " = " << arg1 << endl;}
template <typename Arg1, typename... Args>void ZZ(const char* names, Arg1&& arg1, Args&&... args)
{
  const char* comma = strchr(names + 1, ',');
  std::cout.write(names, comma - names) << " = " << arg1;
  ZZ(comma, args...);
}
int modpow(int a, int b, int m) {int res = 1; while (b) {if (b & 1) res = (res * a) % m; a = (a * a) % m; b >>= 1;} return res;}
int gcd(int a, int b) {if (a == 0 && b == 0) return 1; return __gcd(a,b);}

const int inf = (int)1e9;
const int N = 2e5+5;

int n, k;
long long answer;
vector<int> g[N];
long long subtree[N], dp[6][N];
void dfs(int node = 1, int parent = -1) {
  subtree[node] = 1;
  for (int i = 0; i < k; i++) {
    dp[i][node] = (i == 0);
  }
  for (int next : g[node]) {
    if (next == parent) continue;
    dfs(next, node);
    long long _dp[6];
    for (int r = 0; r < k; r++) {
      _dp[(r + 1) % k] = dp[r][next];
    }
    for (int r1 = 0; r1 < k; r1++) {
      for (int r2 = 0; r2 < k; r2++) {
        int rem = (r1 + r2) % k;
        int needed = (k - rem) % k;
        answer += needed * dp[r1][node] * _dp[r2];
      }
    }
    for (int r = 0; r < k; r++) {
      dp[r][node] += _dp[r];
    }
    subtree[node] += subtree[next];
  }
  answer += subtree[node] * (n - subtree[node]);
}

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  dfs();
  printf("%lld", answer / k);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
/* Policy Based Data Structure
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<
    int,
    null_type,
    less<int>,
    rb_tree_tag,
    tree_order_statistics_node_update > pbds;
*/
#define print_time {cout << flush; cerr << endl << "Time taken: " << fixed << setprecision(3) << 1.0 * clock() / CLOCKS_PER_SEC << " secs\n";}
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//#define int long long
#define LD long double
#define testcases int test; cin >> test; for (int tc = 1; tc <= test; tc++)
#define db(...) ZZ(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>void ZZ(const char* name, Arg1&& arg1){std::cout << name << " = " << arg1 << endl;}
template <typename Arg1, typename... Args>void ZZ(const char* names, Arg1&& arg1, Args&&... args)
{
  const char* comma = strchr(names + 1, ',');
  std::cout.write(names, comma - names) << " = " << arg1;
  ZZ(comma, args...);
}
int modpow(int a, int b, int m) {int res = 1; while (b) {if (b & 1) res = (res * a) % m; a = (a * a) % m; b >>= 1;} return res;}
int gcd(int a, int b) {if (a == 0 && b == 0) return 1; return __gcd(a,b);}

const int inf = (int)1e9;
const int N = 2e5+5;

int n, k, answer;
vector<int> g[N];
int subtree[N], dp[6][N];
void dfs(int node = 1, int parent = -1) {
  subtree[node] = 1;
  for (int i = 0; i < k; i++) {
    dp[i][node] = (i == 0);
  }
  for (int next : g[node]) {
    if (next == parent) continue;
    dfs(next, node);
    int _dp[6];
    for (int r = 0; r < k; r++) {
      _dp[(r + 1) % k] = dp[r][next];
    }
    for (int r1 = 0; r1 < k; r1++) {
      for (int r2 = 0; r2 < k; r2++) {
        int rem = (r1 + r2) % k;
        int needed = (k - rem) % k;
        answer += needed * dp[r1][node] * _dp[r2];
      }
    }
    for (int r = 0; r < k; r++) {
      dp[r][node] += _dp[r];
    }
    subtree[node] += subtree[next];
  }
  answer += subtree[node] * (n - subtree[node]);
}

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  dfs();
  printf("%d", answer / k);
  return 0;
}


#include <bits/stdc++.h>

#define ld long double
#define ll long long
#define F first
#define S second
#define pb push_back
#define sqr(x) (x) * (x)

using namespace std;

//mt19937 gen(time(0));

const int maxn = 200500;
const int inf = 1e9 + 7;

int n, k;
vector <int> g[maxn];
ll dp[maxn][5];
ll sz[maxn];
ll cnt = 0;

void dfs(int v, int pr)
{
    sz[v] = 1;
    dp[v][0]++;
    for(int to:g[v])
    {
        if (to != pr)
        {
            dfs(to, v);
            sz[v] += sz[to];
            for(int i = 0; i < k; ++i)
                for(int j = 0; j < k; ++j)
            {
                int d = (i + j + 1) % k;
                if (d > 0)
                {
                    cnt += (k - d) * dp[v][i] * dp[to][j];
                }
            }
            for(int i = 1; i < k; ++i)
            {
                dp[v][i] += dp[to][i - 1];
            }
            dp[v][0] += dp[to][k - 1];
        }
    }
    cnt += sz[v] * (n - sz[v]);
    return;
}

int main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else

#endif
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    for(int i = 0; i < n - 1; ++i)
    {
        int x, y;
        cin >> x >> y;
        --x;
        --y;
        g[x].pb(y);
        g[y].pb(x);
    }
    dfs(0, 0);
    cout << cnt / k;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5 , M = 6;
vector<int> adj[N];
int n , k;
long long c[N][M] , s[N][M] , ans = 0;

void dfs (int node , int par = -1) {
    for (int child : adj[node]) {
        if (child != par) {
            dfs(child , node);

            for (int i = 1 ;i <= k ;i++) {
                if (i == k)
                    ans += s[node][i] + c[node][i];
                else
                    ans += s[node][i];

                for (int j = 1 ;j <= k ;j++) {
                    ans += c[child][i] * s[node][j];
                    ans += c[node][j] * s[child][i];
                    int len = i + j + 1;
                    if (len <= k)
                        ans -= c[child][i] * c[node][j];
                    else if (len > 2 * k)
                        ans += c[child][i] * c[node][j];
                }
            }

            ans++;
            c[node][1]++;
            s[node][1]++;

            for (int i = 1 ;i <= k ;i++) {
                if (i == k) {
                    ans += s[child][i] + c[child][i];
                    s[node][1] += s[child][i] + c[child][i];
                    c[node][1] += c[child][i];
                } else {
                    ans += s[child][i];
                    s[node][i + 1] += s[child][i];
                    c[node][i + 1] += c[child][i];
                }
            }
        }
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.in" , "r" , stdin);
#endif
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> k;

    for (int i = 0 ;i < n - 1 ;i++) {
        int u , v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    dfs(1);

    cout << ans ;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5 , M = 6;
int k , t[M];

int calc (int i , int t) {
    if (i > t)
        return 0;
    return 1 + (t - i) / k;
}

int nw (int i , int shift) {
    i--;
    return ((i + shift) % k + k) % k + 1;
}

class segtree {
    struct node {
        long long v = 0; // default value for the leafs
        int lazy = 0; // no lazy value
        int cnt[M] = {};

        void apply(int l, int r, long long x) {
            for (int i = 1 ;i <= k ;i++)
                t[i] = cnt[i];

            for (int i = 1 ;i <= k ;i++) {
                cnt[nw(i , x)] = t[i];
                if (x < 0)
                    v -= t[i] * calc(i , -x);
                else
                    v += t[i] * calc(k - i + 1 , x);
            }
            // set v to the new value of range [l , r] when updated with x
        }

        void add_lazy(long long x) {
            lazy += x;
            // lazy combine
        }

        node operator +(const node &other) const {
            node ret;
            ret.v = v + other.v;
            for (int i = 1 ;i <= k ;i++)
                ret.cnt[i] = cnt[i] + other.cnt[i];
            return ret;
        }

        bool isLazy() {return lazy != 0;}
        void cancel() {lazy = 0;}
    };

    void push(int l, int r, int p) {
        if (tree[p].isLazy()) {
            tree[p].apply(l, r, tree[p].lazy);
            if (l != r) {
                tree[left(p)].add_lazy(tree[p].lazy);
                tree[right(p)].add_lazy(tree[p].lazy);
            }
            tree[p].cancel();
        }
    }

    int left(int p) { return p << 1; }
    int right(int p) { return (p << 1) + 1; }

    int n;
    vector<node> tree;

    void build(int l, int r, int p, const vector<int> &v) {
        if (l == r) {
            tree[p].v = v[l];
            return;
        }
        int mid = (l + r) >> 1;
        build(l, mid, left(p), v);
        build(mid + 1, r, right(p), v);
        tree[p] = tree[left(p)] + tree[right(p)];
    }

    void update(int s, int e, long long v, int l, int r, int p) {
        push(l, r, p);
        if (l > e || r < s)
            return;
        if (l >= s && r <= e) {
            tree[p].apply(l, r, v);
            if (l != r) {
                tree[left(p)].add_lazy(v);
                tree[right(p)].add_lazy(v);
            }
            return;
        }
        int mid = (l + r) >> 1;
        update(s, e, v, l, mid, left(p));
        update(s, e, v, mid + 1, r, right(p));
        tree[p] = tree[left(p)] + tree[right(p)];
    }

    void insert(int i , int d , int l, int r, int p) {
        push(l, r, p);
        if (l > i || r < i)
            return;

        if (l == r) {
            tree[p].cnt[1] += d;
            tree[p].v += d;
            return;
        }

        int mid = (l + r) >> 1;
        insert(i , d , l, mid, left(p));
        insert(i , d , mid + 1, r, right(p));
        tree[p] = tree[left(p)] + tree[right(p)];
    }

    node query(int s , int e , int l , int r , int p) {
        push(l , r , p);
        if (l >= s && r <= e) return tree[p];
        int mid = (l + r) >> 1;
        if (e <= mid)
            return query(s , e , l , mid , left(p));
        else if (s > mid)
            return query(s , e , mid + 1 , r , right(p));
        return query(s , e , l , mid , left(p)) + query(s , e , mid + 1 , r , right(p));
    }

public:
    segtree(int _n) : n(_n + 1) {tree.resize(4 * n);}
    segtree(const vector<int> &v) : n(v.size()) {
        tree.resize(4 * n);
        build(0, n - 1, 1, v);
    }

    void update(int s, int e, long long v) {
        if (e < s)
            return;
        update(s, e, v, 0, n - 1, 1);
    }

    void insert(int i , int d) {
        insert(i , d , 0 , n - 1 , 1);
    }

    void print () {
        auto c = query(0 , 2 , 0, n - 1, 1);
        cout << c.v << endl;
        cout << c.cnt[1] << ' ' << c.cnt[2] << ' ' << c.cnt[3] << endl;
    }

    long long query(int s, int e) {
        if (e < s)
            return 0;
        return query(s, e, 0, n - 1, 1).v;
    }
} st(N);

vector<int> adj[N];
int in[N] , nxt;
long long ans;

void dfs (int node , int par) {
    in[node] = nxt++;
    st.update(0 , in[node] - 1 , 1);
    if (par != -1)
        st.insert(in[par] , 1);

    ans += st.query(0 , in[node] - 1);

    int f = nxt;
    for (int child : adj[node]) {
        if (child == par) continue;
        dfs(child , node);
    }

    if (par != -1)
        st.insert(in[par] , -1);

    st.update(0 , in[node] - 1 , -1);
    st.insert(in[node] , 1);
    st.update(f , nxt - 1 , 1);
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.in" , "r" , stdin);
#endif
    int n;
    cin >> n >> k;

    for (int i = 0 ;i < n - 1 ;i++) {
        int u , v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    dfs(1 , -1);

    cout << ans;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Rick From Rickland
 */

#pragma GCC optimize("O2")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef pair<string, string> pss;
typedef pair<pll, ll> ppl;
typedef pair<ll, pll> plp;
typedef pair<pll, pll> ppp;

template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//order_of_key , find_by_order
#define sz(x)                       (ll) x.size()
#define jjoin(x)                    for (auto i : x) cout << i << endl;
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define sep                         ' '
#define error(x)                    cerr << #x << " = " << x << endl
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define set_random                  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define kill(x)                     return cout << x << endl , 0;
#define Hkill(x)                    cout << x << endl; exit(0);
#define endl                        '\n'

ll poww(ll a, ll b, ll md) {
    return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));
}

const ll MAXN = 2e6 + 10;
const ll MAXA = 101;
const ll INF = 8e18;
const ll MOD = 998244353; //1e9 + 7
const ld PI = 4 * atan((ld) 1);
ll cnt[MAXN][5], Sz[MAXN], ans = 0, n, k;
vector<ll> adj[MAXN];

void dfs(ll v, ll par = -1) {
    cnt[v][0] = 1;
    Sz[v] = 1;
    for (ll u : adj[v]) {
        if (u == par) continue;
        dfs(u, v);
        Sz[v] += Sz[u];
        for (ll i = 0; i < k; i++) {
            for (ll j = 0; j < k; j++) {
                if (i + j >= k) ans += 2 * cnt[v][i] * cnt[u][j];
                else ans += cnt[v][i] * cnt[u][j];
            }
        }
        for (ll i = 0; i < k ; i++) {
            cnt[v][i] += cnt[u][(i - 1 + k) % k];
        }
        ans += (n - Sz[v]) * cnt[u][k - 1];
    }
}

int main() {
    fast_io;
    set_random;
    cin >> n >> k;
    for (ll i = 1; i <= n - 1; i++) {
        ll u, v;
        cin >> u >> v;
        adj[v].push_back(u), adj[u].push_back(v);
    }
    dfs(1);
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
const int N=200005;
using namespace std;
typedef long long ll;
vector<int>g[N];
ll n,k,sum=0;
ll S[N][5],sz[N];
void dfs(int u,int pr,int dep){
    sz[u]=S[u][dep%k]=1;
    for(auto v:g[u]){
        if(v==pr)continue;
        dfs(v,u,dep+1);
        for(int i=0;i<k;i++){
            for(int j=0;j<k;j++){
                ll dist=((i+j-2*dep)%k+k)%k;
                ll e=((k-dist)%k+k)%k;
                sum+=e*S[u][i]*S[v][j];
            }
        }
        for(int i=0;i<k;i++)S[u][i]+=S[v][i];
        sz[u]+=sz[v];
    }
    sum+=sz[u]*(n-sz[u]);
}
int main()
{ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
    cin>>n>>k;
    for(int i=1;i<n;i++){
        int u,v;cin>>u>>v;g[u].push_back(v);g[v].push_back(u);
    }
    dfs(1,-1,0);
    cout<<sum/k<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define eb emplace_back
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);

const int N=2e5+5;
bool stat[N];
vector <int> g[N];
int n,k,depth[N],par[N],ans=0;
pair <int,int> dp[N][6];

void inline input() {
	cin>>n>>k;
	for(int i=0;i<n-1;i++) {
		int x,y;
		cin>>x>>y;
		g[x].eb(y);
		g[y].eb(x);
	}
}
void dfs(int u,int p ){
	depth[u]=depth[p]+1;
	par[u]=p;
	for(auto it:g[u]) {
		if(it==p) continue;
		dfs(it,u);
		for(int j=0;j<k;j++) {
			dp[u][j].f+=dp[it][j].f;
			dp[u][j].s+=dp[it][j].s;
		}
	}
	dp[u][depth[u]%k].f++;
	dp[u][depth[u]%k].s+=depth[u];
}
void bfs() {
	memset(stat,false,sizeof(stat));
	queue <int> q;
	q.push(1);
	while(!q.empty()) {
		int u=q.front();
		stat[u]=true;
		q.pop();
		if(u==1) {
			for(int i=0;i<k;i++) {
				//cout<<dp[u][i].f<<endl;
				ans+=(dp[u][i].s)/k;
				ans-=(dp[u][i].f*i)/k;
				if(i!=0) ans+=dp[u][i].f;
			}
			//cout<<ans<<endl;
		}
		else {
			pair<int,int> tmp[k];
			for(int i=0;i<k;i++) {
				tmp[(i+1)%k].f=dp[par[u]][i].f-dp[u][(i+(depth[par[u]]%k)+k)%k].f;
				tmp[(i+1)%k].s=dp[par[u]][i].s-dp[u][(i+(depth[par[u]]%k)+k)%k].s+(dp[u][(i+(depth[par[u]]%k)+k)%k].f*depth[par[u]]);
			}

			// if(u==3) {
			// 	for(int i=0;i<k;i++) cout<<tmp[i].s<<" ";
			// }
			for(int i=0;i<k;i++) tmp[i].s+=tmp[i].f;
			for(int i=0;i<k;i++) {
				tmp[(i-(depth[u]%k)+k)%k].f+=dp[u][i].f;
				tmp[(i-(depth[u]%k)+k)%k].s+=dp[u][i].s-dp[u][i].f*depth[u];
			}
			for(int i=0;i<k;i++) {
				ans+=(tmp[i].s)/k;
				ans-=(tmp[i].f*i)/k;
				if(i!=0)ans+=tmp[i].f;
			}
			for(int i=0;i<k;i++) dp[u][i]=tmp[i];
		//	cout<<u<<" "<<ans<<endl;
		}
		for(auto it:g[u]) {
			if(!stat[it]) {
				q.push(it);
				stat[it]=true;
			}
		}
	}
}
signed main() {
	IOS
	memset(dp,0,sizeof(dp));
	input();
	depth[0]=-1;
	dfs(1,0);
	bfs();
	cout<<ans/2;
	return 0;
}