#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>

using namespace std;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); ++e)
typedef long long LL;
typedef pair<int, int> PII;

int n, m, k;
char s[1003][1003];
int hsum[1003][1003], vsum[1003][1003];
int sum[1003][1003] = {};
bool used[1003][1003] = {};
int y[100], x[100], yy[100], xx[100];
int cnt = 0;
LL ans = 0;

void solve(int x, int y, int xx, int yy) {
    int cur = sum[yy][xx] - sum[y - 1][xx] - sum[yy][x - 1] + sum[y - 1][x - 1];
    int lo = xx, hi = m, mid;
    while (lo < hi) {
        mid = (lo + hi + 1) >> 1;
        if (sum[yy][mid] - sum[y - 1][mid] - sum[yy][x - 1] + sum[y - 1][x - 1] == cur)
            lo = mid;
        else
            hi = mid - 1;
    }
    int hor1 = lo - xx + 1;
    lo = 1, hi = x;
    while (lo < hi) {
        mid = (lo + hi) >> 1;
        if (sum[yy][xx] - sum[y - 1][xx] - sum[yy][mid - 1] + sum[y - 1][mid - 1] == cur)
            hi = mid;
        else
            lo = mid + 1;
    }
    int hor2 = x - lo + 1;
    lo = yy, hi = n;
    while (lo < hi) {
        mid = (lo + hi + 1) >> 1;
        if (sum[mid][xx] - sum[mid][x - 1] - sum[y - 1][xx] + sum[y - 1][x - 1] == cur)
            lo = mid;
        else
            hi = mid - 1;
    }
    int ver1 = lo - yy + 1;
    lo = 1, hi = y;
    while (lo < hi) {
        mid = (lo + hi) >> 1;
        if (sum[yy][xx] - sum[yy][x - 1] - sum[mid - 1][xx] + sum[mid - 1][x - 1] == cur)
            hi = mid;
        else
            lo = mid + 1;
    }
    int ver2 = y - lo + 1;
    if (hor1 * hor2 > ver1 * ver2) {
        for (int i = y; i >= 1; --i) {
            if (i < y && hsum[i][xx] - hsum[i][x - 1] > 0) break;
            for (int ii = yy; ii <= n; ++ii) {
                if (ii > yy && hsum[ii][xx] - hsum[ii][x - 1] > 0) break;
                int cnt1, cnt2;
                lo = xx, hi = m;
                while (lo < hi) {
                    mid = (lo + hi + 1) >> 1;
                    if (sum[ii][mid] - sum[i - 1][mid] - sum[ii][x - 1] + sum[i - 1][x - 1] == cur)
                        lo = mid;
                    else
                        hi = mid - 1;
                }
                cnt1 = lo - xx + 1;
                lo = 1, hi = x;
                while (lo < hi) {
                    mid = (lo + hi) >> 1;
                    if (sum[ii][xx] - sum[i - 1][xx] - sum[ii][mid - 1] + sum[i - 1][mid - 1] == cur)
                        hi = mid;
                    else
                        lo = mid + 1;
                }
                cnt2 = x - lo + 1;
                ans += cnt1 * cnt2;
            }
        }
    } else {
        for (int i = x; i >= 1; --i) {
            if (i < x && vsum[i][yy] - vsum[i][y - 1] > 0) break;
            for (int ii = xx; ii <= m; ++ii) {
                if (ii > xx && vsum[ii][yy] - vsum[ii][y - 1] > 0) break;
                int cnt1, cnt2;
                lo = yy, hi = n;
                while (lo < hi) {
                    mid = (lo + hi + 1) >> 1;
                    if (sum[mid][ii] - sum[mid][i - 1] - sum[y - 1][ii] + sum[y - 1][i - 1] == cur)
                        lo = mid;
                    else
                        hi = mid - 1;
                }
                cnt1 = lo - yy + 1;
                lo = 1, hi = y;
                while (lo < hi) {
                    mid = (lo + hi) >> 1;
                    if (sum[yy][ii] - sum[yy][i - 1] - sum[mid - 1][ii] + sum[mid - 1][i - 1] == cur)
                        hi = mid;
                    else
                        lo = mid + 1;
                }
                cnt2 = y - lo + 1;
                ans += cnt1 * cnt2;
            }
        }
    }
}

int main() {
    scanf("%d%d%d", &n, &m, &k);
    REP(i, n) scanf("%s", s[i + 1] + 1);
    REP(i, n + 2) s[i][0] = s[i][m + 1] = '.';
    REP(i, m + 2) s[0][i] = s[n + 1][i] = '.';
    for (int i = 1; i <= n; ++i) {
        hsum[i][0] = 0;
        for (int j = 1; j <= m; ++j) {
            hsum[i][j] = hsum[i][j - 1] + (s[i][j] == '*');
        }
    }
    for (int i = 1; i <= m; ++i) {
        vsum[i][0] = 0;
        for (int j = 1; j <= n; ++j) {
            vsum[i][j] = vsum[i][j - 1] + (s[j][i] == '*');
        }
    }
    for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) {
        sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + (s[i][j] == '*');
    }
    for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) if (!used[i][j] && s[i][j] == '*') {
        int ii = i, jj = j;
        while (ii < n && s[ii + 1][j] == '*') ++ii;
        while (jj < m && s[i][jj + 1] == '*') ++jj;
        y[cnt] = i;
        x[cnt] = j;
        yy[cnt] = ii;
        xx[cnt] = jj;
        ++cnt;
        for (int p = i; p <= ii; ++p) for (int q = j; q <= jj; ++q) {
            used[p][q] = true;
        }
    }
    assert(cnt == k);
    REP(i, k) for (int j = i + 1; j < k; ++j) for (int q = j + 1; q < k; ++q) {
        int cx = min(min(x[i], x[j]), x[q]);
        int cxx = max(max(xx[i], xx[j]), xx[q]);
        int cy = min(min(y[i], y[j]), y[q]);
        int cyy = max(max(yy[i], yy[j]), yy[q]);
        bool ok = true;
        REP(p, k) if (p != i && p != j && p != q) {
            if (x[p] <= cxx && cx <= xx[p] && y[p] <= cyy && cy <= yy[p]) {
                ok = false;
                break;
            }
        }
        if (!ok) continue;
        solve(cx, cy, cxx, cyy);
    }
    REP(i, k) for (int j = i + 1; j < k; ++j) {
        int cx = min(x[i], x[j]);
        int cxx = max(xx[i], xx[j]);
        int cy = min(y[i], y[j]);
        int cyy = max(yy[i], yy[j]);
        bool ok = true;
        REP(p, k) if (p != i && p != j) {
            if (x[p] <= cxx && cx <= xx[p] && y[p] <= cyy && cy <= yy[p]) {
                ok = false;
                break;
            }
        }
        if (!ok) continue;
        solve(cx, cy, cxx, cyy);
    }
    REP(i, k)
        solve(x[i], y[i], xx[i], yy[i]);
    cout << ans << endl;
    return 0;
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>

using namespace std;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); ++e)
typedef long long LL;
typedef pair<int, int> PII;

int n, m, k;
char s[1003][1003];
int hsum[1003][1003];
int sum[1003][1003] = {};
bool used[1003][1003] = {};
int y[100], x[100], yy[100], xx[100];
int cnt = 0;
LL ans = 0;

void solve(int x, int y, int xx, int yy) {
    int cur = sum[yy][xx] - sum[y - 1][xx] - sum[yy][x - 1] + sum[y - 1][x - 1];
    for (int i = y; i >= 1; --i) {
        if (i < y && hsum[i][xx] - hsum[i][x - 1] > 0) break;
        for (int ii = yy; ii <= n; ++ii) {
            if (ii > yy && hsum[ii][xx] - hsum[ii][x - 1] > 0) break;
            int cnt1, cnt2;
            int lo = xx, hi = m, mid;
            while (lo < hi) {
                mid = (lo + hi + 1) >> 1;
                if (sum[ii][mid] - sum[i - 1][mid] - sum[ii][x - 1] + sum[i - 1][x - 1] == cur)
                    lo = mid;
                else
                    hi = mid - 1;
            }
            cnt1 = lo - xx + 1;
            lo = 1, hi = x;
            while (lo < hi) {
                mid = (lo + hi) >> 1;
                if (sum[ii][xx] - sum[i - 1][xx] - sum[ii][mid - 1] + sum[i - 1][mid - 1] == cur)
                    hi = mid;
                else
                    lo = mid + 1;
            }
            cnt2 = x - lo + 1;
            ans += cnt1 * cnt2;
        }
    }
}

int main() {
    scanf("%d%d%d", &n, &m, &k);
    REP(i, n) scanf("%s", s[i + 1] + 1);
    REP(i, n + 2) s[i][0] = s[i][m + 1] = '.';
    REP(i, m + 2) s[0][i] = s[n + 1][i] = '.';
    for (int i = 1; i <= n; ++i) {
        hsum[i][0] = 0;
        for (int j = 1; j <= m; ++j) {
            hsum[i][j] = hsum[i][j - 1] + (s[i][j] == '*');
        }
    }
    for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) {
        sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + (s[i][j] == '*');
    }
    for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) if (!used[i][j] && s[i][j] == '*') {
        int ii = i, jj = j;
        while (ii < n && s[ii + 1][j] == '*') ++ii;
        while (jj < m && s[i][jj + 1] == '*') ++jj;
        y[cnt] = i;
        x[cnt] = j;
        yy[cnt] = ii;
        xx[cnt] = jj;
        ++cnt;
        for (int p = i; p <= ii; ++p) for (int q = j; q <= jj; ++q) {
            used[p][q] = true;
        }
    }
    assert(cnt == k);
    REP(i, k) for (int j = i + 1; j < k; ++j) for (int q = j + 1; q < k; ++q) {
        int cx = min(min(x[i], x[j]), x[q]);
        int cxx = max(max(xx[i], xx[j]), xx[q]);
        int cy = min(min(y[i], y[j]), y[q]);
        int cyy = max(max(yy[i], yy[j]), yy[q]);
        bool ok = true;
        REP(p, k) if (p != i && p != j && p != q) {
            if (x[p] <= cxx && cx <= xx[p] && y[p] <= cyy && cy <= yy[p]) {
                ok = false;
                break;
            }
        }
        if (!ok) continue;
        solve(cx, cy, cxx, cyy);
    }
    REP(i, k) for (int j = i + 1; j < k; ++j) {
        int cx = min(x[i], x[j]);
        int cxx = max(xx[i], xx[j]);
        int cy = min(y[i], y[j]);
        int cyy = max(yy[i], yy[j]);
        bool ok = true;
        REP(p, k) if (p != i && p != j) {
            if (x[p] <= cxx && cx <= xx[p] && y[p] <= cyy && cy <= yy[p]) {
                ok = false;
                break;
            }
        }
        if (!ok) continue;
        solve(cx, cy, cxx, cyy);
    }
    REP(i, k)
        solve(x[i], y[i], xx[i], yy[i]);
    cout << ans << endl;
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#define mk make_pair
#define pb push_back
using namespace std;
struct ppp{
 int x1,x2,y1,y2;
 void mk(int x1_,int x2_,int y1_,int y2_){
  x1=x1_; x2=x2_; y1=y1_; y2=y2_;
 }
}a[120];
long long ans;
int n,m,i,j,k,x,y,l,r,P[220],tot,Pn,u,d;
char c[1010][1010];
int Find(int x){
 return lower_bound(P+1,P+Pn+1,x)-P;
}
int main(){
 scanf("%d%d%*d",&n,&m);
 for(i=1;i<=n;++i)scanf("%s",c[i]+1);
 for(i=1;i<=n;++i)
  for(j=1;j<=m;++j)if(c[i][j]=='*'){
   k=j;
   while(c[i][k]=='*')k++; k--;
   l=i;
   while(c[l][j]=='*')l++; l--;
   for(x=i;x<=l;++x)
    for(y=j;y<=k;++y)c[x][y]='.';
   a[++tot].mk(i,l,j,k);
   P[++Pn]=i; P[++Pn]=l;
  }
 P[++Pn]=0; P[++Pn]=n+1;
 sort(P+1,P+Pn+1);
 Pn=unique(P+1,P+Pn+1)-P-1;
 for(i=1;i<=tot;++i)a[i].x1=Find(a[i].x1),a[i].x2=Find(a[i].x2);
 for(u=2;u<Pn;++u){
  for(d=u;d<Pn;++d){
   long long tmp=0;
   vector<pair<int,int> >Q;
   for(i=1;i<=tot;++i){
    if(a[i].x2<u || a[i].x1>d)continue;
    if(a[i].x1>=u && a[i].x2<=d)Q.pb(mk(a[i].y1,-1)),Q.pb(mk(a[i].y2,1));
    else Q.pb(mk(a[i].y1,-100)),Q.pb(mk(a[i].y2,100));
   }
   sort(Q.begin(),Q.end());
   int num=Q.size(),C,suml,sumr,cc;
   Q.push_back(mk(m+1,0));
   for(int C=1;C<=3;++C){
    suml=sumr=0; cc=0;
    for(l=0,r=0;r<num;++r){
     sumr+=Q[r].second;
     cc+=max(0,Q[r].second);
     if(!sumr){
      while(suml || cc>C){
       suml-=Q[l].second;
       cc-=max(0,Q[l].second);
       l++;
      }
      if(!suml && cc==C)tmp+=(Q[r+1].first-Q[r].first)*(Q[l].first-(!l?0:Q[l-1].first));
     }
    }
   }
   ans+=tmp*(P[u]-P[u-1])*(P[d+1]-P[d]);
  }
 }
 printf("%I64d\n",ans);
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#define mk make_pair
#define pb push_back
using namespace std;
struct ppp {
  int x1, x2, y1, y2;
  void mk(int x1_, int x2_, int y1_, int y2_) {
    x1 = x1_;
    x2 = x2_;
    y1 = y1_;
    y2 = y2_;
  }
} a[120];
long long ans;
int n, m, i, j, k, x, y, l, r, P[220], tot, Pn, u, d;
char c[1010][1010];
int Find(int x) { return lower_bound(P + 1, P + Pn + 1, x) - P; }
int main() {
  scanf("%d%d%*d", &n, &m);
  for (i = 1; i <= n; ++i) scanf("%s", c[i] + 1);
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j)
      if (c[i][j] == '*') {
        k = j;
        while (c[i][k] == '*') k++;
        k--;
        l = i;
        while (c[l][j] == '*') l++;
        l--;
        for (x = i; x <= l; ++x)
          for (y = j; y <= k; ++y) c[x][y] = '.';
        a[++tot].mk(i, l, j, k);
        P[++Pn] = i;
        P[++Pn] = l;
      }
  P[++Pn] = 0;
  P[++Pn] = n + 1;
  sort(P + 1, P + Pn + 1);
  Pn = unique(P + 1, P + Pn + 1) - P - 1;
  for (i = 1; i <= tot; ++i) a[i].x1 = Find(a[i].x1), a[i].x2 = Find(a[i].x2);
  for (u = 2; u < Pn; ++u) {
    for (d = u; d < Pn; ++d) {
      long long tmp = 0;
      vector<pair<int, int> > Q;
      for (i = 1; i <= tot; ++i) {
        if (a[i].x2 < u || a[i].x1 > d) continue;
        if (a[i].x1 >= u && a[i].x2 <= d)
          Q.pb(mk(a[i].y1, -1)), Q.pb(mk(a[i].y2, 1));
        else
          Q.pb(mk(a[i].y1, -100)), Q.pb(mk(a[i].y2, 100));
      }
      sort(Q.begin(), Q.end());
      int num = Q.size(), C, suml, sumr, cc;
      Q.push_back(mk(m + 1, 0));
      for (int C = 1; C <= 3; ++C) {
        suml = sumr = 0;
        cc = 0;
        for (l = 0, r = 0; r < num; ++r) {
          sumr += Q[r].second;
          cc += max(0, Q[r].second);
          if (!sumr) {
            while (suml || cc > C) {
              suml -= Q[l].second;
              cc -= max(0, Q[l].second);
              l++;
            }
            if (!suml && cc == C)
              tmp += (Q[r + 1].first - Q[r].first) *
                     (Q[l].first - (!l ? 0 : Q[l - 1].first));
          }
        }
      }
      ans += tmp * (P[u] - P[u - 1]) * (P[d + 1] - P[d]);
    }
  }
  printf("%I64d\n", ans);
}


//lxrtxdy!
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#define mk make_pair
#define pb push_back
using namespace std;
struct ppp{
 int x1,x2,y1,y2;
 void mk(int x1_,int x2_,int y1_,int y2_){
  x1=x1_; x2=x2_; y1=y1_; y2=y2_;
 }
}a[120];
long long ans;
int n,m,i,j,k,x,y,l,r,P[220],tot,Pn,u,d;
char c[1010][1010];
int Find(int x){
 return lower_bound(P+1,P+Pn+1,x)-P;
}
int main(){
 scanf("%d%d%*d",&n,&m);
 for(i=1;i<=n;++i)scanf("%s",c[i]+1);
 for(i=1;i<=n;++i)
  for(j=1;j<=m;++j)if(c[i][j]=='*'){
   k=j;
   while(c[i][k]=='*')k++; k--;
   l=i;
   while(c[l][j]=='*')l++; l--;
   for(x=i;x<=l;++x)
    for(y=j;y<=k;++y)c[x][y]='.';
   a[++tot].mk(i,l,j,k);
   P[++Pn]=i; P[++Pn]=l;
  }
 P[++Pn]=0; P[++Pn]=n+1;
 sort(P+1,P+Pn+1);
 Pn=unique(P+1,P+Pn+1)-P-1;
 for(i=1;i<=tot;++i)a[i].x1=Find(a[i].x1),a[i].x2=Find(a[i].x2);
 for(u=2;u<Pn;++u){
  for(d=u;d<Pn;++d){
   long long tmp=0;
   vector<pair<int,int> >Q;
   for(i=1;i<=tot;++i){
    if(a[i].x2<u || a[i].x1>d)continue;
    if(a[i].x1>=u && a[i].x2<=d)Q.pb(mk(a[i].y1,-1)),Q.pb(mk(a[i].y2,1));
    else Q.pb(mk(a[i].y1,-100)),Q.pb(mk(a[i].y2,100));
   }
   sort(Q.begin(),Q.end());
   int num=Q.size(),C,suml,sumr,cc;
   Q.push_back(mk(m+1,0));
   for(int C=1;C<=3;++C){
    suml=sumr=0; cc=0;
    for(l=0,r=0;r<num;++r){
     sumr+=Q[r].second;
     cc+=max(0,Q[r].second);
     if(!sumr){
      while(suml || cc>C){
       suml-=Q[l].second;
       cc-=max(0,Q[l].second);
       l++;
      }
      if(!suml && cc==C)tmp+=(Q[r+1].first-Q[r].first)*(Q[l].first-(!l?0:Q[l-1].first));
     }
    }
   }
   ans+=tmp*(P[u]-P[u-1])*(P[d+1]-P[d]);
  }
 }
 printf("%I64d\n",ans);
}//hellobig_tq
//orzlxr!//hello


//lxrtxdy!
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#define mk make_pair
#define pb push_back
using namespace std;
struct ppp{
 int x1,x2,y1,y2;
 void mk(int x1_,int x2_,int y1_,int y2_){
  x1=x1_; x2=x2_; y1=y1_; y2=y2_;
 }
}a[120];
long long ans;
int n,m,i,j,k,x,y,l,r,P[220],tot,Pn,u,d;
char c[1010][1010];
int Find(int x){
 return lower_bound(P+1,P+Pn+1,x)-P;
}
int main(){
 scanf("%d%d%*d",&n,&m);
 for(i=1;i<=n;++i)scanf("%s",c[i]+1);
 for(i=1;i<=n;++i)
  for(j=1;j<=m;++j)if(c[i][j]=='*'){
   k=j;
   while(c[i][k]=='*')k++; k--;
   l=i;
   while(c[l][j]=='*')l++; l--;
   for(x=i;x<=l;++x)
    for(y=j;y<=k;++y)c[x][y]='.';
   a[++tot].mk(i,l,j,k);
   P[++Pn]=i; P[++Pn]=l;
  }
 P[++Pn]=0; P[++Pn]=n+1;
 sort(P+1,P+Pn+1);
 Pn=unique(P+1,P+Pn+1)-P-1;
 for(i=1;i<=tot;++i)a[i].x1=Find(a[i].x1),a[i].x2=Find(a[i].x2);
 for(u=2;u<Pn;++u){
  for(d=u;d<Pn;++d){
   long long tmp=0;
   vector<pair<int,int> >Q;
   for(i=1;i<=tot;++i){
    if(a[i].x2<u || a[i].x1>d)continue;
    if(a[i].x1>=u && a[i].x2<=d)Q.pb(mk(a[i].y1,-1)),Q.pb(mk(a[i].y2,1));
    else Q.pb(mk(a[i].y1,-100)),Q.pb(mk(a[i].y2,100));
   }
   sort(Q.begin(),Q.end());
   int num=Q.size(),C,suml,sumr,cc;
   Q.push_back(mk(m+1,0));
   for(int C=1;C<=3;++C){
    suml=sumr=0; cc=0;
    for(l=0,r=0;r<num;++r){
     sumr+=Q[r].second;
     cc+=max(0,Q[r].second);
     if(!sumr){
      while(suml || cc>C){
       suml-=Q[l].second;
       cc-=max(0,Q[l].second);
       l++;
      }
      if(!suml && cc==C)tmp+=(Q[r+1].first-Q[r].first)*(Q[l].first-(!l?0:Q[l-1].first));
     }
    }
   }
   ans+=tmp*(P[u]-P[u-1])*(P[d+1]-P[d]);
  }
 }
 printf("%I64d\n",ans);
}//hellobig_tq
//orzlxr!//hello//hello


//lxrtxdy!
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#define mk make_pair
#define pb push_back
using namespace std;
struct ppp{
 int x1,x2,y1,y2;
 void mk(int x1_,int x2_,int y1_,int y2_){
  x1=x1_; x2=x2_; y1=y1_; y2=y2_;
 }
}a[120];
long long ans;
int n,m,i,j,k,x,y,l,r,P[220],tot,Pn,u,d;
char c[1010][1010];
int Find(int x){
 return lower_bound(P+1,P+Pn+1,x)-P;
}
int main(){
 scanf("%d%d%*d",&n,&m);
 for(i=1;i<=n;++i)scanf("%s",c[i]+1);
 for(i=1;i<=n;++i)
  for(j=1;j<=m;++j)if(c[i][j]=='*'){
   k=j;
   while(c[i][k]=='*')k++; k--;
   l=i;
   while(c[l][j]=='*')l++; l--;
   for(x=i;x<=l;++x)
    for(y=j;y<=k;++y)c[x][y]='.';
   a[++tot].mk(i,l,j,k);
   P[++Pn]=i; P[++Pn]=l;
  }
 P[++Pn]=0; P[++Pn]=n+1;
 sort(P+1,P+Pn+1);
 Pn=unique(P+1,P+Pn+1)-P-1;
 for(i=1;i<=tot;++i)a[i].x1=Find(a[i].x1),a[i].x2=Find(a[i].x2);
 for(u=2;u<Pn;++u){
  for(d=u;d<Pn;++d){
   long long tmp=0;
   vector<pair<int,int> >Q;
   for(i=1;i<=tot;++i){
    if(a[i].x2<u || a[i].x1>d)continue;
    if(a[i].x1>=u && a[i].x2<=d)Q.pb(mk(a[i].y1,-1)),Q.pb(mk(a[i].y2,1));
    else Q.pb(mk(a[i].y1,-100)),Q.pb(mk(a[i].y2,100));
   }
   sort(Q.begin(),Q.end());
   int num=Q.size(),C,suml,sumr,cc;
   Q.push_back(mk(m+1,0));
   for(int C=1;C<=3;++C){
    suml=sumr=0; cc=0;
    for(l=0,r=0;r<num;++r){
     sumr+=Q[r].second;
     cc+=max(0,Q[r].second);
     if(!sumr){
      while(suml || cc>C){
       suml-=Q[l].second;
       cc-=max(0,Q[l].second);
       l++;
      }
      if(!suml && cc==C)tmp+=(Q[r+1].first-Q[r].first)*(Q[l].first-(!l?0:Q[l-1].first));
     }
    }
   }
   ans+=tmp*(P[u]-P[u-1])*(P[d+1]-P[d]);
  }
 }
 printf("%I64d\n",ans);
}//hellobig_tq
//orzlxr!


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#define mk make_pair
#define pb push_back
using namespace std;
struct ppp{
 int x1,x2,y1,y2;
 void mk(int x1_,int x2_,int y1_,int y2_){
  x1=x1_; x2=x2_; y1=y1_; y2=y2_;
 }
}a[120];
long long ans;
int n,m,i,j,k,x,y,l,r,P[220],tot,Pn,u,d;
char c[1010][1010];
int Find(int x){
 return lower_bound(P+1,P+Pn+1,x)-P;
}
int main(){
 scanf("%d%d%*d",&n,&m);
 for(i=1;i<=n;++i)scanf("%s",c[i]+1);
 for(i=1;i<=n;++i)
  for(j=1;j<=m;++j)if(c[i][j]=='*'){
   k=j;
   while(c[i][k]=='*')k++; k--;
   l=i;
   while(c[l][j]=='*')l++; l--;
   for(x=i;x<=l;++x)
    for(y=j;y<=k;++y)c[x][y]='.';
   a[++tot].mk(i,l,j,k);
   P[++Pn]=i; P[++Pn]=l;
  }
 P[++Pn]=0; P[++Pn]=n+1;
 sort(P+1,P+Pn+1);
 Pn=unique(P+1,P+Pn+1)-P-1;
 for(i=1;i<=tot;++i)a[i].x1=Find(a[i].x1),a[i].x2=Find(a[i].x2);
 for(u=2;u<Pn;++u){
  for(d=u;d<Pn;++d){
   long long tmp=0;
   vector<pair<int,int> >Q;
   for(i=1;i<=tot;++i){
    if(a[i].x2<u || a[i].x1>d)continue;
    if(a[i].x1>=u && a[i].x2<=d)Q.pb(mk(a[i].y1,-1)),Q.pb(mk(a[i].y2,1));
    else Q.pb(mk(a[i].y1,-100)),Q.pb(mk(a[i].y2,100));
   }
   sort(Q.begin(),Q.end());
   int num=Q.size(),C,suml,sumr,cc;
   Q.push_back(mk(m+1,0));
   for(int C=1;C<=3;++C){
    suml=sumr=0; cc=0;
    for(l=0,r=0;r<num;++r){
     sumr+=Q[r].second;
     cc+=max(0,Q[r].second);
     if(!sumr){
      while(suml || cc>C){
       suml-=Q[l].second;
       cc-=max(0,Q[l].second);
       l++;
      }
      if(!suml && cc==C)tmp+=(Q[r+1].first-Q[r].first)*(Q[l].first-(!l?0:Q[l-1].first));
     }
    }
   }
   ans+=tmp*(P[u]-P[u-1])*(P[d+1]-P[d]);
  }
 }
 printf("%I64d\n",ans);
}//hellobig_tq


#include <bits/stdc++.h>
using  namespace std;
vector <pair<int,int>> ans;
vector <vector <int>> g;
int mx;

void dfs(int v, int t, int p=-1)
{
    int need=t-1;
    ans.push_back({v,t});
    for (int i:g[v])
    {
        if (i==p) continue;
        if (t==mx) t = mx-g[v].size(), ans.push_back({v,t});
        dfs(i,t+1,v);
        t++;
        ans.push_back({v,t});
    }
    if(p!=-1)
        if(t!=need)
            ans.push_back({v,need});
}

int main()
{

  int n;
  cin >> n;
  g.resize(n);
  for (int i=0; i<n-1; i++)
  {
      int x,y; cin >> x >> y;
      x--; y--;
      g[x].push_back(y);
      g[y].push_back(x);
  }

  mx=0;
  for (int i=0; i<n; i++)
    mx= max<int>(mx,g[i].size());

  dfs(1,0);
  cout << ans.size() << "\n";
  for (auto x: ans) cout <<x.first+1 << " " <<x.second << "\n";

}

/*
10 10
0101111
0000000
1111011
1011011
1011011
1111011
0010010
1010010
1101111
0000000

*/


#include <bits/stdc++.h>
using  namespace std;
vector <pair<int,int>> ans;
vector <vector <int>> g;
int mx;

void dfs(int v, int t, int p=-1)
{
    int need=t-1;
    ans.push_back({v,t});
    for (int i:g[v])
    {
        if (i==p) continue;
        if (t==mx) t = mx-g[v].size(), ans.push_back({v,t});
        dfs(i,t+1,v);
        t++;
        ans.push_back({v,t});
    }
    if(p!=-1)
        if(t!=need)
            ans.push_back({v,need});
}

int main()
{

  int n;
  cin >> n;
  g.resize(n);
  for (int i=0; i<n-1; i++)
  {
      int x,y; cin >> x >> y;
      x--; y--;
      g[x].push_back(y);
      g[y].push_back(x);
  }

  mx=0;
  for (int i=0; i<n; i++)
    mx= max<int>(mx,g[i].size());

  dfs(0,0);
  cout << ans.size() << "\n";
  for (auto x: ans) cout <<x.first+1 << " " <<x.second << "\n";

}

/*
10 10
0101111
0000000
1111011
1011011
1011011
1111011
0010010
1010010
1101111
0000000

*/


#include <bits/stdc++.h>
using  namespace std;
vector <pair<int,int>> ans;
vector <vector <int>> g;
int mx;

void dfs(int v, int t, int p=-1)
{
    int need=t-1;
    ans.push_back({v,t});
    for (int i:g[v])
    {
        if (i==p) continue;
        if (t==mx) t = mx-g[v].size(), ans.push_back({v,t});
        dfs(i,t+1,v);
        t++;
        ans.push_back({v,t});
    }
    if(p!=-1)
        if(t!=need)
            ans.push_back({v,need});
}

int main()
{

  int n;
  cin >> n;
  g.resize(n);
  for (int i=0; i<n-1; i++)
  {
      int x,y; cin >> x >> y;
      x--; y--;
      g[x].push_back(y);
      g[y].push_back(x);
  }

  mx=0;
  for (int i=0; i<n; i++)
    mx= max<int>(mx,g[i].size());

  dfs(1,0);
  cout << ans.size() << "\n";
  for (auto x: ans) cout <<x.first << " " <<x.second << "\n";

}

/*
10 10
0101111
0000000
1111011
1011011
1011011
1111011
0010010
1010010
1101111
0000000

*/


#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define mp make_pair
#define reg register int
#define pii pair<int, int>
#define fo(i, a, b) for (reg i = a; i <= b; i++)
#define fd(i, a, b) for (reg i = a; i >= b; i--)
#define bp __builtin_popcount
#define cr const reg&
using namespace std;
const int mod = 998244353, N = 2e5 + 1;

I int _max(cr x, cr y) { return x > y ? x : y; }
I int _min(cr x, cr y) { return x < y ? x : y; }
I int read() {
    reg x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
    return x * f;
}
I void ptt(LL x) {
    if (x >= 10)
        ptt(x / 10);
    putchar(x % 10 + '0');
}
I void put(LL x) { x < 0 ? putchar('-'), ptt(-x) : ptt(x); }
I void pr1(LL x) { put(x), putchar(' '); }
I void pr2(LL x) { put(x), puts(""); }

int n, tot, deg[N];
vector<int> e[N];
pair<int, int> ans[1000005];

void dfs(int x, int fa, int t) {
    ans[++tot] = make_pair(x, t);
    int k, tmp = t - 1;
    if (x == 1)
        k = deg[x];
    else
        k = deg[x] - t;
    if (k < 0)
        k = 0;
    int ret = deg[x] - 1;
    for (auto to : e[x]) {
        if (to == fa)
            continue;
        if (!k)
            t = t - ret - 1, ans[++tot] = make_pair(x, t), k = n;
        dfs(to, x, t + 1);
        t++;
        k--;
    }
    if (!fa)
        return;
    if (t > tmp)
        t = tmp, ans[++tot] = make_pair(x, t);
    ans[++tot] = make_pair(fa, t + 1);
}

int main() {
    n=read();
    fo(i, 1, n - 1) {
        int x, y;
        scanf("%d%d", &x, &y);
        e[x].push_back(y);
        e[y].push_back(x);
        deg[x]++;
        deg[y]++;
    }
    dfs(1, 0, 0);
    printf("%d\n", tot);
    fo(i, 1, tot) printf("%d %d\n", ans[i].fi, ans[i].se);
    return 0;
}


#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define mp make_pair
#define reg register int
#define pii pair<int, int>
#define fo(i, a, b) for (reg i = a; i <= b; i++)
#define fd(i, a, b) for (reg i = a; i >= b; i--)
#define bp __builtin_popcount
#define cr const reg&
using namespace std;
const int mod = 998244353, N = 2e5 + 1;

I int _max(cr x, cr y) { return x > y ? x : y; }
I int _min(cr x, cr y) { return x < y ? x : y; }
I int read() {
    reg x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
    return x * f;
}
I void ptt(LL x) {
    if (x >= 10)
        ptt(x / 10);
    putchar(x % 10 + '0');
}
I void put(LL x) { x < 0 ? putchar('-'), ptt(-x) : ptt(x); }
I void pr1(LL x) { put(x), putchar(' '); }
I void pr2(LL x) { put(x), puts(""); }

int n, tot, deg[N];
vector<int> e[N];
pair<int, int> ans[1000005];

void dfs(int x, int fa, int t) {
    ans[++tot] = make_pair(x, t);
    int k, tmp = t - 1;
    if (x == 1)
        k = deg[x];
    else
        k = deg[x] - t;
    if (k < 0)
        k = 0;
    int ret = deg[x] - 1 - k;
    for (auto to : e[x]) {
        if (to == fa)
            continue;
        if (!k)
            t = t - ret - 1, ans[++tot] = make_pair(x, t), k = n;
        dfs(to, x, t + 1);
        t++;
        k--;
    }
    if (!fa)
        return;
    if (t > tmp)
        t = tmp, ans[++tot] = make_pair(x, t);
    ans[++tot] = make_pair(fa, t + 1);
}

int main() {
    n=read();
    fo(i, 1, n - 1) {
        int x, y;
        scanf("%d%d", &x, &y);
        e[x].push_back(y);
        e[y].push_back(x);
        deg[x]++;
        deg[y]++;
    }
    dfs(1, 0, 0);
    printf("%d\n", tot);
    fo(i, 1, tot) printf("%d %d\n", ans[i].fi, ans[i].se);
    return 0;
}


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

typedef long long Int;
typedef pair<int,int> PII;
typedef vector<int> VInt;

#define FOR(i, a, b) for(i = (a); i < (b); ++i)
#define RFOR(i, a, b) for(i = (a) - 1; i >= (b); --i)
#define EACH(it, a) for(auto it = (a).begin(); it != (a).end(); ++it)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define SIZE(a) int((a).size())
#define ALL(a) (a).begin(),(a).end()
#define MP make_pair

VInt E[1 << 18];
vector<PII> V;
int mx;

void dfs(int a, int p, int t)
{
    int cnt = SIZE(E[a]);
    if(p != -1) --cnt;
    int time = t;
    EACH(it, E[a])
    {
        int b = *it;
        if(b == p) continue;
        if(time == mx)
        {
            time = max(0, time - 1 - cnt);
            V.push_back(PII(a, time));
        }
        ++time;
        V.push_back(PII(b, time));
        dfs(b, a, time);
        --cnt;
    }
    if(p != -1)
    {
        if(time != t - 1)
        {
            if(time >= t) V.push_back(PII(a, t - 1));
            else throw 0;
        }
        V.push_back(PII(p, t));
    }
}

int main()
{
    int n;
    cin >> n;
    int i;
    FOR(i, 1, n)
    {
        int a, b;
        cin >> a >> b;
        --a;
        --b;
        E[a].push_back(b);
        E[b].push_back(a);
    }

    mx = 0;
    FOR(i, 0, n) mx = max(mx, SIZE(E[i]));

    V.push_back(PII(0, 0));
    dfs(0, -1, 0);
    cout << SIZE(V) << endl;
    FOR(i, 0, SIZE(V)) cout << (V[i].first + 1) << ' ' << V[i].second << endl;
    return 0;
}


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

typedef long long Int;
typedef pair<int,int> PII;
typedef vector<int> VInt;

#define FOR(i, a, b) for(i = (a); i < (b); ++i)
#define RFOR(i, a, b) for(i = (a) - 1; i >= (b); --i)
#define EACH(it, a) for(auto it = (a).begin(); it != (a).end(); ++it)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define SIZE(a) int((a).size())
#define ALL(a) (a).begin(),(a).end()
#define MP make_pair

VInt E[1 << 18];
vector<PII> V;
set<PII> S;
int mx;

void dfs(int a, int p, int t)
{
    int cnt = SIZE(E[a]);
    if(p != -1) --cnt;
    int time = t;
    EACH(it, E[a])
    {
        int b = *it;
        if(b == p) continue;
        if(time == mx)
        {
            time = max(0, t - 1 - cnt);
            V.push_back(PII(a, time));
        }
        ++time;
        V.push_back(PII(b, time));
        dfs(b, a, time);
        --cnt;
    }
    if(p != -1)
    {
        if(time != t - 1)
        {
            if(time >= t) V.push_back(PII(a, t - 1));
            else throw 0;
        }
        V.push_back(PII(p, t));
    }
}

int main()
{
    int n;
    cin >> n;
    int i;
    FOR(i, 1, n)
    {
        int a, b;
        cin >> a >> b;
        --a;
        --b;
        E[a].push_back(b);
        E[b].push_back(a);
    }

    mx = 0;
    FOR(i, 0, n) mx = max(mx, SIZE(E[i]));

    V.push_back(PII(0, 0));
    dfs(0, -1, 0);
    EACH(it, V)
    {
        if(S.count(*it) != 0) throw 0;
        S.insert(*it);
    }
    cout << SIZE(V) << endl;
    FOR(i, 0, SIZE(V)) cout << (V[i].first + 1) << ' ' << V[i].second << endl;
    return 0;
}


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

typedef long long Int;
typedef pair<int,int> PII;
typedef vector<int> VInt;

#define FOR(i, a, b) for(i = (a); i < (b); ++i)
#define RFOR(i, a, b) for(i = (a) - 1; i >= (b); --i)
#define EACH(it, a) for(auto it = (a).begin(); it != (a).end(); ++it)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define SIZE(a) int((a).size())
#define ALL(a) (a).begin(),(a).end()
#define MP make_pair

VInt E[1 << 18];
vector<PII> V;
set<PII> S;
int mx;

void dfs(int a, int p, int t)
{
    int cnt = SIZE(E[a]);
    if(p != -1) --cnt;
    int time = t;
    EACH(it, E[a])
    {
        int b = *it;
        if(b == p) continue;
        if(time == mx)
        {
            time = max(0, time - 1 - cnt);
            V.push_back(PII(a, time));
        }
        ++time;
        V.push_back(PII(b, time));
        dfs(b, a, time);
        --cnt;
    }
    if(p != -1)
    {
        if(time != t - 1)
        {
            if(time >= t) V.push_back(PII(a, t - 1));
            else throw 0;
        }
        V.push_back(PII(p, t));
    }
}

int main()
{
    int n;
    cin >> n;
    int i;
    FOR(i, 1, n)
    {
        int a, b;
        cin >> a >> b;
        --a;
        --b;
        E[a].push_back(b);
        E[b].push_back(a);
    }

    mx = 0;
    FOR(i, 0, n) mx = max(mx, SIZE(E[i]));

    V.push_back(PII(0, 0));
    dfs(0, -1, 0);
    EACH(it, V)
    {
        if(S.count(*it) != 0) throw 0;
        S.insert(*it);
    }
    cout << SIZE(V) << endl;
    FOR(i, 0, SIZE(V)) cout << (V[i].first + 1) << ' ' << V[i].second << endl;
    return 0;
}


#include "bits/stdc++.h"

#define pb push_back
#define f first
#define s second

using namespace std;

int n, mx;
vector<bool> vis;
vector<int> deg;
// node, time
vector<pair<int, int>> res;
vector<vector<int>> adj;
void dfs(int v, int tt){
    vis[v] = true;
    int cur = tt;
    for(auto i : adj[v]){
        if(vis[i]) continue;
        if(cur == mx){
            cur = 0;
            res.pb({v, 0});
        }
        cur++;
        res.pb({i, cur});
        dfs(i, cur);
        res.pb({v, cur});
    }
    if(v != 0 && cur > tt - 1){
        res.pb({v, tt - 1});
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    vis.assign(n, 0);
    deg.assign(n, 0);
    adj.assign(n, vector<int>());
    for(int i = 0; i < n - 1; ++i){
        int u, v;
        cin >> u >> v;
        u--, v--;
        deg[u]++;
        deg[v]++;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    mx = *max_element(deg.begin(), deg.end());
    res.pb({0, 0});
    dfs(0, 0);
    cout << res.size() << "\n";
    for(auto i : res){
        cout << i.f + 1 << " " << i.s << "\n";
    }
    return 0;
}


#include "bits/stdc++.h"

#define pb push_back
#define f first
#define s second

using namespace std;

int n, mx;
vector<bool> vis;
vector<int> deg;
// node, time
vector<pair<int, int>> res;
vector<vector<int>> adj;
void dfs(int v, int tt){
    vis[v] = true;
    int cur = tt;
    int mn = min(mx, tt + deg[v]);
    for(auto i : adj[v]){
        if(vis[i]) continue;
        if(cur == mn){
            cur -= deg[v];
            res.pb({v, cur});
        }
        cur++;
        res.pb({i, cur});
        dfs(i, cur);
        res.pb({v, cur});
    }
    if(v != 0 && cur != tt - 1){
        res.pb({v, tt - 1});
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    vis.assign(n, 0);
    deg.assign(n, 0);
    adj.assign(n, vector<int>());
    for(int i = 0; i < n - 1; ++i){
        int u, v;
        cin >> u >> v;
        u--, v--;
        deg[u]++;
        deg[v]++;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    mx = *max_element(deg.begin(), deg.end());
    res.pb({0, 0});
    dfs(0, 0);
    cout << res.size() << "\n";
    for(auto i : res){
        cout << i.f + 1 << " " << i.s << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
template<typename T = int> vector<T> create(size_t n){ return vector<T>(n); }
template<typename T, typename... Args> auto create(size_t n, Args... args){ return vector<decltype(create<T>(args...))>(n, create<T>(args...)); }
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	vector<vector<int>> g(n);
	for(int i = 0; i < n - 1; i++){
		int u, v; cin >> u >> v;
		u--; v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> sub(n), val(n);
	function<void(int,int)> init = [&](int u, int p){
		sub[u] = 1;
		val[u] = 1;
		vector<int> at;
		for(int v : g[u]){
			if(v == p) continue;
			init(v, u);
			sub[u] += sub[v];
			at.push_back(val[v]);
		}
		sort(at.rbegin(), at.rend());
		int len = at.size();
		for(int i = 0; i < len; i++){
			val[u] = max(val[u], at[u] + i + 1);
		}
	};
	init(0, -1);
	vector<pair<int,int>> ans;
	function<void(int,int,int)> solve = [&](int u, int p, int t){
		ans.push_back({u, t});
		vector<pair<int,int>> at;
		for(int v : g[u]){
			if(v == p) continue;
			at.push_back({val[v], v});
		}
		sort(at.rbegin(), at.rend());
		int len = at.size();
		int kek = max(t - 1, 0);
		int rec = len - kek;
		int c = t;
		for(int i = kek; i < len; i++){
			auto [d, v] = at[i];
			++c;
			solve(v, u, c);
			ans.push_back({u, c});
		}
		if(kek > 0){
			ans.push_back({u, t - 1 - min(kek, len)});
			c = t - 1 - min(kek, len);
		} else {
			if(p != -1)
				ans.push_back({u, t - 1});
		}
		for(int i = 0; i < min(kek, len); i++){
			auto [d, v] = at[i];
			++c;
			solve(v, u, c);
			ans.push_back({u, c});
		}
	};
	solve(0, -1, 0);
	cout << ans.size() << '\n';
	for(auto [v, t] : ans){
		cout << v + 1 << ' ' << t << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
template<typename T = int> vector<T> create(size_t n){ return vector<T>(n); }
template<typename T, typename... Args> auto create(size_t n, Args... args){ return vector<decltype(create<T>(args...))>(n, create<T>(args...)); }
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	vector<vector<int>> g(n);
	for(int i = 0; i < n - 1; i++){
		int u, v; cin >> u >> v;
		u--; v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> sub(n), val(n);
	function<void(int,int)> init = [&](int u, int p){
		sub[u] = 1;
		val[u] = 1;
		vector<int> at;
		for(int v : g[u]){
			if(v == p) continue;
			init(v, u);
			sub[u] += sub[v];
			at.push_back(val[v]);
		}
		sort(at.rbegin(), at.rend());
		int len = at.size();
		for(int i = 0; i < len; i++){
			val[u] = max(val[u], at[u] + i + 1);
		}
	};
	init(0, -1);
	vector<pair<int,int>> ans;
	function<void(int,int,int)> solve = [&](int u, int p, int t){
		ans.push_back({u, t});
		vector<pair<int,int>> at;
		for(int v : g[u]){
			if(v == p) continue;
			at.push_back({val[v], v});
		}
		sort(at.rbegin(), at.rend());
		int c = t;
		for(auto [d, v] : at){
			++c;
			solve(v, u, c);
			ans.push_back({u, c});
		}
		if(p != -1){
			ans.push_back({u, t - 1});
		}
	};
	solve(0, -1, 0);
	cout << ans.size() << '\n';
	for(auto [v, t] : ans){
		cout << v + 1 << ' ' << t << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
template<typename T = int> vector<T> create(size_t n){ return vector<T>(n); }
template<typename T, typename... Args> auto create(size_t n, Args... args){ return vector<decltype(create<T>(args...))>(n, create<T>(args...)); }
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	vector<vector<int>> g(n);
	for(int i = 0; i < n - 1; i++){
		int u, v; cin >> u >> v;
		u--; v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> sub(n), val(n);
	function<void(int,int)> init = [&](int u, int p){
		sub[u] = 1;
		val[u] = 1;
		vector<int> at;
		for(int v : g[u]){
			if(v == p) continue;
			init(v, u);
			sub[u] += sub[v];
			at.push_back(val[v]);
		}
		sort(at.rbegin(), at.rend());
		int len = at.size();
		for(int i = 0; i < len; i++){
			val[u] = max(val[u], at[i] + i + 1);
		}
	};
	init(0, -1);
	vector<pair<int,int>> ans;
	function<void(int,int,int)> solve = [&](int u, int p, int t){
		ans.push_back({u, t});
		vector<pair<int,int>> at;
		for(int v : g[u]){
			if(v == p) continue;
			at.push_back({val[v], v});
		}
		sort(at.rbegin(), at.rend());
		int len = at.size();
		int kek = min(max(t - 1, 0), len);
		int c = t;
		for(int i = kek; i < len; i++){
			auto [d, v] = at[i];
			++c;
			solve(v, u, c);
			ans.push_back({u, c});
		}
		if(p != -1){
			c = t - 1 - kek;
			ans.push_back({u, c});
		}
		for(int i = 0; i < kek; i++){
			auto [d, v] = at[i];
			++c;
			solve(v, u, c);
			ans.push_back({u, c});
		}
	};
	solve(0, -1, 0);
	cout << ans.size() << '\n';
	for(auto [v, t] : ans){
		cout << v + 1 << ' ' << t << '\n';
	}
	return 0;
}


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

const int SZ = 1e5 + 5;
vector<int> adj_[SZ], children[SZ];
vector<pair<int, int>> res;

void dfs(int v, int par) {
  for (int nv : adj_[v]) {
    if (nv == par) continue;
    children[v].push_back(nv);
    dfs(nv, v);
  }
}

void solve(int v, int t) {
  res.push_back({v, t});
  if (adj_[v].size() > t) {
    int cur = t + 1;
    for (int c : children[v]) {
      solve(c, cur);
      res.push_back({v, cur});
      if (++cur > adj_[v].size()) {
        res.push_back({v, 0});
        cur = 1;
      }
    }
  } else {
    int cur = t - adj_[v].size();
    res.push_back({v, cur++});
    for (int c : children[v]) {
      solve(c, cur);
      res.push_back({v, cur++});
    }
  }
}

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

  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    adj_[a].push_back(b);
    adj_[b].push_back(a);
  }
  dfs(1, -1);
  solve(1, 0);
  res.pop_back();
  cout << res.size() << '\n';
  for (const auto& p : res) cout << p.first << ' ' << p.second << '\n';
}


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

const int SZ = 1e5 + 5;
vector<int> adj_[SZ], children[SZ];
vector<pair<int, int>> res;

void dfs(int v, int par) {
  for (int nv : adj_[v]) {
    if (nv == par) continue;
    children[v].push_back(nv);
    dfs(nv, v);
  }
}

void solve(int v, int t) {
  res.push_back({v, t});
  if (adj_[v].size() > t) {
    int cur = t + 1;
    for (int c : children[v]) {
      solve(c, cur);
      res.push_back({v, cur});
      if (++cur > adj_[v].size()) {
        res.push_back({v, 0});
        cur = 1;
      }
    }
  } else {
    int cur = t - adj_[v].size();
    res.push_back({v, cur++});
    for (int c : children[v]) {
      solve(c, cur);
      res.push_back({v, cur++});
    }
  }
}

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

  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    adj_[a].push_back(b);
    adj_[b].push_back(a);
  }
  dfs(1, -1);
  solve(1, 0);
  cout << res.size() << '\n';
  for (const auto& p : res) cout << p.first << ' ' << p.second << '\n';
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC

const int N = 2e5 + 7;
int n;
vector <int> g[N];
vector <ii> ans;
set <int> us[N];
int dfs(int u, int p, int cur, int mx) {
    //cout << "in " << u << endl;

    ans.app(mp(u, cur));
    us[u].insert(cur);

    for (int v : g[u]) {
        if (v != p) {

            int to = cur + 1;
            if (cur == mx) {
                int x = cur - 1;
                while (us[u].find(x) != us[u].end()) {
                    --x;
                }
                if (x == -1)
                    return -1;
                ans.app(mp(u, x));
                us[u].insert(x);

                to = x + 1;
            }

            cur = dfs(v, u, to, mx);

            //cout << v << " return " << cur << endl;

            if (cur == -1)
                return -1;
        }
    }
    if (u == 1) {
        return cur;
    }

    int to = cur + 1;
    if (cur == mx || us[p].find(to) != us[p].end()) {
        int x = cur - 1;
        while (us[u].find(x) != us[u].end() || us[p].find(x + 1) != us[p].end()) {
            --x;
        }
        if (x == -1)
            return -1;
        ans.app(mp(u, x));
        us[u].insert(x);
        to = x + 1;
    }

    //cout << "out " << u << endl;

    ans.app(mp(p, to));
    us[p].insert(to);
    return to;
}

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    cin >> n;
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].app(v);
        g[v].app(u);
    }
    int l = -1, r = N;
    while (l < r - 1) {
        int m = (l + r) >> 1;
        if (dfs(1, 1, 0, m) != -1)
            r = m;
        else
            l = m;
        ans.clear();
        for (int i = 1; i <= n; ++i)
            us[i].clear();
    }
    dfs(1, 1, 0, r);
    cout << ans.size() << endl;
    for (auto e : ans)
        cout << e.f << ' ' << e.s << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC

const int N = 2e5 + 7;
int n;
set <int> us[N];
vector <int> g[N];
vector <ii> ans;
int dfs(int u, int p, int cur, int mx) {
    ans.app(mp(u, cur));
    us[u].insert(cur);

    for (int v : g[u]) {
        if (v != p) {

            int to = cur + 1;
            if (cur == mx) {
                int x = cur - 1;
                while (us[u].find(x) != us[u].end()) {
                    --x;
                }
                if (x == -1)
                    return -1;
                ans.app(mp(u, x));
                us[u].insert(x);

                to = x + 1;
            }

            cur = dfs(v, u, to, mx);
            if (cur == -1)
                return -1;
        }
    }
    if (u == 1)
        return cur;

    int to = cur + 1;
    if (cur == mx || us[p].find(to) != us[p].end()) {
        int x = cur - 1;
        while (us[u].find(x) != us[u].end() || us[p].find(x + 1) != us[p].end()) {
            --x;
        }
        if (x == -1)
            return -1;
        ans.app(mp(u, x));
        us[u].insert(x);
        to = x + 1;
    }

    ans.app(mp(p, to));
    us[p].insert(to);
    return to;
}

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    cin >> n;
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].app(v);
        g[v].app(u);
    }
    int l = -1, r = N;
    while (l < r - 1) {
        int m = (l + r) >> 1;

        ans.clear();
        for (int i = 0; i < N; ++i)
            us[i].clear();

        if (dfs(1, 1, 0, m) != -1)
            r = m;
        else
            l = m;
    }
    dfs(1, 1, 0, r);

    cout << ans.size() << endl;
    for (auto e : ans)
        cout << e.f << ' ' << e.s << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 1e6 + 7;
int n;
vector <int> g[N], tree[N];
void dfs(int u, int p) {
    for (int v : g[u]) {
        if (v != p) {
            tree[u].app(v);
            dfs(v, u);
        }
    }
}
int T = -1;
vector <ii> ans;
void solve(int u, int t) {
    int want = t - 1;
    int son = tree[u].size();
    ans.app(mp(u, t));
    for (int v : tree[u]) {
        if (t == T) {
            t = want - son;
            ans.app(mp(u, t));
        }
        solve(v, t + 1);
        ++t;
        ans.app(mp(u, t));
        --son;
    }
    if (u != 1 && t > want)
        ans.app(mp(u, want));
}
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    cin >> n;
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].app(v); g[v].app(u);
    }
    for (int i = 1; i <= n; ++i)
        T = max(T, (int)g[i].size());
    dfs(1, 1);
    solve(1, 0);
    cout << ans.size() << endl;
    for (auto e : ans)
        cout << e.f << ' ' << e.s << endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> p2;
ll cur,hi;
vector<p2> res;
void dfs(ll i,ll p,ll part,vector<vector<ll>> &v){
	res.push_back({i,cur});
	ll sz=ll(v[i].size());
	if(p)
	sz--;
	for(ll j=0;j<ll(v[i].size());j++)
	if(v[i][j]!=p){
		if(cur==hi){
			cur=part-sz-1;
			res.push_back({i,cur});
		}
	    cur++;
		dfs(v[i][j],i,cur,v);
		res.push_back({i,cur});
		sz--;
	}
	if(!p)
	return;
	if(cur>=part){
		cur=part-1;
		res.push_back({i,cur});
	}
	cur++;
}
void solve(){
	ll n;
	cin>>n;
	vector<vector<ll>> v(n+1);
	for(ll i=0;i<n-1;i++){
		ll a,b;
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
		hi=max(hi,ll(v[a].size()));
		hi=max(hi,ll(v[b].size()));
	}
	dfs(1,0,0,v);
	cout<<(ll)res.size()<<"\n";
	for(ll i=0;i<ll(res.size());i++)
	cout<<res[i].first<<" "<<res[i].second<<"\n";
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	ll qc=1;
    for(ll i=1;i<=qc;i++)
    solve();
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> p2;
ll cur,hi;
vector<p2> res;
void dfs(ll i,ll p,ll part,vector<vector<ll>> &v){
	res.push_back({i,cur});
	ll sz=ll(v[i].size());
	if(!p)
	sz--;
	for(ll j=0;j<ll(v[i].size());j++)
	if(v[i][j]!=p){
		if(cur==hi){
			cur=part-sz-1;
			res.push_back({i,cur});
		}
		sz--;
		cur++;
		res.push_back({i,cur});
		dfs(v[i][j],i,cur,v);
	}
	if(!p)
	return;
	if(cur>=part){
		cur=part-1;
		res.push_back({i,cur});
	}
}
void solve(){
	ll n;
	cin>>n;
	vector<vector<ll>> v(n+1);
	for(ll i=0;i<n-1;i++){
		ll a,b;
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
		hi=max(hi,ll(v[a].size()));
		hi=max(hi,ll(v[b].size()));
	}
	dfs(1,0,0,v);
	cout<<(ll)res.size()<<"\n";
	for(ll i=0;i<ll(res.size());i++)
	cout<<res[i].first<<" "<<res[i].second<<"\n";
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	ll qc=1;
    for(ll i=1;i<=qc;i++)
    solve();
}


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

#define SZ(v) ((int)(v).size())
using ll = long long;

const int MAXN = 1e5;

int cur_time=0;
vector<int> G[MAXN];
vector<pair<int, int> > ops;

void solve(int u, int p, int exit = -1)
{
	//cout << "(" << u+1 << ' ' << exit << ")" << endl;
	ops.emplace_back(u+1, cur_time++);
	//cout << u + 1 << ' ' << cur_time++ << endl;
	int nb_restants(SZ(G[u]) - (u!=p));
	if (cur_time > SZ(G[u]) and SZ(G[u]))
	{
		int reset = (exit == -1 ? 0 : exit - nb_restants);
		assert(reset>=0);
		ops.emplace_back(u+1, reset);
		//cout << u+1 << ' ' << reset << endl;
		cur_time = reset+1;
	}
	for (auto v : G[u])
		if (v != p)
		{
			if (cur_time > SZ(G[u]))
			{
				int reset = (exit == -1 ? 0 : exit - nb_restants);
				assert(reset >= 0);
				ops.emplace_back(u+1, reset);
				//cout << u + 1 << ' ' << reset << endl;
				cur_time = reset+1;
			}
			solve(v, u, cur_time-1);
			//cout << u+1 << ' ' << cur_time++ << endl;
			ops.emplace_back(u+1, cur_time++);
			--nb_restants;
		}
	if (exit != -1)
	{
		if (cur_time != exit+1)
			ops.emplace_back(u+1, exit);
		//cout << u+1 << ' ' << exit << '\n';
		cur_time = exit+1;
	}
}

int main(void)
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	int nb_sommets;
	cin >> nb_sommets;
	for (int i(1); i < nb_sommets; ++i)
		{
			int _u, _v;
			cin >> _u >> _v;
			--_u, --_v;
			G[_u].push_back(_v);
			G[_v].push_back(_u);
		}
	solve(0,0);
	cout << SZ(ops) << endl;
	for (auto [u, t] : ops)
		cout << u << ' ' << t << '\n';
}


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

#define SZ(v) ((int)(v).size())
using ll = long long;

const int MAXN = 1e5;

int cur_time=0;
vector<int> G[MAXN];
vector<pair<int, int> > ops;

void solve(int u, int p, int exit = -1)
{
	//cout << "(" << u+1 << ' ' << exit << ")" << endl;
	ops.emplace_back(u+1, cur_time++);
	//cout << u + 1 << ' ' << cur_time++ << endl;
	int nb_restants(SZ(G[u]) - (u!=p));
	for (auto v : G[u])
		if (v != p)
		{
			if (cur_time > SZ(G[u]))
			{
				int reset = (exit == -1 ? 0 : exit - nb_restants-1);
				assert(reset >= 0);
				ops.emplace_back(u+1, reset);
				//cout << u + 1 << ' ' << reset << endl;
				cur_time = reset+1;
			}
			solve(v, u, cur_time-1);
			//cout << u+1 << ' ' << cur_time++ << endl;
			ops.emplace_back(u+1, cur_time++);
			--nb_restants;
		}
	if (exit != -1)
	{
		if (cur_time != exit+1)
			ops.emplace_back(u+1, exit);
		//cout << u+1 << ' ' << exit << '\n';
		cur_time = exit+1;
	}
}

int main(void)
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	int nb_sommets;
	cin >> nb_sommets;
	for (int i(1); i < nb_sommets; ++i)
		{
			int _u, _v;
			cin >> _u >> _v;
			--_u, --_v;
			G[_u].push_back(_v);
			G[_v].push_back(_u);
		}
	solve(0,0);
	cout << SZ(ops) << endl;
	for (auto [u, t] : ops)
		cout << u << ' ' << t << '\n';
}


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

#define SZ(v) ((int)(v).size())
using ll = long long;

const int MAXN = 1e5;

int cur_time=0;
vector<int> G[MAXN];
vector<pair<int, int> > ops;

void solve(int u, int p, int exit = -1)
{
	//cout << "(" << u+1 << ' ' << exit << ")" << endl;
	ops.emplace_back(u+1, cur_time++);
	//cout << u + 1 << ' ' << cur_time++ << endl;
	int nb_restants(SZ(G[u]) - (u!=p));
	for (auto v : G[u])
		if (v != p)
		{
			if (cur_time > SZ(G[u]))
			{
				int reset = (exit == -1 ? 0 : exit - nb_restants);
				assert(reset >= 0);
				ops.emplace_back(u+1, reset);
				//cout << u + 1 << ' ' << reset << endl;
				cur_time = reset+1;
			}
			solve(v, u, cur_time-1);
			//cout << u+1 << ' ' << cur_time++ << endl;
			ops.emplace_back(u+1, cur_time++);
			--nb_restants;
		}
	if (exit != -1)
	{
		if (cur_time != exit+1)
			ops.emplace_back(u+1, exit);
		//cout << u+1 << ' ' << exit << '\n';
		cur_time = exit+1;
	}
}

int main(void)
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	int nb_sommets;
	cin >> nb_sommets;
	for (int i(1); i < nb_sommets; ++i)
		{
			int _u, _v;
			cin >> _u >> _v;
			--_u, --_v;
			G[_u].push_back(_v);
			G[_v].push_back(_u);
		}
	solve(0,0);
	cout << SZ(ops) << endl;
	for (auto [u, t] : ops)
		cout << u << ' ' << t << '\n';
}


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

#define SZ(v) ((int)(v).size())
using ll = long long;

const int MAXN = 1e5;

int cur_time=1;
vector<int> G[MAXN];
vector<pair<int, int> > ops;

void solve(int u, int p, int exit = -1)
{
	if (u)
	{
		ops.emplace_back(u+1, ++cur_time);
		//cout << u + 1 << ' ' << ++cur_time << endl;
	}
	int nb_restants(SZ(G[u]) - (u!=p));
	if (cur_time >= SZ(G[u]))
	{
		int reset = (exit == -1 ? 0 : exit - nb_restants);
		assert(reset>=0);
		ops.emplace_back(u+1, reset);
		//cout << u+1 << ' ' << reset << endl;
		cur_time = reset;
	}
	for (auto v : G[u])
		if (v != p)
		{
			if (cur_time +1>= SZ(G[u]) and cur_time)
			{
				int reset = (exit == -1 ? 0 : exit - nb_restants);
				assert(reset >= 0);
				ops.emplace_back(u+1, reset);
				//cout << u + 1 << ' ' << reset << endl;
				cur_time = reset;
			}
			if (nb_restants==1)
			{
				solve(v, u);
				return ;
			}
			else
			{
				solve(v, u, cur_time);
				//cout << u+1 << ' ' << ++cur_time << endl;
				ops.emplace_back(u+1, ++cur_time);
			}
			--nb_restants;
		}
}

int main(void)
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	int nb_sommets;
	cin >> nb_sommets;
	for (int i(1); i < nb_sommets; ++i)
		{
			int _u, _v;
			cin >> _u >> _v;
			--_u, --_v;
			G[_u].push_back(_v);
			G[_v].push_back(_u);
		}
	if (SZ(G[0]) == 1)
	{
		ops.emplace_back(1, --cur_time);
		//cout << 1 << ' ' << --cur_time << endl;
	}
	solve(0,0);
	cout << SZ(ops) << endl;
	for (auto [u, t] : ops)
		cout << u+1 << ' ' << t << '\n';
}


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

#define SZ(v) ((int)(v).size())
using ll = long long;

const int MAXN = 1e5;

int cur_time=0;
vector<int> G[MAXN];
vector<pair<int, int> > ops;

void solve(int u, int p, int exit = -1)
{
	//cout << "(" << u+1 << ' ' << exit << ")" << endl;
	ops.emplace_back(u+1, cur_time++);
	//cout << u + 1 << ' ' << cur_time++ << endl;
	int nb_restants(SZ(G[u]) - (u!=p));
	for (auto v : G[u])
		if (v != p)
		{
			if (cur_time > SZ(G[u]))
			{
				int reset = (exit == -1 ? 0 : exit - nb_restants);
				assert(reset >= 0);
				if (reset != cur_time-1)
					ops.emplace_back(u+1, reset);
				//cout << u + 1 << ' ' << reset << endl;
				cur_time = reset+1;
			}
			solve(v, u, cur_time-1);
			//cout << u+1 << ' ' << cur_time++ << endl;
			ops.emplace_back(u+1, cur_time++);
			--nb_restants;
		}
	if (exit != -1)
	{
		if (cur_time != exit+1)
			ops.emplace_back(u+1, exit);
		//cout << u+1 << ' ' << exit << '\n';
		cur_time = exit+1;
	}
}

int main(void)
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	int nb_sommets;
	cin >> nb_sommets;
	for (int i(1); i < nb_sommets; ++i)
		{
			int _u, _v;
			cin >> _u >> _v;
			--_u, --_v;
			G[_u].push_back(_v);
			G[_v].push_back(_u);
		}
	solve(0,0);
	cout << SZ(ops) << endl;
	for (auto [u, t] : ops)
		cout << u << ' ' << t << '\n';
}


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

#define SZ(v) ((int)(v).size())
using ll = long long;

const int MAXN = 1e5;

int cur_time=0;
vector<int> G[MAXN];
vector<pair<int, int> > ops;

void solve(int u, int p, int exit = -1)
{
	//cout << "(" << u+1 << ' ' << exit << ")" << endl;
	ops.emplace_back(u+1, cur_time++);
	//cout << u + 1 << ' ' << cur_time++ << endl;
	int nb_restants(SZ(G[u]) - (u!=p));
	if (cur_time > SZ(G[u]))
	{
		int reset = (exit == -1 ? 0 : exit - nb_restants);
		assert(reset>=0);
		ops.emplace_back(u+1, reset);
		//cout << u+1 << ' ' << reset << endl;
		cur_time = reset+1;
	}
	for (auto v : G[u])
		if (v != p)
		{
			if (cur_time > SZ(G[u]))
			{
				int reset = (exit == -1 ? 0 : exit - nb_restants);
				assert(reset >= 0);
				ops.emplace_back(u+1, reset);
				//cout << u + 1 << ' ' << reset << endl;
				cur_time = reset+1;
			}
			solve(v, u, cur_time-1);
			//cout << u+1 << ' ' << cur_time++ << endl;
			ops.emplace_back(u+1, cur_time++);
			--nb_restants;
		}
	if (exit != -1)
	{
		if (cur_time != exit+1)
			ops.emplace_back(u+1, exit);
		//cout << u+1 << ' ' << exit << '\n';
		cur_time = exit+1;
	}
}

int main(void)
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	int nb_sommets;
	cin >> nb_sommets;
	for (int i(1); i < nb_sommets; ++i)
		{
			int _u, _v;
			cin >> _u >> _v;
			--_u, --_v;
			G[_u].push_back(_v);
			G[_v].push_back(_u);
		}
	solve(0,0);
	cout << SZ(ops) << endl;
	for (auto [u, t] : ops)
		cout << u << ' ' << t << '\n';
}


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

#define SZ(v) ((int)(v).size())
using ll = long long;

const int MAXN = 1e5;

int cur_time=1;
vector<int> G[MAXN];
vector<pair<int, int> > ops;

void solve(int u, int p, int exit = -1)
{
	if (u)
	{
		ops.emplace_back(u+1, ++cur_time);
		//cout << u + 1 << ' ' << ++cur_time << endl;
	}
	int nb_restants(SZ(G[u]) - (u!=p));
	if (cur_time >= SZ(G[u]))
	{
		int reset = (exit == -1 ? 0 : exit - nb_restants);
		assert(reset>=0);
		ops.emplace_back(u+1, reset);
		//cout << u+1 << ' ' << reset << endl;
		cur_time = reset;
	}
	for (auto v : G[u])
		if (v != p)
		{
			if (cur_time +1>= SZ(G[u]) and cur_time)
			{
				int reset = (exit == -1 ? 0 : exit - nb_restants);
				assert(reset >= 0);
				ops.emplace_back(u+1, reset);
				//cout << u + 1 << ' ' << reset << endl;
				cur_time = reset;
			}
			if (nb_restants==1)
			{
				solve(v, u);
				return ;
			}
			else
			{
				solve(v, u, cur_time);
				//cout << u+1 << ' ' << ++cur_time << endl;
				ops.emplace_back(u+1, ++cur_time);
			}
			--nb_restants;
		}
}

int main(void)
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	int nb_sommets;
	cin >> nb_sommets;
	for (int i(1); i < nb_sommets; ++i)
		{
			int _u, _v;
			cin >> _u >> _v;
			--_u, --_v;
			G[_u].push_back(_v);
			G[_v].push_back(_u);
		}
	if (SZ(G[0]) == 1)
		cout << 1 << ' ' << --cur_time << endl;
	solve(0,0);
	cout << SZ(ops) << endl;
	for (auto [u, t] : ops)
		cout << u+1 << ' ' << t << '\n';
}


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

#define SZ(v) ((int)(v).size())
using ll = long long;

const int MAXN = 1e5;

int cur_time=0;
vector<int> G[MAXN];
vector<pair<int, int> > ops;

void solve(int u, int p, int exit = -1)
{
	//cout << "(" << u+1 << ' ' << exit << ")" << endl;
	ops.emplace_back()ck(u+1, cur_time++);
	//cout << u + 1 << ' ' << cur_time++ << endl;
	int nb_restants(SZ(G[u]) - (u!=p));
	for (auto v : G[u])
		if (v != p)
		{
			if (cur_time > SZ(G[u]))
			{
				int reset = (exit == -1 ? 0 : exit - nb_restants);
				assert(reset >= 0);
				if (reset != cur_time-1)
					ops.emplace_back(u+1, reset);
				//cout << u + 1 << ' ' << reset << endl;
				cur_time = reset+1;
			}
			solve(v, u, cur_time-1);
			//cout << u+1 << ' ' << cur_time++ << endl;
			ops.emplace_back(u+1, cur_time++);
			--nb_restants;
		}
	if (exit != -1)
	{
		if (cur_time != exit+1)
			ops.emplace_back(u+1, exit);
		//cout << u+1 << ' ' << exit << '\n';
		cur_time = exit+1;
	}
}

int main(void)
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	int nb_sommets;
	cin >> nb_sommets;
	for (int i(1); i < nb_sommets; ++i)
		{
			int _u, _v;
			cin >> _u >> _v;
			--_u, --_v;
			G[_u].push_back(_v);
			G[_v].push_back(_u);
		}
	solve(0,0);
	cout << SZ(ops) << endl;
	for (auto [u, t] : ops)
		cout << u << ' ' << t << '\n';
}


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

#define SZ(v) ((int)(v).size())
using ll = long long;

const int MAXN = 1e5;

int cur_time=1;
vector<int> G[MAXN];
vector<pair<int, int> > ops;

void solve(int u, int p, int exit = -1)
{
	if (u)
	{
		ops.emplace_back(u+1, ++cur_time);
		//cout << u + 1 << ' ' << ++cur_time << endl;
	}
	int nb_restants(SZ(G[u]) - (u!=p));
	if (cur_time >= SZ(G[u]))
	{
		int reset = (exit == -1 ? 0 : exit - nb_restants);
		assert(reset>=0);
		ops.emplace_back(u+1, reset);
		//cout << u+1 << ' ' << reset << endl;
		cur_time = reset;
	}
	for (auto v : G[u])
		if (v != p)
		{
			if (cur_time +1>= SZ(G[u]) and cur_time)
			{
				int reset = (exit == -1 ? 0 : exit - nb_restants);
				assert(reset >= 0);
				ops.emplace_back(u+1, reset);
				//cout << u + 1 << ' ' << reset << endl;
				cur_time = reset;
			}
			if (nb_restants==1)
			{
				solve(v, u);
				return ;
			}
			else
			{
				solve(v, u, cur_time);
				//cout << u+1 << ' ' << ++cur_time << endl;
				ops.emplace_back(u+1, ++cur_time);
			}
			--nb_restants;
		}
}

int main(void)
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	int nb_sommets;
	cin >> nb_sommets;
	for (int i(1); i < nb_sommets; ++i)
		{
			int _u, _v;
			cin >> _u >> _v;
			--_u, --_v;
			G[_u].push_back(_v);
			G[_v].push_back(_u);
		}
	if (SZ(G[0]) == 1)
	{
		ops.emplace_back(1, --cut_time);
		//cout << 1 << ' ' << --cur_time << endl;
	}
	solve(0,0);
	cout << SZ(ops) << endl;
	for (auto [u, t] : ops)
		cout << u+1 << ' ' << t << '\n';
}


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

#define SZ(v) ((int)(v).size())
using ll = long long;

const int MAXN = 1e5;

int cur_time=1;
vector<int> G[MAXN];
vector<pair<int, int> > ops;

void solve(int u, int p, int exit = -1)
{
	if (u)
	{
		ops.emplace_back(u+1, ++cur_time);
		//cout << u + 1 << ' ' << ++cur_time << endl;
	}
	int nb_restants(SZ(G[u]) - (u!=p));
	if (cur_time >= SZ(G[u]))
	{
		int reset = (exit == -1 ? 0 : exit - nb_restants);
		assert(reset>=0);
		ops.emplace_back(u+1, reset);
		//cout << u+1 << ' ' << reset << endl;
		cur_time = reset;
	}
	for (auto v : G[u])
		if (v != p)
		{
			if (cur_time +1>= SZ(G[u]) and cur_time)
			{
				int reset = (exit == -1 ? 0 : exit - nb_restants);
				assert(reset >= 0);
				ops.emplace_back(u+1, reset);
				//cout << u + 1 << ' ' << reset << endl;
				cur_time = reset;
			}
			if (nb_restants==1)
			{
				solve(v, u);
				return ;
			}
			else
			{
				solve(v, u, cur_time);
				//cout << u+1 << ' ' << ++cur_time << endl;
				ops.emplace_back(u+1, ++cur_time);
			}
			--nb_restants;
		}
}

int main(void)
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	int nb_sommets;
	cin >> nb_sommets;
	for (int i(1); i < nb_sommets; ++i)
		{
			int _u, _v;
			cin >> _u >> _v;
			--_u, --_v;
			G[_u].push_back(_v);
			G[_v].push_back(_u);
		}
	if (SZ(G[0]) == 1)
	{
		ops.emplace_back(1, --cur_time);
		//cout << 1 << ' ' << --cur_time << endl;
	}
	solve(0,0);
	cout << SZ(ops) << endl;
	for (auto [u, t] : ops)
		cout << u << ' ' << t << '\n';
}


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

#define SZ(v) ((int)(v).size())
using ll = long long;

const int MAXN = 1e5;

int cur_time=0;
vector<int> G[MAXN];
vector<pair<int, int> > ops;

void solve(int u, int p, int exit = -1)
{
	//cout << "(" << u+1 << ' ' << exit << ")" << endl;
	ops.emplace_back(u+1, cur_time++);
	//cout << u + 1 << ' ' << cur_time++ << endl;
	int nb_restants(SZ(G[u]) - (u!=p));
	if (cur_time > SZ(G[u]))
	{
		int reset = (exit == -1 ? 0 : exit - nb_restants);
		assert(reset>=0);
		ops.emplace_back(u+1, reset);
		//cout << u+1 << ' ' << reset << endl;
		cur_time = reset+1;
	}
	for (auto v : G[u])
		if (v != p)
		{
			if (cur_time > SZ(G[u]))
			{
				int reset = (exit == -1 ? 0 : exit - nb_restants);
				assert(reset >= 0);
				ops.emplace_back(u+1, reset);
				//cout << u + 1 << ' ' << reset << endl;
				cur_time = reset+1;
			}
			solve(v, u, cur_time-1);
			//cout << u+1 << ' ' << cur_time++ << endl;
			ops.emplace_back(u+1, cur_time++);
			--nb_restants;
		}
	if (exit != -1)
	{
		ops.emplace_back(u+1, exit);
		//cout << u+1 << ' ' << exit << '\n';
		cur_time = exit+1;
	}
}

int main(void)
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	int nb_sommets;
	cin >> nb_sommets;
	for (int i(1); i < nb_sommets; ++i)
		{
			int _u, _v;
			cin >> _u >> _v;
			--_u, --_v;
			G[_u].push_back(_v);
			G[_v].push_back(_u);
		}
	solve(0,0);
	cout << SZ(ops) << endl;
	for (auto [u, t] : ops)
		cout << u << ' ' << t << '\n';
}


#include <cstdio>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;

namespace io {
#define File(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
  const int SIZE = (1 << 21) + 1;
  char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1;
  inline char getc () {return (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++);}
  inline void flush () {fwrite (obuf, 1, oS - obuf, stdout); oS = obuf;}
  inline void putc (char x) {*oS ++ = x; if (oS == oT) flush ();}
  template<class T>
  inline void read(T &x) {
    char ch; int f = 1;
    x = 0;
    while(isspace(ch = getc()));
    if(ch == '-') ch = getc(), f = -1;
    do x = x * 10 + ch - '0'; while(isdigit(ch = getc()));
    x *= f;
  }
  template<class T, class ...Args>
  inline void read(T &x, Args&... args) {read(x); read(args...);}
  template<class T>
  inline void write(T x) {
    static char stk[128];
    int top = 0;
    if(x == 0) {putc('0'); return;}
    if(x < 0) putc('-'), x = -x;
    while(x) stk[top++] = x % 10, x /= 10;
    while(top) putc(stk[--top] + '0');
  }
  template<class T, class ...Args>
  inline void write(T x, Args... args) {write(x); putc(' '); write(args...);}
  inline void space() {putc(' ');}
  inline void endl() {putc('\n');}
  struct _flush {~_flush() {flush();}} __flush;
};
using io::read; using io::write; using io::flush; using io::space; using io::endl; using io::getc; using io::putc;

const int N = 100005;
vector<int> G[N];
vector<pair<int, int> > res;
int t = 0, maxdeg = 0;

void dfs(int x, int fa) {
  bool flag = false;
  res.emplace_back(x, t);
  if(t == maxdeg) {
    t -= G[x].size(), flag = true;
    res.emplace_back(x, t);
  }
  for (int v : G[x]) {
    if(v == fa) continue;
    ++t;
    dfs(v, x);
    res.emplace_back(x, t);
    if(x != 1 && t == maxdeg) {
      t -= G[x].size(), flag = true;
      res.emplace_back(x, t);
    }
  }
  if(x != 1 && !flag) {
    t -= G[x].size(); flag = true;
    res.emplace_back(x, t);
  }
  ++t;
}

int main() {
  int n;
  read(n);
  if(n == 1) {
    write(1), endl();
    write(1, 0), endl();
    return 0;
  }
  for (int i = 1; i < n; ++i) {
    int u, v;
    read(u, v);
    G[u].push_back(v); G[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i)
    maxdeg = max(maxdeg, (int)G[i].size());
  dfs(1, 1);
  write(res.size()), endl();
  for (auto p : res)
    write(p.first, p.second), endl();
  return 0;
}


#include <cstdio>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;

namespace io {
#define File(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
  const int SIZE = (1 << 21) + 1;
  char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1;
  inline char getc () {return (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++);}
  inline void flush () {fwrite (obuf, 1, oS - obuf, stdout); oS = obuf;}
  inline void putc (char x) {*oS ++ = x; if (oS == oT) flush ();}
  template<class T>
  inline void read(T &x) {
    char ch; int f = 1;
    x = 0;
    while(isspace(ch = getc()));
    if(ch == '-') ch = getc(), f = -1;
    do x = x * 10 + ch - '0'; while(isdigit(ch = getc()));
    x *= f;
  }
  template<class T, class ...Args>
  inline void read(T &x, Args&... args) {read(x); read(args...);}
  template<class T>
  inline void write(T x) {
    static char stk[128];
    int top = 0;
    if(x == 0) {putc('0'); return;}
    if(x < 0) putc('-'), x = -x;
    while(x) stk[top++] = x % 10, x /= 10;
    while(top) putc(stk[--top] + '0');
  }
  template<class T, class ...Args>
  inline void write(T x, Args... args) {write(x); putc(' '); write(args...);}
  inline void space() {putc(' ');}
  inline void endl() {putc('\n');}
  struct _flush {~_flush() {flush();}} __flush;
};
using io::read; using io::write; using io::flush; using io::space; using io::endl; using io::getc; using io::putc;

const int N = 100005;
vector<int> G[N];
vector<pair<int, int> > res;
int t = 0, maxdeg = 0;

void dfs(int x, int fa) {
  res.emplace_back(x, t);
  bool flag = false;
  for (int v : G[x]) {
    if(v == fa) continue;
    ++t;
    dfs(v, x);
    res.emplace_back(x, t);
    if(t == maxdeg){
      t -= G[x].size(), flag = true;
      res.emplace_back(x, t);
    }
  }
  if(x != 1 && !flag) {
    t -= G[x].size(); flag = true;
    res.emplace_back(x, t);
  }
  ++t;
}

int main() {
//  File("cf1340d");
  int n;
  read(n);
  for (int i = 1; i < n; ++i) {
    int u, v;
    read(u, v);
    G[u].push_back(v); G[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i)
    maxdeg = max(maxdeg, (int)G[i].size());
  dfs(1, 1);
  write(res.size()), endl();
  for (auto p : res)
    write(p.first, p.second), endl();
  return 0;
}


#include <cstdio>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;

namespace io {
#define File(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
  const int SIZE = (1 << 21) + 1;
  char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1;
  inline char getc () {return (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++);}
  inline void flush () {fwrite (obuf, 1, oS - obuf, stdout); oS = obuf;}
  inline void putc (char x) {*oS ++ = x; if (oS == oT) flush ();}
  template<class T>
  inline void read(T &x) {
    char ch; int f = 1;
    x = 0;
    while(isspace(ch = getc()));
    if(ch == '-') ch = getc(), f = -1;
    do x = x * 10 + ch - '0'; while(isdigit(ch = getc()));
    x *= f;
  }
  template<class T, class ...Args>
  inline void read(T &x, Args&... args) {read(x); read(args...);}
  template<class T>
  inline void write(T x) {
    static char stk[128];
    int top = 0;
    if(x == 0) {putc('0'); return;}
    if(x < 0) putc('-'), x = -x;
    while(x) stk[top++] = x % 10, x /= 10;
    while(top) putc(stk[--top] + '0');
  }
  template<class T, class ...Args>
  inline void write(T x, Args... args) {write(x); putc(' '); write(args...);}
  inline void space() {putc(' ');}
  inline void endl() {putc('\n');}
  struct _flush {~_flush() {flush();}} __flush;
};
using io::read; using io::write; using io::flush; using io::space; using io::endl; using io::getc; using io::putc;

const int N = 100005;
vector<int> G[N];
vector<pair<int, int> > res;
int t = 0, maxdeg = 0;

void dfs(int x, int fa) {
  bool flag = false;
  res.emplace_back(x, t);
  if(t == maxdeg) {
    t -= G[x].size(), flag = true;
    res.emplace_back(x, t);
  }
  for (int v : G[x]) {
    if(v == fa) continue;
    ++t;
    dfs(v, x);
    res.emplace_back(x, t);
    if(t == maxdeg) {
      t -= G[x].size(), flag = true;
      res.emplace_back(x, t);
    }
  }
  if(x != 1 && !flag) {
    t -= G[x].size(); flag = true;
    res.emplace_back(x, t);
  }
  ++t;
}

int main() {
  int n;
  read(n);
  if(n == 1) {
    write(1), endl();
    write(1, 0), endl();
    return 0;
  }
  for (int i = 1; i < n; ++i) {
    int u, v;
    read(u, v);
    G[u].push_back(v); G[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i)
    maxdeg = max(maxdeg, (int)G[i].size());
  dfs(1, 1);
  write(res.size()), endl();
  for (auto p : res)
    write(p.first, p.second), endl();
  return 0;
}


#include <cstdio>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;

namespace io {
#define File(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
  const int SIZE = (1 << 21) + 1;
  char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1;
  inline char getc () {return (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++);}
  inline void flush () {fwrite (obuf, 1, oS - obuf, stdout); oS = obuf;}
  inline void putc (char x) {*oS ++ = x; if (oS == oT) flush ();}
  template<class T>
  inline void read(T &x) {
    char ch; int f = 1;
    x = 0;
    while(isspace(ch = getc()));
    if(ch == '-') ch = getc(), f = -1;
    do x = x * 10 + ch - '0'; while(isdigit(ch = getc()));
    x *= f;
  }
  template<class T, class ...Args>
  inline void read(T &x, Args&... args) {read(x); read(args...);}
  template<class T>
  inline void write(T x) {
    static char stk[128];
    int top = 0;
    if(x == 0) {putc('0'); return;}
    if(x < 0) putc('-'), x = -x;
    while(x) stk[top++] = x % 10, x /= 10;
    while(top) putc(stk[--top] + '0');
  }
  template<class T, class ...Args>
  inline void write(T x, Args... args) {write(x); putc(' '); write(args...);}
  inline void space() {putc(' ');}
  inline void endl() {putc('\n');}
  struct _flush {~_flush() {flush();}} __flush;
};
using io::read; using io::write; using io::flush; using io::space; using io::endl; using io::getc; using io::putc;

const int N = 100005;
vector<int> G[N];
vector<pair<int, int> > res;
int t = 0, maxdeg = 0;

void dfs(int x, int fa) {
  bool flag = false;
  res.emplace_back(x, t);
  if(t == maxdeg) {
    t -= G[x].size(), flag = true;
    res.emplace_back(x, t);
  }
  for (int v : G[x]) {
    if(v == fa) continue;
    ++t;
    dfs(v, x);
    res.emplace_back(x, t);
    if(t == maxdeg) {
      t -= G[x].size(), flag = true;
      res.emplace_back(x, t);
    }
  }
  if(x != 1 && !flag) {
    t -= G[x].size(); flag = true;
    res.emplace_back(x, t);
  }
  ++t;
}

int main() {
  int n;
  read(n);
  for (int i = 1; i < n; ++i) {
    int u, v;
    read(u, v);
    G[u].push_back(v); G[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i)
    maxdeg = max(maxdeg, (int)G[i].size());
  dfs(1, 1);
  write(res.size()), endl();
  for (auto p : res)
    write(p.first, p.second), endl();
  return 0;
}


#include <cstdio>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;

namespace io {
#define File(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
  const int SIZE = (1 << 21) + 1;
  char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1;
  inline char getc () {return (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++);}
  inline void flush () {fwrite (obuf, 1, oS - obuf, stdout); oS = obuf;}
  inline void putc (char x) {*oS ++ = x; if (oS == oT) flush ();}
  template<class T>
  inline void read(T &x) {
    char ch; int f = 1;
    x = 0;
    while(isspace(ch = getc()));
    if(ch == '-') ch = getc(), f = -1;
    do x = x * 10 + ch - '0'; while(isdigit(ch = getc()));
    x *= f;
  }
  template<class T, class ...Args>
  inline void read(T &x, Args&... args) {read(x); read(args...);}
  template<class T>
  inline void write(T x) {
    static char stk[128];
    int top = 0;
    if(x == 0) {putc('0'); return;}
    if(x < 0) putc('-'), x = -x;
    while(x) stk[top++] = x % 10, x /= 10;
    while(top) putc(stk[--top] + '0');
  }
  template<class T, class ...Args>
  inline void write(T x, Args... args) {write(x); putc(' '); write(args...);}
  inline void space() {putc(' ');}
  inline void endl() {putc('\n');}
  struct _flush {~_flush() {flush();}} __flush;
};
using io::read; using io::write; using io::flush; using io::space; using io::endl; using io::getc; using io::putc;

const int N = 100005;
vector<int> G[N];
vector<pair<int, int> > res;
int t = 0, maxdeg = 0;

void dfs(int x, int fa) {
  bool flag = false;
  res.emplace_back(x, t);
  if(t == maxdeg) {
    t -= G[x].size(), flag = true;
    res.emplace_back(x, t);
  }
  for (int v : G[x]) {
    if(v == fa) continue;
    ++t;
    dfs(v, x);
    res.emplace_back(x, t);
    if(t == maxdeg) {
      t -= G[x].size(), flag = true;
      res.emplace_back(x, t);
    }
  }
  if(x != 1 && !flag) {
    t -= G[x].size(); flag = true;
    res.emplace_back(x, t);
  }
  ++t;
}

int main() {
  int n;
  read(n);
  if(n == 1) {
    write(1, 0), endl();
    return 0;
  }
  for (int i = 1; i < n; ++i) {
    int u, v;
    read(u, v);
    G[u].push_back(v); G[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i)
    maxdeg = max(maxdeg, (int)G[i].size());
  dfs(1, 1);
  write(res.size()), endl();
  for (auto p : res)
    write(p.first, p.second), endl();
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

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

const int N = (int)2e5 + 5;

int T;
vector<int> g[N];
vector<pii> out;

void dfs(int v, int p, int t) {
    out.push_back({v, t});
    int tin = t;
    int restart = t - sz(g[v]);
    bool re = false;
    for (int u : g[v]) {
        if (u == p)
            continue;
        if (t == T) {
            re = true;
            t = restart;
            out.push_back({v, t});
        }
        ++t;
        ++restart;
        dfs(u, v, t);
        out.push_back({v, t});
    }
    if (!re && p != -1) {
        t = tin - 1;
        out.push_back({v, t});
    }
}

int main()
{

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif

    int n;
    scanf("%d", &n);
    for (int i = 1; i < n; ++i) {
        int u, v;
        scanf("%d %d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }

    if (n == 1) {
        printf("1\n");
        printf("1 0\n");
        return 0;
    }

    T = 0;
    for (int i = 1; i <= n; ++i)
        T = max(T, sz(g[i]));

    dfs(1, -1, 0);

    printf("%d\n", sz(out));
    for (auto& [v, t] : out)
        printf("%d %d\n", v, t);
    printf("\n");

}


#include <bits/stdc++.h>

using namespace std;

#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

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

const int N = (int)2e5 + 5;

int T;
vector<int> g[N];
vector<pii> out;

void dfs(int v, int p, int t) {
    out.push_back({v, t});
    int tin = t;
    int restart = t - sz(g[v]);
    bool re = false;
    for (int u : g[v]) {
        if (u == p)
            continue;
        if (t == T) {
            re = true;
            t = restart;
            out.push_back({v, t});
        }
        ++t;
        ++restart;
        dfs(u, v, t);
        out.push_back({v, t});
    }
    if (!re && p != -1) {
        t = tin - 1;
        out.push_back({v, t});
    }
}

int main()
{

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif

    int n;
    scanf("%d", &n);
    for (int i = 1; i < n; ++i) {
        int u, v;
        scanf("%d %d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }

    if (n == 1) {
        printf("1 0\n");
        return 0;
    }

    T = 0;
    for (int i = 1; i <= n; ++i)
        T = max(T, sz(g[i]));

    dfs(1, -1, 0);

    printf("%d\n", sz(out));
    for (auto& [v, t] : out)
        printf("%d %d\n", v, t);
    printf("\n");

}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pc __builtin_popcount

constexpr int MAXN = 1 << 17;

int mod = 1;
vector<int> grafo[MAXN];
vector<pair<int, int>> path;

void dfs(int v, int u, int t, bool last) {
  for (int& i : grafo[v]) {
    if (i == u) swap(i, grafo[v].back());
  }
  if (grafo[v].back() == u) grafo[v].pop_back();
  for (int i = 0; i < grafo[v].size(); i++) {
    int x = grafo[v][i];
    if (t + i == mod) path.emplace_back(v, mod);
    path.emplace_back(v, (t + i) % mod);
    dfs(x, v, (t + i) % mod + 1, i + 1 == grafo[v].size());
  }
  if (t + grafo[v].size() == mod) path.emplace_back(v, mod);
  path.emplace_back(v, (t + grafo[v].size()) % mod);
  if (!last && path.back().se != t - 1) {
    path.emplace_back(v, t - 1);
  }
}

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

  int N;
  cin >> N;

  if (N == 1) {
    cout << "1 0" << endl;
    return 0;
  }

  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    grafo[a].push_back(b);
    grafo[b].push_back(a);
  }

  for (int i = 1; i <= N; i++) {
    mod = max(mod, (int)grafo[i].size());

  }

  dfs(1, 0, 0, 1);
  path.pop_back();

  cout << path.size() << '\n';
  for (auto [a, b] : path) {
    cout << a << ' ' << b << '\n';
  }

  return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pc __builtin_popcount

constexpr int MAXN = 1 << 17;

int mod = 1;
vector<int> grafo[MAXN];
vector<pair<int, int>> path;

void dfs(int v, int u, int t) {
  for (int& i : grafo[v]) {
    if (i == u) swap(i, grafo[v].back());
  }
  if (grafo[v].back() == u) grafo[v].pop_back();
  for (int i = 0; i < grafo[v].size(); i++) {
    int x = grafo[v][i];
    if (t + i == mod) path.emplace_back(v, mod);
    path.emplace_back(v, (t + i) % mod);
    dfs(x, v, (t + i) % mod + 1);
  }
  if (t + grafo[v].size() == mod) path.emplace_back(v, mod);
  path.emplace_back(v, (t + grafo[v].size()) % mod);
  if (v != 1 && path.back().se != t - 1) {
    path.emplace_back(v, t - 1);
  }
}

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

  int N;
  cin >> N;

  if (N == 1) {
    cout << "1 0" << endl;
    return 0;
  }

  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    grafo[a].push_back(b);
    grafo[b].push_back(a);
  }

  for (int i = 1; i <= N; i++) {
    mod = max(mod, (int)grafo[i].size());

  }

  dfs(1, 0, 0);
  path.pop_back();

  cout << path.size() << '\n';
  for (auto [a, b] : path) {
    cout << a << ' ' << b << '\n';
  }

  return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pc __builtin_popcount

constexpr int MAXN = 1 << 17;

int mod = 3;
vector<int> grafo[MAXN];
vector<pair<int, int>> path;

void dfs(int v, int u, int t) {
  int x = t;
  for (int i : grafo[v]) {
    if (i == u) continue;
    path.emplace_back(v, x);
    if (x == mod) {
      x = mod - grafo[v].size();
      path.emplace_back(v, x);
    }
    dfs(i, v, ++x);
  }
  path.emplace_back(v, x);
  if (v == 1) return;
  if (x == mod) {
    x = mod - grafo[v].size();
    path.emplace_back(v, x);
  }
  if (path.back().se != t - 1) {
    path.emplace_back(v, t - 1);
  }
}

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

  int N;
  cin >> N;

  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    grafo[a].push_back(b);
    grafo[b].push_back(a);
  }

  for (int i = 1; i <= N; i++) {
    mod = max(mod, (int)grafo[i].size());
  }

  dfs(1, 0, 0);

  cout << path.size() << '\n';
  for (auto [a, b] : path) {
    cout << a << ' ' << b << '\n';
  }

  return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pc __builtin_popcount

constexpr int MAXN = 1 << 17;

int mod;
vector<int> grafo[MAXN];
vector<pair<int, int>> path;

void dfs(int v, int u, int t) {
  int x = t;
  for (int i : grafo[v]) {
    if (i == u) continue;
    path.emplace_back(v, x);
    if (x == mod) {
      x = mod - grafo[v].size();
      path.emplace_back(v, x);
    }
    dfs(i, v, ++x);
  }
  path.emplace_back(v, x);
  if (v == 1) return;
  if (x == mod) {
    x = mod - grafo[v].size();
    path.emplace_back(v, x);
  }
  if (path.back().se != t - 1) {
    path.emplace_back(v, t - 1);
  }
}

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

  int N;
  cin >> N;

  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    grafo[a].push_back(b);
    grafo[b].push_back(a);
  }

  for (int i = 1; i <= N; i++) {
    mod = max(mod, (int)grafo[i].size());
  }

  dfs(1, 0, 0);

  cout << path.size() << '\n';
  for (auto [a, b] : path) {
    cout << a << ' ' << b << '\n';
  }

  return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;

vi adj[222222];
int dp[222222];

void dfs(int u, int p=-1)
{
	vi vec;
	for(int v:adj[u])
	{
		if(v==p) continue;
		dfs(v,u);
		vec.pb(dp[v]);
	}
	sort(vec.rbegin(),vec.rend());
	for(int i=0;i<vec.size();i++)
	{
		dp[u]=max(dp[u],vec[i]+i+1);
	}
}

vector<ii> ans;
int mxdeg;
set<int> used[111111];

void push(int u, int t)
{
	ans.pb({u,t});
	used[u].insert(t);
}

int solve(int u, int p, int curt)
{
	push(u,curt);
	//cerr<<"START: "<<u<<' '<<p<<' '<<curt<<'\n';
	for(int v:adj[u])
	{
		if(v==p) continue;
		if(curt+1>mxdeg)
		{
			for(int i=0;i<=mxdeg;i++)
			{
				if(used[u].find(i)==used[u].end())
				{
					curt=i;
					push(u,i);
					break;
				}
			}
		}
		int ret = solve(v,u,curt+1);
		curt=ret;
		//cerr<<"PUSH "<<u<<' '<<v<<' '<<ret<<'\n';
		push(u,curt);
	}
	//i need the final push
	if(p!=-1)
	{
		//go to something <= curt?
		int minex=int(1e9);
		for(int i=1;i<=mxdeg;i++)
		{
			if((used[p].find(i)==used[p].end())&&(i-1==curt||used[u].find(i-1)==used[u].end()))
			{
				minex=i-1; break;
			}
		}
		////cerr<<"AT "<<u<<' '<<minex<<' '<<curt<<' '<<(used[p].find(2)==used[p].end())<<'\n';
		assert(minex<=curt);
		if(minex<curt)
		{
			curt=minex;
			push(u,curt);
		}
	}
	//cerr<<u<<' '<<p<<' '<<curt<<'\n';
	return curt+1;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n; cin>>n;
	for(int i=0;i<n-1;i++)
	{
		int u,v; cin>>u>>v; u--; v--;
		adj[u].pb(v); adj[v].pb(u);
	}
	mxdeg=0;
	for(int i=0;i<n;i++) mxdeg=max(mxdeg,int(adj[i].size()));
	solve(0,-1,0);
	cout<<ans.size()<<'\n';
	for(ii x:ans)
	{
		cout<<x.fi+1<<' '<<x.se<<'\n';
	}
}


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

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;

vi adj[222222];
int dp[222222];

void dfs(int u, int p=-1)
{
	vi vec;
	for(int v:adj[u])
	{
		if(v==p) continue;
		dfs(v,u);
		vec.pb(dp[v]);
	}
	sort(vec.rbegin(),vec.rend());
	for(int i=0;i<vec.size();i++)
	{
		dp[u]=max(dp[u],vec[i]+i+1);
	}
}

vector<ii> ans;
int mxdeg;

void push(int u, int t)
{
	ans.pb({u,t});
}

void solve(int u, int p, int curt)
{
	push(u,curt);
	int reser=curt-1;
	int ch=0;
	for(int v:adj[u])
	{
		if(v==p) continue;
		ch++;
	}
	for(int v:adj[u])
	{
		if(v==p) continue;
		if(curt>=mxdeg)
		{
			curt=reser-ch;
			while(curt==reser) curt++;
			push(u,curt);
		}
		solve(v,u,curt+1);
		push(u,curt+1);
		curt++;
		ch--;
	}
	if(p!=-1&&curt!=reser)
	{
		push(u,reser);
	}
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n; cin>>n;
	for(int i=0;i<n-1;i++)
	{
		int u,v; cin>>u>>v; u--; v--;
		adj[u].pb(v); adj[v].pb(u);
	}
	mxdeg=0;
	for(int i=0;i<n;i++) mxdeg=max(mxdeg,int(adj[i].size()));
	solve(0,-1,0);
	cout<<ans.size()<<'\n';
	for(ii x:ans)
	{
		cout<<x.fi+1<<' '<<x.se<<'\n';
	}
}


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

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;

vi adj[222222];
int dp[222222];

void dfs(int u, int p=-1)
{
	vi vec;
	for(int v:adj[u])
	{
		if(v==p) continue;
		dfs(v,u);
		vec.pb(dp[v]);
	}
	sort(vec.rbegin(),vec.rend());
	for(int i=0;i<vec.size();i++)
	{
		dp[u]=max(dp[u],vec[i]+i+1);
	}
}

vector<ii> ans;
int mxdeg;

void push(int u, int t)
{
	ans.pb({u,t});
}

int solve(int u, int p, int curt)
{
	push(u,curt);
	int reser=curt-1;
	for(int v:adj[u])
	{
		if(v==p) continue;
		if(curt>=mxdeg)
		{
			curt=0;
			while(curt==reser) curt++;
			push(u,curt);
		}
		solve(v,u,curt+1);
		push(u,curt+1);
		curt++;
	}
	if(p!=-1) push(u,reser);
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n; cin>>n;
	for(int i=0;i<n-1;i++)
	{
		int u,v; cin>>u>>v; u--; v--;
		adj[u].pb(v); adj[v].pb(u);
	}
	mxdeg=0;
	for(int i=0;i<n;i++) mxdeg=max(mxdeg,int(adj[i].size()));
	solve(0,-1,0);
	cout<<ans.size()<<'\n';
	for(ii x:ans)
	{
		cout<<x.fi+1<<' '<<x.se<<'\n';
	}
}


#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define x first
#define y second
#define ld long double
#define ll long long

using namespace std;

const int MOD = (int)1e9 + 7;
const int MOD2 = (int)1e9 + 9;
const int B = 79;

int n, x, y, mx, t;

vector <int> g[100005];
vector <pair <int, int>> v;

void dfs(int nod, int tata, int t2) {
  int nr = g[nod].size() - (tata != 0);
  v.push_back({nod, t});
  for(auto &fiu : g[nod]) {
    if(fiu == tata)
      continue;
    if(t == mx) {
      t = t2 - nr - 1;
      v.push_back({nod, t});
    }
    t++;
    dfs(fiu, nod, t);
    v.push_back({nod, t});
    nr--;
  }
  if(nod == 1)
    return;
  if(t >= t2) {
    t = t2 - 1;
    v.push_back({nod, t});
  }
  t++;
}

int main() {
  ios_base :: sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  cin >> n;
  for(int i = 1; i < n; i++) {
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for(int i = 1; i <= n; i++)
    mx = max(mx, (int)g[i].size());
  dfs(1, 0, -1);
  cout << v.size() << "\n";
  for(auto &i : v)
    cout << i.first << " " << i.second << "\n";
  return 0;
}


#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define x first
#define y second
#define ld long double
#define ll long long

using namespace std;

const int MOD = (int)1e9 + 7;
const int MOD2 = (int)1e9 + 9;
const int B = 79;

int n, x, y, mx, t;

vector <int> g[100005];
vector <pair <int, int>> v;

void dfs(int nod, int tata, int t2) {
  int nr = g[nod].size() - (tata != 0);
  v.push_back({nod, t});
  for(auto &fiu : g[nod]) {
    if(fiu == tata)
      continue;
    if(t == mx) {
      t = t2 - nr - 1;
      v.push_back({nod, t});
    }
    t++;
    dfs(fiu, nod, t);
    v.push_back({nod, t});
    nr--;
  }
  if(nod == 1)
    return;
  t = t2 - 1;
  v.push_back({nod, t});
  t++;
}

int main() {
  ios_base :: sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  cin >> n;
  for(int i = 1; i < n; i++) {
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for(int i = 1; i <= n; i++)
    mx = max(mx, (int)g[i].size());
  dfs(1, 0, -1);
  cout << v.size() << "\n";
  for(auto &i : v)
    cout << i.first << " " << i.second << "\n";
  return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>

#include <algorithm>
#include <random>
#include <bitset>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <vector>
#include <chrono>
#include <iostream>
#include <limits>
#include <numeric>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (T& x : v)
    is >> x;
  return is;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  if (!v.empty()) {
    os << v.front();
    for (int i = 1; i < v.size(); ++i)
      os << ' ' << v[i];
  }
  return os;
}

int main() {
#ifdef ELEGIA
  freopen("test.in", "r", stdin);
  int nol_cl = clock();
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n; cin >> n;
  vector<vector<int>> g(n + 1);
  vector<pair<int, int>> ans;
  for (int rep = 1; rep < n; ++rep) {
    int u, v; cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  function<void(int, int, int)> dfs = [&](int u, int x, int p) {
    ans.emplace_back(u, x);
    if (g[u].size() == 1) {
      ans.emplace_back(u, x - 1);
      return;
    }

    int y = x;
    int r = (int)g[u].size() - 1;
    if (x - 1 >= r) {
      y = x - (int)g[u].size();
      ans.emplace_back(u, y);
    }
    for (int v : g[u])
      if (v != p) {
        dfs(v, ++y, u);
        ans.emplace_back(u, y);
        if (x - 1 == --r) {
          y = 0;
          ans.emplace_back(u, y);
        }
      }

  };
  ans.emplace_back(1, 0);
  for (int i = 0; i < g[1].size(); ++i) {
    dfs(g[1][i], i + 1, 1);
    ans.emplace_back(1, i + 1);
  }

  cout << ans.size() << '\n';
  for (auto pr : ans)
    cout << pr.first << ' ' << pr.second << '\n';

#ifdef ELEGIA
  LOG("Time: %dms\n", int ((clock()
          -nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
  return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>

#include <algorithm>
#include <random>
#include <bitset>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <vector>
#include <chrono>
#include <iostream>
#include <limits>
#include <numeric>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (T& x : v)
    is >> x;
  return is;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  if (!v.empty()) {
    os << v.front();
    for (int i = 1; i < v.size(); ++i)
      os << ' ' << v[i];
  }
  return os;
}

int main() {
#ifdef ELEGIA
  freopen("test.in", "r", stdin);
  int nol_cl = clock();
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n; cin >> n;
  vector<vector<int>> g(n + 1);
  vector<pair<int, int>> ans;
  for (int rep = 1; rep < n; ++rep) {
    int u, v; cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  function<void(int, int, int, int)> dfs = [&](int u, int x, int y, int p) {
    //cerr << u << " : " << x << " => " << y << '\n';
    ans.emplace_back(u, x);
    int deg = (int)g[u].size() - 1;
    if (deg == 0) {
      ans.emplace_back(u, y);
      return;
    }

    if (y == 0) {
      vector<int> prep;
      int cur = x - 1;
      if (cur != 0) {
        while (deg--) {
          prep.push_back(cur--);
          if (cur == 0)
            break;
        }
      }
      if (deg > 0) {
        cur = x + deg;
        while (cur > x) prep.push_back(cur--);
      }
      reverse(begin(prep), end(prep));
      cur = 0;
      for (int v : g[u])
        if (v != p) {
          dfs(v, x + 1, prep[cur] - 1, u);
          x = prep[cur++];
          ans.emplace_back(u, x);
        }
      ans.emplace_back(u, 0);
      return;
    }

    bool fl = false;
    if (x - 1 > deg) {
      fl = true;
      ++deg;
    }

    vector<int> prep;
    int cur = y;
    while (deg--) {
      if (cur <= y) {
        prep.push_back(cur--);
        if (cur == 0) {
          cur = x - 1;
          if (x - 1 == y)
            break;
        }
      } else {
        prep.push_back(cur--);
        if (cur == y)
          break;
      }
    }

    if (deg > 0) {
      cur = x + deg;
      while (cur > x) prep.push_back(cur--);
    }
    reverse(begin(prep), end(prep));
    cur = 0;
    if (fl) ans.emplace_back(u, x = prep[cur++]);
    for (int v : g[u])
      if (v != p) {
        dfs(v, x + 1, prep[cur] - 1, u);
        ans.emplace_back(u, x = prep[cur++]);
      }

  };
  ans.emplace_back(1, 0);
  for (int i = 0; i < g[1].size(); ++i) {
    dfs(g[1][i], i + 1, i, 1);
    ans.emplace_back(1, i + 1);
  }

  cout << ans.size() << '\n';
  for (auto pr : ans)
    cout << pr.first << ' ' << pr.second << '\n';

#ifdef ELEGIA
  LOG("Time: %dms\n", int ((clock()
          -nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define fast ios::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int nax = 1e5 + 10;

int T;
vector<pair<int,int>> m;
set<pair<int,int>> check;
vector<int> g[nax];
int val[nax];

void add(int u,int t){
	if(check.find({u,t}) == check.end()){
		m.push_back({u,t});
		check.insert({u,t});
	}
}

int fr[nax];

int best(int u,int t){
	for(int i=fr[u];i<=T;i++){
		if(check.find({u,i}) == check.end()){
			fr[u] = i;
			return i;
		}
	}
	return T;
}

int dfs(int u,int p,int t,int b=0){
	add(u,t);
	val[u] = t;
	if(t == T){
		t = 0;
		add(u,t);
	}
	// cerr << u << '\n';
	vector<int> to;
	for(int v : g[u]){
		if(v == p)continue;
		to.push_back(v);
	}
	for(int i=0;i<(int)to.size()-1;i++){
		// if(u == 3)cerr << to.size() << '\n';
		int v = to[i];
		t = dfs(v,u,t+1,0) + 1;
		add(u,t);
		if(t == T){
			t = best(u,t);
			add(u,t);
		}
	}
	for(int i=T-1;i>=0;i--){
		if(check.find({u,i}) == check.end()){
			if(check.find({p,i+1}) == check.end() && i >= b-1){
				if(to.size()){
					// cerr << u << ' ' << to.back() << ' ' << i << '\n';
					t = dfs(to.back(),u,t+1,i) + 1;
					// cerr << t << '\n';
					add(u,t);
					if(t == T){
						t = best(u,t);
						add(u,t);
					}
				}
				if(t != i && p){
					assert(t > i);
					add(u,i);
				}
				return i;
			}
		}
	}
	if(p && t+1 <= T && check.find({p,t+1}) == check.end()){
		int w = t;
		if(to.size()){
			// cerr << t << ' ' << u << ' ' << to.back() << '\n';
			t = dfs(to.back(),u,t+1,t+1) + 1;
			add(u,t);
			if(t == T){
				t = best(u,t);
				add(u,t);
			}
			add(u,t);
		}
		// if(t != w && p){
		// 	// assert(t > w);
		// 	cerr << u << ' ' << w << '\n';
		// 	add(u,w);
		// }
		return t;
	}
	// if(p)
	// 	assert(0);
	return 0;
}

int main(){
	fast
	int n;
	cin>>n;
	for(int i=0;i<n-1;i++){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i=1;i<=n;i++)T = max(T , (int)g[i].size());
	val[1]=1;
	dfs(1,0,0);
	// assert(check.size() == m.size());
	cout << m.size() << '\n';
	for(pair<int,int> i : m){
		cout << i.first << ' ' << i.second << '\n';
	}
}