#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int N = 100010,M = 2 * N;
int h[N],e[M],ne[M],w[M],idx;
int n, k;
int s, p;
int dist[N],father[N];
int d[N];
void add(int a,int b,int c){
e[idx] = b,w[idx] = c,ne[idx] = h[a],h[a] = idx ++ ;
}
int bfs(int u){
memset(dist,0x3f,sizeof dist);
father[u] = -1;
dist[u] = 0;
queue<int> q;
q.push(u);
while(q.size()){
auto t = q.front();
q.pop();
for(int i = h[t];~i;i = ne[i]){
int j = e[i];
if(dist[j] == 0x3f3f3f3f){
dist[j] = dist[t] + w[i];
father[j] = i;
q.push(j);
}
}
}
int x, y;
for(x = 1,y = 1;x <= n;x ++)
if(dist[x] > dist[y]) y = x;
return y;
}
int ans = 0;
int dp(int u,int fa){
for(int i = h[u];~i;i = ne[i]){
int j = e[i];
if(j == fa) continue;
dp(j,u);
ans = max(ans,d[u] + d[j] + w[i]);
d[u] = max(d[u],d[j] + w[i]);
}
return ans;
}
void change(){
for( ; father[p] != -1;p = e[father[p] ^ 1]) w[father[p]] = w[father[p] ^ 1] = -1;
}
int get(){
s = bfs(1);
p = bfs(s);
return dist[p];
}
int main(){
memset(h,-1,sizeof h);
scanf("%d%d",&n,&k);
for(int i = 1;i < n; i++){
int a, b;
scanf("%d%d",&a,&b);
add(a,b,1),add(b,a,1);
}
int l1 = get();
if(k == 1) cout << 2 * (n - 1) - l1 + 1 << endl;
else{
change();
int l2 = dp(1,0);
cout << 2 * (n - 1) - l1 - l2 + 2;
}
return 0;
}