stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

#define rep(i,n) for (int i = 0; i < (n); i++)

#define rep1(i,n) for (int i = 1; i <= (n); i++)

#define repf(i,a,b) for (int i = (a); i < (b); i++)

#define repi(i,a,b) for (int i = (a); i <= (b); i++)

#define repr(i,a,b) for (int i = (a); i >= (b); i--)

#define all(x) (x).begin(),(x).end()

using namespace std;

using uint = unsigned int;

using ll = long long;

using ull = unsigned long long;

using vi = vector<int>;

using vvi = vector<vi>;

using vd = vector<double>;

using pii = pair<int, int>;

using vc = vector<char>;

using vvc = vector<vc>;

using vs = vector<string>;

using tiii = tuple<int, int, int>;



const int N = 1000000;



int main() {

  int s;

  cin >> s;

  

  vi a(N+1,0);

  a.at(1) = s;

  repi(i,2,N) {

    if (a.at(i-1)%2==0) {

      a.at(i) = a.at(i-1)/2;

    }

    else {

      a.at(i) = 3*a.at(i-1)+1;

    }

    

    repi(j,1,i-1) {

      if (a.at(j)==a.at(i)) {

        cout << i << endl;

        return 0;

      }

    }

  }

}

// Faster C++ solution for the same problem (measured speedup: 5.043535375995943x):
#include <bits/stdc++.h>

#define rep(i,n) for (int i = 0; i < (n); i++)

#define rep1(i,n) for (int i = 1; i <= (n); i++)

#define repf(i,a,b) for (int i = (a); i < (b); i++)

#define repi(i,a,b) for (int i = (a); i <= (b); i++)

#define repr(i,a,b) for (int i = (a); i >= (b); i--)

#define all(x) (x).begin(),(x).end()

using namespace std;

using uint = unsigned int;

using ll = long long;

using ull = unsigned long long;

using vi = vector<int>;

using vvi = vector<vi>;

using vd = vector<double>;

using pii = pair<int, int>;

using vc = vector<char>;

using vvc = vector<vc>;

using vs = vector<string>;

using tiii = tuple<int, int, int>;



const int N = 1000000;



int main() {

  int s;

  cin >> s;

  

  set<int> st;

  int x = s;

  while (st.count(x)==0) {

    st.insert(x);

    if(x%2 == 0) {

      x /= 2;

    }

    else {

      x = x*3+1;

    }

  }

  

  cout << st.size()+1 << endl;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int min(int a,int b){

    if(a<b) return a;

    else return b;

}





int main(){

    int he[100005],dp[100005];

    int n,k;

    cin >> n >> k;

    memset(dp,-1,sizeof(dp));

    for(int i=1;i<=n;i++){

        cin >> he[i];

    }

    

    for(int i=n;i>=0;i--){

         int result;

    if(i==n) result=0;

    

    else{

        int x=-1;

        for(int j=i+1;j<=n;j++){

            int a;

            a=abs(he[i]-he[j])+dp[j];

            

            if(x==-1) x=a;

            x=min(x,a);

            if(j-i==k) break;

        }

        result=x;

    }

    dp[i]=result;

    }

    cout << dp[1] << endl;

    

}

// Faster C++ solution for the same problem (measured speedup: 1.4035388544399472x):
#include<bits/stdc++.h>

#define MAXN 1e5



using namespace std;





int dp (){

}



long long n,m,a,b,c,k;

string s1,s2;



int main(){

    cin>>n>>k;

    int l[n],l2[n];

    for (int i=0; i<n; i++){

        cin>>l[i];

        l2[i]=0;

    }

    for (long long i=1; i<n; i++){

        l2[i]=l2[i-1]+abs(l[i]-l[i-1]);

        for (int j=1; j<=min(i, k); j++){

            l2[i]=min(l2[i], abs( l[i] - l[i-j]) + l2[i-j]);

        }

    }

    cout<<l2[n-1]<<endl;

}
// Slower C++ solution:
#include<iostream>

#include <bits/stdc++.h>

#include<algorithm>

using namespace std;

int main(){

	int k,x;

	cin>>k>>x;//k连续  第  x  块儿

	//向后 

	int num1=x-k;

	int num2=x+k;

	int t=2*k-1;

	int f=0;

	for(int i=num1+1;i<num2;i++){

		if(i>=-1000000||i<=1000000){

			cout<<i;

			if(f==t){

			}else{

				cout<<" ";

			}

		}

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.217687097643155x):
///d. one clue

#include <stdio.h>

#include <math.h>



int main()

{

    int k,x;

    scanf("%d %d",&k, &x);



    for(int i=x-k+1;i<=x+k-1;i++){

        printf("%d ",i);

    }

    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define inf 0x3f3f3f3f

#define ll long long

const int N=200005;

const int mod=1e9+7;

const double eps=1e-8;

const double PI = acos(-1.0);

#define lowbit(x) (x&(-x))

ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}

ll qpow(ll a,ll b){ll res=1;while(b){if(b&1) res=res*a%mod;a=a*a%mod;b>>=1;}return res;}

ll inv(ll a,ll p){return qpow(a,p-2);}

int main()

{

    std::ios::sync_with_stdio(false);

    string s;

    cin>>s;

    char ans[15];

    int j=0,l=s.length();

    for(int i=0;i<l;i++)

    {

        if(s[i]=='B')

        {

            if(j>0)

                j--;

        }

        else

            ans[j++]=s[i];

    }

    ans[j++]='\0';

    cout<<ans<<endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.399806139391618x):
#include<stdio.h>

#include<string.h>

int main(){

    char G[100], H[100];

    int A=0;

    scanf("%s", G);

    int len=strlen(G);

    for(int i=0;i<len;i++)

    {

        if(G[i]=='B')

        {

            A--;

            if(A<0)

                A=0;

        }

        else

            H[A++]=G[i];

    }

    H[A]='\0';

    printf("%s\n", H);

    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;

int main(){

	long long n,k,a[100000],dp[100000];

	scanf("%lld %lld", &n, &k);

	for(long long i=0;i<n;i++){

		scanf("%lld", &a[i]);

	}

	for(long long i=1;i<=k;i++){

		dp[i]=abs(a[0]-a[i]);

	}

	for(long long i=k+1;i<n;i++){

		dp[i]=1e9;

		for(long long j=1;j<=k;j++)

		dp[i]=min(dp[i-j]+abs(a[i]-a[i-j]),dp[i]);

	}

	printf("%lld", dp[n-1]);

}


// Faster C++ solution for the same problem (measured speedup: 5.354209872632783x):
#include<cstdio>

#include<algorithm>

using namespace std;



#define ll long long 



#define file_io 0



ll i,t,j;

ll n,k,h[100005],dp[100005];



int main(){

	if (file_io){

		freopen(".in","r",stdin);

		freopen(".out","w",stdout);

	}

	

	scanf("%lld%lld",&n,&k);

	

	for (i=1; i<=n; ++i){

		scanf("%lld",&h[i]);

	}

	

	dp[1]=0;

	for (i=2; i<=n; ++i){

		dp[i]=2e9;

		for (t=max(1ll,i-k); t<i; ++t){

			dp[i]=min(dp[t]+abs(h[i]-h[t]),dp[i]);

		}

	}

	

	printf("%lld\n",dp[n]);

	

	return 0;

}
// Slower C++ solution:


#include <iostream>

#include <cstdio>

#include <cmath>

#include <cstring>

#include <string>

#include <vector>

#include <set>

#include <queue>

#include <utility>

#include <algorithm>

#include <map>

#include <cassert>

using namespace std;



const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

int n, k, h[maxn], cost[maxn];



int solve2(int x) {

    // recursive method

    int res = cost[x];

    if (res < inf) return res;

    for (int i = 1; i <= k; i++) {

        if (x - i >= 0) res = min(res, solve2(x - i) + abs(h[x] - h[x - i]));

    }

    cost[x] = res;

    return res;

}



int main() {

    cin >> n >> k;

    for (int i = 0; i < n; i++) cin >> h[i];

    memset(cost, inf, sizeof(cost));

    cost[0] = 0;

    cout << solve2(n - 1) << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4000400218280185x):
#include <iostream>

#include <cstdio>

#include <cmath>

#include <cstring>

#include <string>

#include <vector>

#include <set>

#include <queue>

#include <utility>

#include <algorithm>

#include <map>

#include <cassert>

using namespace std;



const int maxn = 1e5 + 5;

int n, k, h[maxn], cost[maxn];



int main() {

    cin >> n >> k;

    for (int i = 0; i < n; i++) cin >> h[i];

    for (int i = 1; i < n; i++) cost[i] = 1e9;

    cost[0] = 0;

    for (int i = 1; i < n; i++) {

        for (int j = 1; j <= min(i, k); j++) {

            cost[i] = min(cost[i], cost[i - j] + abs(h[i] - h[i - j]));

        }

    }

    cout << cost[n - 1] << endl;

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <vector>

#include <algorithm>

#include <climits>

using namespace std ;



int main(){

    std::ios_base::sync_with_stdio(0);  cin.tie(NULL);  cout.tie(NULL);

    string text , actions ;

    cin >> actions ;

    for(int i = 0 ; i < actions.size() ; i++){

        if(actions[i] == '1') text += '1' ;

        else if(actions[i] == '0') text += '0' ;

        else {

            if(text.size() > 0)

            text.pop_back() ;

        }

    }

    cout << text ;

    return 0 ;

}

// Faster C++ solution for the same problem (measured speedup: 5.369296802757118x):
#include <stdio.h>

#include <string.h>



int main(){

	char str[15];

	char str2[15] = {0};

	int x = 0, ctr = 0, j = 0;

	scanf("%s", str);

	

	for(int i =0;i < strlen(str);i++){

		if(str[i] == '0'){

			str2[j++] = '0';

			ctr++;

		}

	    else if(str[i] == '1'){

			str2[j++] = '1';

			ctr++;

		}

        else{

			if(ctr > 0){

			j--;

			str2[j] = '\0';

			ctr--;

		}

		}

	}

	printf("%s", str2);

	return 0;

	}
// Slower C++ solution:
/**

*   Bismillahir Rahmanir Rahim.

*   Imtiaz_rafi

*   PCIU, CHITTAGONG

**/

#include<bits/stdc++.h>

using namespace std;

#define speed ios::sync_with_stdio(0);cin.tie(0);

#define filein freopen ("input.txt", "r", stdin)

#define fileout freopen ("output.txt", "w", stdout)

#define rep(i,a) for(ll i=0;i<a;i++)

#define rep1(i,begin,end) for(ll i=begin;i<=end;i++)

#define all(v)   v.begin(),v.end()

#define mem(dp,i) memset(dp,i,sizeof(dp))

#define pb push_back

#define pf push_front

#define mp make_pair

#define pi acos(-1)

// bitshift (1LL<<(3)) *2^3

typedef long long int ll;

typedef vector<ll>vi;

typedef map<ll,ll>m;

typedef map<char,ll>mc;

typedef set<ll>st;

typedef set<char>sc;

ll dx[] ={-1,+1,0,0,+1,-1,+1,-1};

ll dy[] ={0,0,-1,+1,+1,+1,-1,-1};

const ll mx = 1e5+123;

bool vis[mx];

vi adj[mx];

ll a,k,b[mx],dp[mx];

ll solve(ll i)

{

    if(i==a) return 0;

    if(dp[i]!=-1) return dp[i];

    ll x=INT_MAX;

    for(ll j=1;j<=k;j++)

    {

        if(i+j<=a)

            x=min(x,abs(b[i]-b[i+j])+solve(i+j));

    }

    return dp[i]=x;

}

int main()

{

    speed;

    cin>>a>>k;

    rep1(i,1,a)

        cin>>b[i];

    mem(dp,-1);

    cout<<solve(1)<<endl;

}




// Faster C++ solution for the same problem (measured speedup: 2.4239855423120686x):
/**

*   Bismillahir Rahmanir Rahim.

*   Imtiaz_rafi

*   PCIU, CHITTAGONG

**/

#include<bits/stdc++.h>

using namespace std;

#define speed() ios::sync_with_stdio(0);cin.tie(0);

#define file() freopen ("input.txt", "r", stdin);freopen ("output.txt", "w", stdout);

#define rep(i,a) for(ll i=0;i<a;i++)

#define rep1(i,begin,end) for(ll i=begin;i<=end;i++)

#define mem(dp,i) memset(dp,i,sizeof(dp))

#define all(v)   v.begin(),v.end()

#define rall(v)   v.rbegin(),v.rend()

#define pb push_back

#define pf push_front

#define mp make_pair

#define pi acos(-1)

// bitshift (1LL<<(3)) *2^3

typedef long long int ll;

typedef vector<ll>vi;

typedef map<ll,ll>mi;

typedef pair<ll,ll>pll;

typedef vector<pll> vll;

typedef set<ll>st;

typedef set<char>sc;

ll dx[] = {-1,+1,0,0,+1,-1,+1,-1};

ll dy[] = {0,0,-1,+1,+1,+1,-1,-1};

const ll mx = 1e5+123;

const ll MOD = 1e9+7;



template <class T> T power(T a,T b){a%=MOD;if(!a) return 0;T p=1;while(b>0){if(b&1){p*=a;p%=MOD;}a*=a;a%=MOD;b=b>>1;}return p;}

template <class S> void print(vector<S> &v){for(ll i=0; i<(ll)v.size(); i++){cerr << v[i] << ' ';}cerr << endl;}

template <class T> T gcd(T a, T b){return (b!=0?gcd<T>(b,a%b):a);}

template <class T> T lcm(T a, T b){return (a/gcd<T>(a,b)*b);}



ll BS(ll l,ll r,ll a[],ll k){ll mid,i=-1;while(l<=r){mid=(l+r)>>1;if(a[mid]==k){return i=mid;/*r=mid-1;*/}if(a[mid]<k) l=mid+1;if(a[mid]>k) r=mid-1;}return i;}



ll dp[mx],b[mx],a,k;

ll solve(ll i)

{

    if(i==a)

        return 0;

    if(dp[i]!=-1)

        return dp[i];

    ll x=INT_MAX;

    for(ll j=1;j<=k;j++)

    {

        if(i+j<=a)

            x = min(x,abs(b[i]-b[i+j])+solve(i+j));

    }

    return dp[i] = x;

}



int main()

{

    speed();

    ll i;

    cin>>a>>k;

    rep1 (i,1,a)

        cin>>b[i];

    mem(dp,-1);

    cout<<solve(1)<<endl;



}
// Slower C++ solution:
#include<iostream>

#include<vector>

#include<string.h>

#include<cstdio>

using namespace std;

int main()

{

    char s[12],a;

    int i,j,len,num;

    vector<char>str;

    cin.get(s,12);

    len=strlen(s);

    for(i=0;i<len;i++){

        if(str.empty()==false && s[i]=='B') str.pop_back();

        else if(s[i]=='1' || s[i]=='0') str.push_back(s[i]);

    }

    for(i=0;i<str.size();i++){

        cout<<str[i];

    }

    cout<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 5.319140131945491x):
#include<stdio.h>

#include<string.h>

int main(){

    char G[100], H[100];

    int A=0;

    scanf("%s", G);

    int len=strlen(G);

    for(int i=0;i<len;i++)

    {

        if(G[i]=='B')

        {

            A--;

            if(A<0)

                A=0;

        }

        else

            H[A++]=G[i];

    }

    H[A]='\0';

    printf("%s\n", H);

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <cmath>

#include <cstring>



using namespace std;



int pedras[100100];

int dp[100100]; ///Guardo os valores da funcao frog;



int n,k;

const int inf =1000000/*1e9*/;



int main(){



    memset(dp,inf,sizeof(dp));

    cin >>n>>k;



    for(int i=0;i<n;i++){

        cin >> pedras[i];

    }



    ///Comeca aki a bagaca

    dp[n-1]=0;



    for(int i=n-2;i>=0;i--){



        ///int menor=inf;



        for(int j=1;j<=k;j++){

            if(i+j<=n-1){

                int cost = abs(pedras[i]-pedras[i+j]);

                dp[i]=min(dp[i],dp[i+j]+cost);

                /*menor = min(menor,dp[i+j]+cost);*/

            }

        }



        ///dp[i]=menor;

    }



    cout<<dp[0];



}


// Faster C++ solution for the same problem (measured speedup: 1.4039417333990956x):
#include <iostream>

#include <algorithm>

#include <cmath>

#include <cstring>



using namespace std;



int pedras[100100];

int dp[100100]; ///Guardo os valores da funcao frog;



int n,k;

const int inf =1e9;



int main(){



    ///memset(dp,inf,sizeof(dp));

    cin >>n>>k;



    for(int i=0;i<n;i++){

        cin >> pedras[i];

    }



    ///Comeca aki a bagaca

    dp[n-1]=0;



    for(int i=n-2;i>=0;i--){



        int menor=inf;



        for(int j=1;j<=k;j++){

            if(i+j<=n-1){

                int cost = abs(pedras[i]-pedras[i+j]);

                ///dp[i]=min(dp[i],dp[i+j]+cost);

                menor = min(menor,dp[i+j]+cost);

            }

        }



        dp[i]=menor;

    }



    cout<<dp[0];



}
// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#define ll long long

#define Di 3.1415926

#include<math.h>

#include<string>

#include<stack>

#define INF 0x3f3f3f3f

using namespace std;

ll  gcd(ll a,ll b)

{

    return a%b? gcd(b,a%b):b;

}

int main()

{   stack<char>w;

      string s;

      cin>>s;

      char a[20];

     int len =s.length();

     for(int i=0;i<len;i++)

     {

         if(s[i]=='0')

            w.push('0');

         else if(s[i]=='1')

            w.push('1');

            else if(s[i]=='B')

            {

              if(!w.empty())

              {

                  w.pop();

              }

            }



     }

     int i=0;

     while(!w.empty() )

     {

         a[i]=w.top();

         w.pop();

         i++;



     }

     for(int j=i-1;j>=0;j--)

        cout<<a[j];

     cout<<endl;



 return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.335854085761804x):
#include<stdio.h>

#include<string.h>

int main(){

    char G[100], H[100];

    int A=0;

    scanf("%s", G);

    int len=strlen(G);

    for(int i=0;i<len;i++)

    {

        if(G[i]=='B')

        {

            A--;

            if(A<0)

                A=0;

        }

        else

            H[A++]=G[i];

    }

    H[A]='\0';

    printf("%s\n", H);

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

int main()

{

  int n,k;

  int h[100005] = {};

  int dp[100005] = {};

  int until;

  cin >> n >> k;



  for(int i=0; i<n; i++) cin >> h[i];



  dp[0] = 0;

  dp[1] = abs(h[0] - h[1]);

  until = 1;

  for(int i=2; i<n; i++){

    until = 1;

    dp[i] = abs(h[i] - h[i-1]) + dp[i-1];

    for(int j=2; j<=k; j++){

      if(i - j < 0) break;

      if(abs(h[i] - h[i - j]) + dp[i - j] < abs(h[i] - h[i - until]) + dp[i - until] ){

	dp[i] = abs(h[i] - h[i - j]) + dp[i - j];

	until = j;

      }

    }

  }

  cout << dp[n-1] << endl;

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.7837522238787817x):
#include <bits/stdc++.h>

using namespace std;

int main()

{

  int dp[100005],h[100005];

  int n,k,sm;

  cin >> n >> k;

  for(int i=0; i<n; i++) cin >> h[i];

  

  dp[0] = 0;

  dp[1] = abs(h[1] - h[0]);



  for(int i=2; i<n; i++){

    sm = 1;

    dp[i] = abs(h[i] - h[i-1]) + dp[i-1];

    for(int j=2; j<=k; j++){

      if(i - j < 0) break;

      if(abs(h[i] - h[i-j]) + dp[i-j] < abs(h[i] - h[i-sm]) + dp[i-sm]){

	dp[i] = abs(h[i] - h[i-j]) + dp[i-j];

	sm = j;

      }

    }

  }

  

  cout << dp[n-1] << endl;

  return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

#define in read()

#define fur(i,a,b) for(int i=a;i<=b;i++)

#define fdr(i,a,b) for(int i=a;i>=b;i--)

#define xx 100001

#define ll long long

inline ll read()

{

	ll x=0,f=1;char ch=getchar();

	for(;!isalnum(ch);ch=getchar()) if(ch=='-') f=-1;

	for(;isalnum(ch);ch=getchar()) x=x*10+ch-'0';

	return x*f;

}

ll a[xx],cha[xx],ru[xx*2];

ll n;

inline void cot(ll k,ll x)

{

	while(k<xx*2)

	{

		ru[k]+=x;

		k+=(k&-k);

	}

}

inline ll get(ll k)

{

	ll ans=0;

	while(k>0)

	{

		ans+=ru[k];

		k-=(k&-k);

	}

	return ans;

}

inline bool check(ll k)

{

	memset(ru,0,sizeof(ru));

	cha[0]=0;

	fur(i,1,n) cha[i]=cha[i-1]+(a[i]>=k?1:-1);

	ll ans=0;

	fur(i,0,n)

	{

		ans+=get(cha[i]+xx);

		cot(cha[i]+xx,1ll);

	}

	return ans>=(n*(n+1)>>2);

}

int main()

{

	ll l=0,r=0;

	n=in;

	fur(i,1,n) a[i]=in,r=max(a[i],r);

	while(l<=r)

	{

		ll mid=l+r>>1;

		if(check(mid)) l=mid+1;

		else r=mid-1;

	}

	printf("%d\n",r);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.213788979100242x):
#include <bits/stdc++.h>

using namespace std;



const int N = 110000;



int a[N], sum[N];

int BIT[3 * N];



int main() {

	

	ios::sync_with_stdio(0);

	cin.tie(0), cout.tie(0);

	

	int n; cin >> n;

	for (int i = 1; i <= n; i++) cin >> a[i];

	

	int st = 0, en = 1e9, mid, ans = en;

	

	auto add = [&](int u, int v) {

		for (int i = u; i <= 3 * (n + 1); i += i & -i) BIT[i] += v;

	};

	

	auto get = [&](int u) {

		int ret = 0;

		for (int i = u; i > 0; i -= i & -i) ret += BIT[i];

		return ret;

	};

	

	auto can = [&](int mid) {

		sum[0] = 0;

		for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + 2 * (a[i] <= mid);

		for (int i = 0; i <= 3 * (n + 1); i++) BIT[i] = 0;

		long long cnt = 0;

		add(0 + n + 1, 1);

		for (int i = 1; i <= n; i++) {

			cnt += get(sum[i] - i + n);

			add(sum[i] - i + n + 1, 1);

		}

		return cnt * 2 > (long long)n * (n + 1) / 2;

	};

	

	while (st <= en) {

		mid = st + en >> 1;

		if (can(mid)) ans = mid, en = mid - 1;

		else st = mid + 1;

	}

	cout << ans << "\n";

	

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

 

using namespace std;



int N, A, B, used[100], rings[100], G[100][199];

 

int rec(int pos)

{

    used[pos] = true; int ret = 1;

     

    for(int i = 0; i < rings[pos]; i++)

    {

        if(!used[G[pos][i]])

        {

            ret = max(ret, rec(G[pos][i]) + 1);

        }

    }

     

    used[pos] = false;

     

    return ret;

}

 

int main()

{

    int N, A, B;

     

    while(true)

    {

        scanf("%d", &N);

         

        if(N == 0) { break; }

 

        for(int i = 0; i < 100; i++)

        {

            used[i] = false; rings[i] = 0;

        }

     

        for(int i = 0; i < N; i++)

        {

            scanf("%d", &A); A--;

            scanf("%d", &B); B--;

         

            G[A][rings[A]] = B; rings[A]++;

            G[B][rings[B]] = A; rings[B]++;

        }



        int nodes = 0;

         

        for(int i = 0; i < 100; i++)

        {

            if(rings[i])

            {

                nodes++;

            }

        }

     

        int ret = 0;

     

        for(int i = 0; i < 100; i++)

        {

            ret = max(ret, rec(i));

                 

            if(ret == nodes) { break; }

        }

         

        printf("%d\n", ret);

    }

     

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.943216056403596x):
#include <stdio.h>

#include <algorithm>

#pragma warning(disable : 4996)

using namespace std;

int N, A, B, v[100], r[100], G[100][199];

int rec(int pos) {

	int ret = 0; v[pos] = 1;

	for (int i = 0; i < r[pos]; i++) {

		if (!v[G[pos][i]]) ret = max(ret, rec(G[pos][i]));

	}

	v[pos] = 0;

	return ret + 1;

}

int main() {

	while (scanf("%d", &N), N) {

		for (int i = 0; i < 100; i++) r[i] = 0;

		for (int i = 0; i < N; i++) {

			scanf("%d%d", &A, &B), A--, B--;

			G[A][r[A]++] = B, G[B][r[B]++] = A;

		}

		int ret = 0;

		for (int i = 0; i < 100; i++) ret = max(ret, rec(i));

		printf("%d\n", ret);

	}

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

long long a[100001],n,k;

int main()

{

	cin>>n>>k;

	for(int i=1;i<=n;i++)cin>>a[i];

	long long f[100001];

	memset(f, 0x7F, sizeof(f));

	f[1]=0;

	for(int i=2;i<=n;i++)

		for(int j=1;j<=k&&j<i;j++)

			f[i]=min(f[i-j]+abs(a[i]-a[i-j]),f[i]);

	cout<<f[n];

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.049508652663016x):
#include <cstdio>

#include <cstring>

#include <algorithm>

#include <cmath>

using namespace std;

int n,k,a[100005],f[100005];

int main()

{

	scanf("%d%d",&n,&k);

	for(int i=1;i<=n;++i)

		scanf("%d",a+i);

	memset(f,0x3f,sizeof(f));

	f[1]=0;

	for(int i=2;i<=n;++i)

		for(int j=max(i-k,1);j<i;++j)

			f[i]=min(f[i],f[j]+abs(a[i]-a[j]));

	printf("%d\n",f[n]);

	return 0;

}