#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 24;
int c[200][200];
int t[200][200];
int cost[200][200];
int tim[200][200];

int main(){
  int n, m, K;
  int p, q, r;
  while(cin >> n >> m && (n || m)){
    fill(&cost[0][0], &cost[199][199] + 1, INF);
    fill(&tim[0][0], &tim[199][199] + 1, INF);
    for(int i = 1; i <= m; i++){
      cost[i][i] = tim[i][i] = 0;
      c[i][i] = t[i][i] = 0;
    }

    for(int i = 0; i < n; i++){
      int a, b;
      int d, e;
      cin >> a >> b;
      cin >> d >> e;
      cost[b][a] = min(cost[b][a], d);
      cost[a][b] = min(cost[a][b], d);
      tim[b][a] = min(tim[b][a], e);
      tim[a][b] = min(tim[a][b], e);
    }

    for(int k = 1; k <= m; k++){
      for(int i = 1; i <= m; i++){
	for(int j = 1; j <= m; j++){
	  if(cost[i][j] > cost[i][k] + cost[k][j]){
	    cost[i][j] = cost[i][k] + cost[k][j];
	  }
	  if(tim[i][j] > tim[i][k] + tim[k][j]){
	    tim[i][j] = tim[i][k] + tim[k][j];
	  }
	}
      }
    }
    cin >> K;
    for(int i = 0; i < K; i++){
      cin >> p >> q >> r;
      if(!r){
	cout << cost[p][q] << endl;
      }else{
	cout << tim[p][q] << endl;
      }
    }

  }
  return 0;
}

#include <iostream>
#include<algorithm>
#include <vector>
#include <functional>
#include <limits.h>

template<typename T, typename S>
struct LazySegTree{
    int size;
    std::vector<T> nod;
    std::vector<S> monoid;
    std::vector<bool> flag;
    T t0;
    S s0;
    std::function<T(T,T)> operation;
    std::function<S(S,S)> merge;
    std::function<T(T,S,int)> calc;
    LazySegTree(int n, T _t0, S _s0, std::function<T(T,T)> _operation, std::function<S(S,S)> _merge, std::function<T(T,S,int)> _calc){
        t0 = _t0;
        s0 = _s0;
        operation = _operation;
        merge = _merge;
        calc = _calc;
        size = 1;
        while(n > size) size *= 2;
        nod = std::vector<T> (size*2-1, t0);
        monoid = std::vector<S> (size*2-1, s0);
        flag = std::vector<bool> (size*2-1);
    }
    void disassembly(int k, int l, int r){
        if(r-l>1){
            monoid[k*2+1] = merge(monoid[k*2+1], monoid[k]);
            monoid[k*2+2] = merge(monoid[k*2+2], monoid[k]);
            flag[k*2+1] = true;
            flag[k*2+2] = true;
        }
        nod[k] = calc(nod[k],monoid[k],r-l);
        monoid[k] = s0;
        flag[k] = false;
    }
    S update_query(int a,int b,int k,int l,int r,S x){
        //[a,b)について
        //kは節点の番号,[l,r)はその節点がカバーする範囲
        if(r<=a||b<=l){ //[a,b)と[l,r)が重なって無いとき
            if(flag[k]) return calc(nod[k], monoid[k], r-l);
            return nod[k];
        }
        if(a<=l && r<=b){ //[a,b)が[l,r)を完全に含んでいるとき
            monoid[k] = merge(monoid[k], x);
            flag[k] = true;
            return calc(nod[k], monoid[k],r-l);
        }
        else{
            if(flag[k]) disassembly(k,l,r);
            nod[k] = operation(update_query(a,b,k*2+1,l,(l+r)/2,x), update_query(a,b,k*2+2,(l+r)/2,r,x));
            return nod[k];
        }
    }
    void update(int a, int b, S x){
        // [a,b)をxに更新
        update_query(a,b,0,0,size,x);
    }
    T sum_query(int a,int b,int k,int l,int r){
        if(r<=a||b<=l){
            return t0;
        }
        if(a<=l && r<=b){
            if(flag[k]) return calc(nod[k],monoid[k],r-l);
            else return nod[k];
        }
        if(flag[k]) disassembly(k, l, r);
        return operation(sum_query(a,b,k*2+1,l,(l+r)/2),sum_query(a,b,k*2+2,(l+r)/2,r));
    }
    T sum(int a, int b){
        return sum_query(a,b,0,0,size);
    }
   void deb(){ //全ノードの出力
		for(int i=0;i<size*2-1;i++){
			if(i==size*2-2){
				std::cout << nod[i] <<std:: endl;
			}
			else{
				std::cout << nod[i] << ' ';
			}
		}
        for(int i=0;i<size*2-1;i++){
			if(i==size*2-2){
				std::cout << monoid[i] <<std:: endl;
			}
			else{
				std::cout << monoid[i] << ' ';
			}
		}
		return;
	}
};

int main(){
    int n,q; std::cin >> n >> q;
    std::vector<int> ans(0);
    LazySegTree<long long, long long> lst(n,INT_MAX,0,[](long long a, long long b){return std::min(a,b);}, [](long long a, long long b){return b;},[](long long a, long long b, long long c){return b;});
    for(int i= 0;i<q;i++){
        int w; std::cin >> w;
        if(w == 0){
            int s,t,x; std::cin >> s >> t >> x;
            lst.update(s, t+1, x);
        }
        else{
            int s,t; std::cin >> s;
            ans.push_back(lst.sum(s,s+1));
        }
        //lst.deb();
    }
    for(int i = 0;i<ans.size();i++) std::printf("%d\n", ans[i]);
}


#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int, int> ii;

int n;

string ans, newans;

int main() {
    cin.tie(0), ios::sync_with_stdio(0);
    cin >> n;
    cout << "0\n";
    cout.flush();
    cin >> ans;
    if(ans == "Vacant"){
        cout << "0\n";
        cout.flush();
        cin >> ans;
        exit(0);
    }
    int pos = 0;
    for(int i = 17; i >= 0; i--){
        if(pos + (1 << i) < n){
            cout << pos + (1 << i) << "\n";
            cout.flush();
            cin >> newans;
            if(newans == "Vacant"){
                cout << pos + (1 << i) << "\n";
                cout.flush();
                cin >> newans;
                exit(0);
            }
            if(ans == newans)
                pos += (1 << i);
        }
    }
    cout << pos + 1 << "\n";
    cout.flush();
    cin >> newans;
    exit(0);
}

#include <iostream>
#include <string>

using namespace std;

int main()
{
	int n;
	cin >> n;
	cout << 0 << endl;
	string s;
	cin >> s;

	int a;
	if (s == "Male") {
		a = 1;
	} else if (s == "Female") {
		a = 0;
	}

	int l = 0, r = n;
	while (s != "Vacant") {
		int c = (l + r) / 2;
		cout << c << endl;
		cin >> s;

		if (s == "Male") {
			if (a) {
				if (c & 1 == 1) {
					r = c;
				} else {
					l = c;
				}
			} else {
				if (c & 1 == 1) {
					l = c;
				} else {
					r = c;
				}
			}
		} else if (s == "Female") {
			if (a) {
				if (c & 1 == 1) {
					l = c;
				} else {
					r = c;
				}
			} else {
				if (c & 1 == 1) {
					r = c;
				} else {
					l = c;
				}
			}
		}
	}

	return 0;
}


#include <iostream>

using namespace std;

int main()
{
	int d;

	while (cin >> d)
	{
		cout << 72000000 - 180000 * d + 100 * d * d << endl;
	}

	return 0;
}

#include<stdio.h>
int main()
{
    int a,b,c,d,s,s1;
    scanf("%d%d%d%d",&a,&b,&c,&d);
    if(a<b)
        s=a;
    else
        s=b;
        if(c<d)
            s1=c;
            else
            s1=d;
        printf("%d\n",(s+s1));

}


#include<bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define ll long long
#define rep(i,n) for(int i = 0; i < int(n); i++)
#define vi vector<int>
using namespace std;
const int INF = 1001001001;
const int MOD = 1e9+7;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template<class T> inline bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; }

int main(){
  cin.tie(0), ios::sync_with_stdio(false);
  string s; cin >> s;
  ll n = s.size();
  vector<ll> cnt(26);
  rep(i,n) cnt[s[i]-'a']++;
  ll ans = n*(n-1)/2+1;
  rep(i,26) ans -= cnt[i]*(cnt[i]-1)/2;
  cout << ans;
  return 0;
}

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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<long long> vl;
typedef pair<long long, long long> pl;
typedef vector<vector<long long>> matrix;

#define F 						first
#define S 						second
#define PB 						push_back
#define MP 						make_pair
#define all(c) 					c.begin(), c.end()
#define f(i,a,b) 				for(ll i=a; i<b; i++)
#define rep(i,n) 				f(i,0,n)
#define tr(c,i) 				for(typeof(c).begin() i = c.begin(); i != c.end(); i++)
#define present(c,x) 			(c.find(x) != c.end())	//for set and map
#define cpresent(c,x) 			(find(all(c),x) != c.end())	//for vectors
#define w(t)					ll t; cin>>t; while(t--)
#define epsilon 				1e-9
#define MOD						(ll)1e9

void doublade() {
	ios::sync_with_stdio(false); cin.tie(0); cout.precision(10);
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
}

int main() {
	doublade();
	// w(t) {
		ll n;
		cin >> n;
		ll p = n/500;
		ll rs = 1000*p;
		n -= p*500;
		ll q = n/5;
		rs += 5*q;
		cout<<rs<<"\n";
	// }
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int Mod=998244353;
int n,K;
#define Maxn 305
char S[Maxn];
int a[Maxn],cnt=0;
int dp[Maxn][Maxn][Maxn];
int main(){
	scanf("%s",S+1);scanf("%d",&K);
	n=strlen(S+1);
	S[n+1]='0';
	n++;
	int lst=0;
	for(int i=1;i<=n;++i)
	    if(S[i]=='0'){
	    	a[++cnt]=i-lst-1;
	    	lst=i;
		}
	reverse(a+1,a+cnt+1);
	int sum=0;
	dp[0][0][0]=1;
	for(int i=0;i<cnt;++i){
		for(int j=0;j<=sum;++j)
		    for(int k=0;k<=j;++k){
		    	dp[i+1][j][k]=dp[i][j][k];
		    	if(k){
		    		dp[i+1][j][k]+=dp[i+1][j][k-1];
		    		if(dp[i+1][j][k]>=Mod)dp[i+1][j][k]-=Mod;
				}
			}
		for(int j=0;j<=sum;++j)
		    for(int k=0;k<=j;++k)
		        for(int l=1;l<=a[i+1];++l){
		        	dp[i+1][j+l][k]+=dp[i][j][k];
		        	if(dp[i+1][j+l][k]>=Mod)dp[i+1][j+l][k]-=Mod;
				}
		sum+=a[i+1];
	}
	int Ans=0;
	for(int i=0;i<=min(K,sum);++i){
		Ans+=dp[cnt][i][i];
		if(Ans>=Mod)Ans-=Mod;
	}
	printf("%d\n",Ans);
	return 0;
}

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

#define int long long
#define rep(i,l,r) for(int i=(int)(l);i<(int)(r);i++)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)x.size())
template<class T>bool chmax(T &a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,T b){if(a>b){a=b;return 1;}return 0;}

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

const int inf = 1LL<<60;
const int mod = 1e9 + 7;
const double eps = 1e-9;

/*{
  }*/

signed main(){
  int n;
  string s;
  cin >> n >> s;

  vi a(2*n);
  rep(i, 0, 2*n){
    a[i] = ((s[i] == 'B') + (i%2 == 0)) % 2;
  }

  int ans = 1, cnt = 0;
  rep(i, 0, 2*n){
    if(a[i]){
      (ans *= cnt) %= mod;
      cnt--;
    }else{
      cnt++;
    }
  }

  if(cnt) ans = 0;

  rep(i, 1, n+1) (ans *= i) %= mod;

  cout << ans << endl;

//  rep(i, 0, 2*n) cout << a[i] << " ";
//  cout << endl;

  return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
#define F first
#define S second
#define all(v) (v).begin(),v.end()
#define np next_permutation
using namespace std;
using vi=vector<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,j,n) for(int i=j;i<=n;i++)
#define vii vector<vi>
#define vb vector<bool>
#define pr pair<int,int>
#define vl vector<ll>
#define eb emplace_back
#define umap unordered_map<ll,int>
const int N=(int)1e5+11;

int n,t;
string s;

void solve(){
   cin>>n;
   vl a(n);
   for(int i=0;i<n;i++)
       cin>>a[i];
   cin>>s;
   vl b;
   for(int i=n-1;i>=0;i--){
      ll v=a[i];
      for(auto x:b){
        if(v > (v^x))
           v=v^x;
      }
      if(v){
           if(s[i]=='1'){
             cout<<1<<"\n";
             return;
           }
           b.pb(v);
      }
   }
   cout<<0<<"\n";
}
int main(){
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cin>>t;
   while(t--) solve();
   return 0;
}

#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define FOR(i,x,y) for (register int i=(x);i<(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define Debug(v) for (auto i:v) printf("%lld ",i);puts("")
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define fil(a,b) memset((a),(b),sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pa;
typedef pair<ll,ll> PA;
typedef vector<int> poly;
inline ll read(){
    ll x=0,f=1;char c=getchar();
    while ((c<'0'||c>'9')&&(c!='-')) c=getchar();
    if (c=='-') f=-1,c=getchar();
    while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}

const int N = 210;
int n;
ll a[N];
char s[N];

ll b[N];
inline void init(){
	For(i,0,63) b[i]=0;
}
inline bool insert(ll x){
	Dow(i,63,0) if (x&(1ll<<i)){
		if (!b[i]) return b[i]=x,1;
			else x^=b[i];
	}
	return 0;
}

inline void solve(){
	n=read();
	For(i,1,n) a[i]=read();
	scanf("%s",s+1);
	init();
	Dow(i,n,1) if (s[i]=='0'){
		insert(a[i]);
	} else if (insert(a[i])) return puts("1"),void(0);
	puts("0");
}

int main(){
	int T=read();
	while (T--) solve();
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const char* sol3[3]={"aab","c.b","cdd"};
const char* sol4[4]={"aacd","bbcd","efgg","efhh"};
const char* sol5[5]={"aabbc","dee.c","d..fg","h..fg","hiijj"};
const char* sol6[6]={"aabbc.","ddeec.",".f.g.h",".f.g.h","i.j.kk","i.j.ll"};
const char* sol7[7]={"aa.bb.c",".dd..ec","f.gg.e.","f...h.i",".j..h.i","kj..ll.","k.mm.nn"};

char ans[1005][1005];

int main() {
  int n;
  scanf("%d",&n);
  if (n<=2) {
  	puts("-1");
  	return 0;
  }
  if (n==3) {
  	for(int i=0;i<3;i++) puts(sol3[i]);
  	return 0;
  }
  for(int i=0;i<n;i++)
    for(int j=0;j<n;j++) ans[i][j]='.';
  int d=0;
  while (n-d>=8) {
  	for(int i=0;i<4;i++)
  	  for(int j=0;j<4;j++) ans[d+i][d+j]=sol4[i][j];
  	d+=4;
  }
  if (n-d==4) {
  	for(int i=0;i<4;i++)
  	  for(int j=0;j<4;j++) ans[d+i][d+j]=sol4[i][j];
  }
  else if (n-d==5) {
  	for(int i=0;i<5;i++)
  	  for(int j=0;j<5;j++) ans[d+i][d+j]=sol5[i][j];
  }
  else if (n-d==6) {
  	for(int i=0;i<6;i++)
  	  for(int j=0;j<6;j++) ans[d+i][d+j]=sol6[i][j];
  }
  else {
  	for(int i=0;i<7;i++)
  	  for(int j=0;j<7;j++) ans[d+i][d+j]=sol7[i][j];
  }
  for(int i=0;i<n;i++) puts(ans[i]);
  return 0;
}

#include <bits/stdc++.h>
#include <iostream>
#include <ctime>
using namespace std;
#define _LL long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define vecrep(itr, v) for (auto itr = (v).begin(); itr != (v).end(); itr++)

#define MMAX (1000*1000)
pair<int, int> _next[MMAX];
int _in[MMAX];
int _day[MMAX];

int getindex(int i, int j, int n)
{
    return min(i, j) * n + max(i, j);
}

bool searchsub(int day, int next, queue<int> &q)
{
    if( next < 0 ) return true;
    _in[next]--;
    _day[next] = max(_day[next], day);
    if( _in[next] == 0 ) q.push(next);
    return false;
}

int main()
{
    int n; cin >> n;
    vector<int> first(n);
    rep(i, n * n) { _next[i].first = -1;  _next[i].second = -1; _in[i] = 0; _day[i] = -1;}

    rep(i, n)
    {
        int pre = -1;
        rep(j, n - 1)
        {
            int a; cin >> a; a--;
            int index = getindex(i, a, n);
            if(j == 0)
            {
                 first[i] = index;
            }
            else
            {
                if( _next[pre].first < 0 )
                {
                     _next[pre].first = index;
                }
                else
                {
                     _next[pre].second = index;
                }
            }
            _in[index]++;
            pre = index;
        }
    }

    int ans = 0;
    queue<int> q;
    rep(i, n)
    {
        searchsub(1, first[i], q);
    }

    while(!q.empty())
    {
        int index = q.front();
        q.pop();
        ans = max(ans, _day[index]);
        searchsub(_day[index] + 1, _next[index].first, q);
        searchsub(_day[index] + 1, _next[index].second, q);
    }
    bool flag = false;
    rep(i, n * n)
        if( _in[i] > 0 ) flag = true;

    if( flag )
    {
        cout << -1 << endl;
    }
    else
    {
        cout << ans << endl;
    }

    return 0;
}


//In the name of GOD
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const ll maxn = 2e5 + 100;
const ll maxm = 1e6 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e18;

#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt" , "r+" , stdin) ; freopen("output.txt" , "w+" , stdout);
#define pb push_back
#define Mp make_pair
#define pll pair<ll, ll>
#define F first
#define S second

ll n, m, t, d[maxm];
bool vis[maxm];
pair<pll, ll> e[maxn];
vector<ll> g[maxm], p[maxn], dsu[maxm], sz[maxm];
map<ll, ll> mp[maxn];
queue<ll> q;

ll root(ll c, ll v){
	if(dsu[c][v] != v) dsu[c][v] = root(c, dsu[c][v]);
	return dsu[c][v];
}

void Union(ll c, ll v, ll u){
	v = root(c, v);
	u = root(c, u);
	if(v == u) return;
	if(sz[c][v] < sz[c][u]) swap(v, u);
	dsu[c][u] = v;
	sz[c][v] += sz[c][u];
	return;
}

int main(){
	ll v, u, c;
	fast_io;

	cin >> n >> m;
	for(ll i = 0; i < m; i++){
		cin >> v >> u >> c;
		p[v].pb(c);
		p[u].pb(c);
		if(mp[v][c] == 0){
			dsu[c].pb(dsu[c].size());
			sz[c].pb(1);
			mp[v][c] = dsu[c].size();
		}
		if(mp[u][c] == 0){
			dsu[c].pb(dsu[c].size());
			sz[c].pb(1);
			mp[u][c] = dsu[c].size();
		}
		e[i] = Mp(Mp(v, u), c);
	}
	for(ll i = 0; i < m; i++){
		v = e[i].F.F;
		u = e[i].F.S;
		c = e[i].S;
		Union(c, mp[v][c] - 1, mp[u][c] - 1);
	}
	t = n + 1;
	for(ll i = 1; i < maxm; i++){
		for(ll j = 0; j < dsu[i].size(); j++){
			if(dsu[i][j] == j) sz[i][j] = t++;
		}
	}
	for(ll i = 1; i <= n; i++){
		for(ll j = 0; j < p[i].size(); j++){
			g[i].pb(sz[p[i][j]][root(p[i][j], mp[i][p[i][j]] - 1ll)]);
			g[sz[p[i][j]][root(p[i][j], mp[i][p[i][j]] - 1ll)]].pb(i);
		}
	}
	q.push(1);
	vis[1] = 1;
	while(!q.empty()){
		v = q.front();
		q.pop();
		for(ll i = 0; i < g[v].size(); i++){
			u = g[v][i];
			if(!vis[u]){
				vis[u] = 1;
				d[u] = d[v] + 1;
				q.push(u);
			}
		}
	}
	if(!vis[n]) cout << -1;
	else cout << d[n] / 2;

	return 0;
}

#include <bits/stdc++.h>
#define mod 1000000007
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define nyan "(=^･ω･^=)"
#define mkp make_pair
#define lP pair<ll, ll>
#define iP pair<int,int>
typedef long long ll;
using namespace std;

int N, M, K, A, B, C, D[100000], vis[100000];
map<int, vector<pair<int,bool>>>v[100000];
queue<int>q;

void dfs(int x, int a, int t) {
	vector<pair<int,bool>>&V = v[x][a];
	for (int i = 0; i < V.size(); ++i) {
		if (!V[i].second) {
			V[i].second = true;
			if (D[V[i].first] == t + 1 && vis[V[i].first] != a) {
				vis[V[i].first] = a;
				dfs(V[i].first, a, t);
			}
			if (D[V[i].first] == -1) {
				D[V[i].first] = t + 1;
				q.push(V[i].first);
				dfs(V[i].first, a, t);
			}
		}
	}
}

int main() {
	cin >> N >> M;
	while (M--) {
		cin >> A >> B >> C;
		--A;
		--B;
		v[A][C].push_back(mkp(B, false));
		v[B][C].push_back(mkp(A, false));
	}
	memset(D, -1, sizeof D);
	D[0] = 0;
	q.push(0);
	while (!q.empty()) {
		int x = q.front();
		q.pop();
		for (auto it = v[x].begin(); it != v[x].end(); ++it) {
			dfs(x, it->first, D[x]);
		}
	}
	cout << D[N - 1] << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

using namespace std;

template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

const ll mod=1e9+7;

int main()
{
    ll n,m;
    cin >> n >> m;
    if(n%2==0 && n-2==2*m){
        bool ok=false;
        for(ll l=1,r=n;l<r;l++,r--){
            if(r-l<=n/2 && ok==false){
                r++;
                ok=true;
                continue;
            }
            cout << l << " " << r << endl;
        }
    }
    else{
        for(ll i=1;i<=m;i++){
            cout << i << " " << n+1-i << endl;
        }
    }
    return 0;
}

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

#define int long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define range(i,a,b) ((a)<=(i) && (i)<(b))
#define debug(x) cout << #x << " = " << (x) << endl;
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define SP << " " <<

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vec;
typedef vector<P> pvec;
typedef vector<vector<int>> vvec;
typedef vector<vector<P>> pvvec;
typedef priority_queue<int> PQI;
typedef priority_queue<P> PQP;
typedef priority_queue<int,vector<int>,greater<int>> PQIG;
typedef priority_queue<P,vector<P>,greater<P>> PQPG;

const vector<int> DX = {0, -1, 0, 1, 1, 1, -1, -1};
const vector<int> DY = {1, 0, -1, 0, 1, -1, 1, -1};
constexpr int MOD = (1000000007);
// const int MOD = (998244353);
// const int INF = (1 << 30); // 1073741824
const ll INF = (1LL << 60); // 1152921504606846976
const double PI = (3.141592653589794);
const double EPS = (0.0000000001); // 10^(-10)

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return 1;} return 0;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return 1;} return 0;}
template<class T> inline T ceil(T a, T b) {return T((a + b - 1) / b);}
template<class T> inline T round(T a, T b) {return T(a / b);}
template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; }
template <class T> inline void out(T &a) { bool flag=true; for(auto&x:a){if(flag) {flag=false;} else{ cout << ' '; } cout<<x; } cout << endl; }

//----------------------------------------------------------------
int nmax=200000; // 2*(10^5)
vvec G(nmax);

void solve4ts()
{

    int n,m;
    cin>>n>>m;

    int i1=1;
    int i2=m+2;

    for(int i=m;i>=1;i--){
        cout<<i1<<" "<<i1+i<<endl;
        i1++;
        swap(i1,i2);
    }

}
//-----------------------------------------------------------------

signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15);
    int repeat = 1;
    // cin >> repeat;
    while(repeat--) solve4ts();
}


#include <iostream>
#include <stdio.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <map>
#include <cstdio>

using namespace std;

int main(){
	int a[12];
	while(scanf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d"
				,&a[0],&a[1],&a[2],&a[3],&a[4],&a[5],&a[6],&a[7],&a[8],&a[9],&a[10],&a[11]) != EOF){

		int ans = 0;
		double t = 0, d = 0;
		int sum = 0;
		for(int i = 0;i < 10;i++){
			sum += a[i];
		}
		t = (double)sum / (a[10]+a[11]);
		d = (double)a[10] * t;
		sum = 0;
		for(int i = 0;i < 10;i++){
			sum += a[i];
			if(sum >= d){
				ans = i + 1;
				break;
			}
		}
		cout << ans << endl;
	}

	return 0;
}

#include <algorithm>
#include <cmath>
#include <iostream>

#define MAX_N 100000

using namespace std;

struct Test
{
    long long score, upper, lower;

    long long sum();
};

Test tests[MAX_N];
long long cum[MAX_N + 1];
long long N, X;
long long aokiScore;

bool cmp( Test u, Test v ) { return u.sum() > v.sum(); }

long long Test::sum() { return upper * ( X - score ) + lower * score; }

bool isok( long long k )
{
    long long q = k / X;
    long long r = k % X;

    if ( q == N ) return true;

    long long total = cum[q];

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

        if ( i < q )
        {
            tmp -= tests[i].sum();
            tmp += tests[q].sum();
        }

        tmp += min( r, tests[i].score ) * tests[i].lower;
        tmp += max( 0LL, r - tests[i].score ) * tests[i].upper;

        if ( tmp >= aokiScore ) return true;
    }

    return false;
}

int main()
{
    N = 0;
    aokiScore = 0;
    cin >> N >> X;

    for ( int i = 0; i < N; ++i )
    {
        cin >> tests[i].score >> tests[i].lower >> tests[i].upper;
        aokiScore += tests[i].score * tests[i].lower;
    }

    sort( tests, tests + N, cmp );

    for ( int i = 0; i < N; ++i ) cum[i + 1] = cum[i] + tests[i].sum();

    long long l = 0;
    long long r = N * X;

    while ( l < r )
    {
        long long mid = ( l + r ) / 2;

        if ( isok( mid ) )
            r = mid;
        else
            l = mid + 1;
    }

    cout << l << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template<class T>inline bool chmax(T &a, const T &b) { if (b > a) { a = b; return true; } return false; }
template<class T>inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

std::map<long long, int> primeFact(long long n) {
    std::map<long long, int> res;
    for (long long i=2; i*i<=n; i++) {
        while (n%i == 0) {
            res[i]++;
            n /= i;
        }
    }
    if (n != 1) {
        res[n] = 1;
    }
    return res;
}

long long inv(long long n) {
    long long res = 1;
    while (n > 1) {
        res *= (MOD - MOD/n);
        res %= MOD;
        n = MOD % n;
    }
    return res;
}

void solve() {
    int n;
    ll m;
    cin >> n >> m;

    auto pf = primeFact(m);

    vector<ll> comb(31);
    ll nume = 1, deno = 1;
    for (int i=0; i<31; ++i) {
        comb.at(i) = nume * inv(deno) % MOD;
        nume *= n+i;
        nume %= MOD;
        deno *= i+1;
        deno %= MOD;
    }

    ll res = 1;
    for (auto iter=pf.begin(); iter!=pf.end(); ++iter) {
        res *= comb.at(iter->second);
        res %= MOD;
    }

    cout << res << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);

    solve();

    return 0;
}

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

int main() {
  int N,K;
  cin >> N >> K;
  int sum = 1;
  for(int i = 0; i < N; i++){
    if((sum * 2) - (sum + K) > 0){
      sum += K;
    } else {
      sum *= 2;
    }
  }
  cout << sum << endl;
}


//#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
#define PI 3.14159265359
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const long long INF= 1e+18+1;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll> >vvl;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> T;
const ll MOD=1000000007LL;
string abc="abcdefghijklmnopqrstuvwxyz";
string ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int main(){
  ll n,k;cin>>n>>k;
  cout<<n-k+1<<endl;
}

#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>

#define rep(index,max) for(int index=0;index<max;index++)

using namespace std;

int main(){
    string s1,s2;
    int l1,l2;
    cin>>s1>>s2;
    l1=s1.length();
    l2=s2.length();
    int dp[1010][1010];

    rep(i,l1+1) dp[i][0]=i;
    rep(j,l2+1) dp[0][j]=j;

    rep(i,l1){
        rep(j,l2){
            if(s1[i]==s2[j]){
                dp[i+1][j+1]=dp[i][j];
            }
            else{
                dp[i+1][j+1]=min(min(dp[i+1][j],dp[i][j+1]),dp[i][j])+1;
            }
        }
    }

    cout<<dp[l1][l2]<<endl;
    return 0;
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define ALL(a)     (a).begin(),(a).end()
#define VI         vector<int>
#define MOD 1000000007

typedef long long int ll;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

const ll INF=(ll)1e19;
//const int INF=(1<<30);

int main(){
  int k;
  cin >> k;

  VI col(8,-1);
  REP(i,k){
    int r, c;
    cin>> r >> c;
    col[c] = r;
  }

  VI a = {0,1,2,3,4,5,6,7};
  VI ans;
  do {
    bool ok=true;
    set<int> st;
    REP(i,8){
      if(col[i] != -1 && col[i] != a[i])ok=false;
    }
    if(!ok)continue;

    ok=true;

    REP(i,8){
      FOR(j,i+1,8){
        if(abs(i - j) == abs(a[i] - a[j]))ok=false;
      }
    }

    if(ok){
      ans = a;
      break;
    }

  }while(next_permutation(ALL(a)));

  REP(i,8){
    REP(j,8){
      if(ans[j]==i)cout << "Q";
      else cout << ".";
    }
    cout << endl;
  }
}

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_13_A&lang=ja
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)

// 盤面の縦横サイズ
const int SZ = 8;

// クイーンが配置可能かチェック
bool check(int row, int col, map<int, int> mp) {
    bool ret = true;
    REP(i, SZ) {
        // 左上, 右上
        if (mp.find(row - i) != mp.end()) {
            int t = mp[row - i];
            if (t == col - i || t == col + i) {
                ret = false;
                break;
            }
        }
        // 左下, 右下
        if (mp.find(row + i) != mp.end()) {
            int t = mp[row + i];
            if (t == col - i || t == col + i) {
                ret = false;
                break;
            }
        }
        // 列
        if (mp.find(i) != mp.end()) {
            int t = mp[i];
            if (t == col || t == col) {
                ret = false;
                break;
            }
        }
    }
    return ret;
}

// 深さ優先探索で配置可能な場所を探す。引数は行、配置済みのmap<行,列>
// 答えを出力したらtrueを返す。
bool dfs(int row, map<int, int> mp) {
    // クイーンを8行配置した
    if (row == SZ) {
        // 正解パターンを出力
        REP(row, SZ) {
            // Queenが存在する列
            int t = mp[row];
            REP(col, t) { cout << "."; }
            cout << "Q";
            FOR(col, t + 1, SZ) { cout << "."; }
            cout << "\n";
        }
        return true;
    }
    // その行にQueenが存在しなければ追加
    if (mp.count(row) == 0) {
        REP(col, SZ) {
            if (check(row, col, mp)) {
                mp[row] = col;
                if (dfs(row + 1, mp)) {
                    return true;
                }
            }
        }
    } else {
        if (dfs(row + 1, mp)) {
            return true;
        }
    }
    return false;
}

int main(void) {
    int N;
    cin >> N;
    map<int, int> mp;
    REP(i, N) {
        int a, b;
        cin >> a >> b;
        mp[a] = b;
    }
    dfs(0, mp);
    return 0;
}


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

int main() {

	int n, m; cin >> n >> m;
	ll ansAC = 0;
	ll ansWA = 0;
	vector<bool> usedAC(n, false);
	vector<int> cntWA(n, 0);
	for (int i = 0; i < m; i++) {
		int p; string s; cin >> p >> s;
		--p;
		if (s == "AC") {
			if (usedAC[p]) {}
			else {
				usedAC[p] = true;
				ansAC++;
			}
		}
		else { // s == "WA"
			if (usedAC[p]) {}
			else {
				cntWA[p]++;
			}
		}
	}
	for (int i = 0; i < n; i++) {
		if (usedAC[i]) ansWA += cntWA[i];
	}
	cout << ansAC << " " << ansWA << endl;
	return 0;
}

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <ctime>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {0,1,0,-1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
const int mod = 1e9 + 7;
//const int mod = 998244353;

template< typename G >
struct StronglyConnectedComponents {
  const G &g;
  vector<vector<int>> gg, rg;
  vector< int > comp, order, used;

  StronglyConnectedComponents(G &g) : g(g), gg(g.size()), rg(g.size()), comp(g.size(), -1), used(g.size()) {
    for(int i = 0; i < g.size(); i++) {
      for(auto e : g[i]) {
        gg[i].emplace_back((int) e);
        rg[(int) e].emplace_back(i);
      }
    }
  }

  int operator[](int k) {
    return comp[k];
  }

  void dfs(int idx) {
    if(used[idx]) return;
    used[idx] = true;
    for(int to : gg[idx]) dfs(to);
    order.push_back(idx);
  }

  void rdfs(int idx, int cnt) {
    if(comp[idx] != -1) return;
    comp[idx] = cnt;
    for(int to : rg[idx]) rdfs(to, cnt);
  }

  void build(vector<vector<int>> &t) {
    for(int i = 0; i < gg.size(); i++) dfs(i);
    reverse(begin(order), end(order));
    int ptr = 0;
    for(int i : order) if(comp[i] == -1) rdfs(i, ptr), ptr++;

    t.resize(ptr);
    for(int i = 0; i < g.size(); i++) {
      for(auto &to : g[i]) {
        int x = comp[i], y = comp[to];
        if(x == y) continue;
        t[x].push_back(y);
      }
    }
  }
};

int main(){
	ll n,m; cin >> n >> m;
	vvl G(n);
	vector<vector<int>> g;
	rep(i,m){
		ll s,t; cin >> s >> t;
		G[s].push_back(t);
	}
	StronglyConnectedComponents<vvl> scc(G);
	ll q; cin >> q;
	scc.build(g);
	while(q--){
		ll u,v; cin >> u >> v;
		cout << (scc[u] == scc[v]) << "\n";
	}
}


#include<iostream>
using namespace std;

int main(){
	int n,cnt=0,num=0;
	int base[4]={0};
	cin>>n;
	string s;
	while(1){
		cin>>s;
		if(s=="HIT"){
			base[3]+=base[2];
			base[2]=base[1];
			base[1]=base[0];
			base[0]=1;
		}
		if(s=="HOMERUN"){
			for(int i=0;i<3;i++){
				base[3]+=base[i];
				base[i]=0;
			}
			base[3]++;
		}
		if(s=="OUT"){
			cnt++;
		}
		if(cnt==3){
			cout<<base[3]<<endl;
			cnt=0;
			num++;
			base[0]=base[1]=base[2]=base[3]=0;
		}
		if(num==n)break;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main()
{
    ll sx, sy, tx, ty;
    cin >> sx >> sy >> tx >> ty;

    string L;
    string R;
    rep(i, tx - sx)
    {
        L += 'L';
        R += 'R';
    }

    string U;
    string D;
    rep(i, ty - sy)
    {
        U += 'U';
        D += 'D';
    }

    string ans = R + U + L + D + 'D' + R + 'R' + U + 'U' + 'L' + 'U' + L + 'L' + D + 'D' + 'R';

    cout << ans << endl;

    return 0;
}

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int main(){
    cin.tie(0)->sync_with_stdio(false);

	int sx, sy, tx, ty;
	cin >> sx >> sy >> tx >> ty;

	int dy = ty - sy;
	int dx = tx - sx;

	REP(i, dy) cout << 'U';
	REP(i, dx) cout << 'R';
	REP(i, dy) cout << 'D';
	REP(i, dx) cout << 'L';
	cout << 'L';
	REP(i, dy + 1) cout << 'U';
	REP(i, dx + 1) cout << 'R';
	cout << 'D';
	cout << 'R';
	REP(i, dy + 1) cout << 'D';
	REP(i, dx + 1) cout << 'L';
	cout << 'U';
	cout << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main()
{
  ll n,m;
  cin >> n >> m;

  ll p[m];
  string s[m];
  rep(i,m) cin >> p[i] >> s[i];

  ll acnum[n+1];
  for(ll i = 0; i < m; i++){
    if(s[i] == "AC"){
      acnum[p[i]] = 1;
    }
  }

  ll ac = 0, wa = 0;
  ll ok[n+1];
  for(ll i = 1; i <= n; i++) ok[i] = 0;
  for(ll i = 0; i < m; i++){
    if(s[i] == "WA" && ok[p[i]] == 0 && acnum[p[i]] == 1){
      wa++;
    }
    if(s[i] == "AC" && ok[p[i]] == 0){
      ok[p[i]] = 1;
      ac++;
    }
  }

  cout << ac << " " << wa << endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 100000000;
#define rep1(i, n) for (ll i=0; i<(n); i++)
#define rep2(i, k, n) for (ll i=k; i<(n); i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main() {
    ll n, m;
    cin >> n >> m;
    vector<ll> a(n, -1), s(n), c(n);
    rep1(i, m) cin >> s[i] >> c[i];
    rep1(i, m){
        if (a[s[i]-1]!=-1 && a[s[i]-1]!=c[i]){
            cout << -1 << endl;
            return 0;
        }
        a[s[i]-1] = c[i];
    }
    if (a[0]==0 && n>1){
        cout << -1 << endl;
        return 0;
    }
    rep1(i, n){
        if (i==0 && n>1){
            if (a[i]==-1) cout << 1;
            else cout << a[i];
        }else{
            if (a[i]==-1) cout << 0;
            else cout << a[i];
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
const int N=4e5+5;
int n,m,ty,now,b[N],v[N],vi[N],nex[N],head[N];
void addedges(int x,int y){
	nex[++now]=head[x];
	head[x]=now,v[now]=y;
}
long long c,one,two;
void dfs(int x,int y){
	vi[x]=y;
	for(int i=head[x];i;i=nex[i])
		if(!vi[v[i]])	dfs(v[i],y^1);
		else	if(vi[v[i]]==y)	ty=1;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		addedges(x,y),++b[x];
		addedges(y,x),++b[y];
	}
	for(int i=1;i<=n;i++)
		if(!vi[i]){
			if(!b[i])	++c;
			else{
				ty=0,dfs(i,2);
				if(ty)	++one;
				else	++two;
			}
		}
	printf("%lld\n",c*n*2-c*c+2*two*(two+one)+one*one);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ldouble = long double;

const ll inf = 1ll << 60;
const ll mod = (ll)1e9 + 7;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define print(s) cout << s;
#define println(s) cout << s << endl;
#define printd(s) cout << fixed << setprecision(10) << s << endl;

ll res;

int main()
{
    ll n;
    cin >> n;

    ll res = 1;
    for (ll i = 1; ; i++)
    {

        if (i * i > n)
        {
            break;
        }
        res = i * i;
    }

    println(res)
}

#include <bits/stdc++.h>
using namespace std;
int main()
  {int N, i{}; cin >> N;
  while ((i + 1) * (i + 1) <= N) ++i;
  cout << i * i;}


#include <bits/stdc++.h>

using namespace std;

const int mod = 1e9+7;

int main(){
	int n; cin >> n;
	string s; cin >> s;
	int open=0, ans=1;
	for(int i=0;i<2*n;i++){
		if((s[i]=='B' && (open&1)) || (s[i]=='W' && (!(open&1)))){
			if(open<=0){
				cout << 0 << endl;
				return 0;
			}
			ans=(1ll*ans*open)%mod;
			open--;
		}
		else{
			open++;
		}
	}
	for(int i=1;i<=n;i++){
		ans=(1ll*ans*i)%mod;
	}
	if(open==0){
		cout << ans << endl;
	}
	else{
		cout << 0 << endl;
	}
	return 0;
}

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

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

#define MOD 1000000007
#define REP(i, N) for (int i = 0; i < N; ++i)
#define REP1(i, N) for (int i = 1; i <= N; ++i)
#define RREP(i, N) for (int i = N - 1; i >= 0; --i)
#define ALL(a) a.begin(), a.end()

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  string t = s;
  reverse(ALL(t));
  map<P, ll> mp1, mp2;
  REP(bit, 1 << n) {
    string r1 = "", b1 = "";
    string r2 = "", b2 = "";
    REP(i, n) {
      (bit >> i & 1 ? r1 : b1) += s[i];
      (bit >> i & 1 ? r2 : b2) += t[i];
    }
    mp1[P(r1, b1)]++;
    mp2[P(r2, b2)]++;
  }
  ll ans = 0;
  for (auto a : mp1) {
    if (mp2.find(a.first) != mp2.end()) {
      ans += a.second * mp2[a.first];
    }
  }
  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    string S;
    cin >> S;

    vector<string> vec = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

    int cnt = 0;
    for (string x:vec) {
        if (S == x) {
            cout << 7 - cnt << endl;
        } else {
            cnt++;
        }
    }
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <iomanip>
#include <math.h>
#include <fstream>

using namespace std;

class pqComp
{
public:
    bool operator()(pair<int, int> &a, pair<int, int> &b)
    {
        return a.second < b.second;
    }
};

bool sorting(pair<int, int> a, pair<int, int> b)
{
    return a.second < b.second;
}

int main()
{
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(12);
    cin.tie();
    cout.tie();
    // ifstream cin("in.txt");
    // ofstream cout("out.txt");

    // int t;
    // cin >> t;
    // for (int qwe = 0; qwe < t; qwe++)
    // {
    //     cout << "Case #" << qwe + 1 << ": ";
    // }

    int k;
    string s;
    cin >> k >> s;
    if (s.size() <= k)
    {
        cout << s << '\n';
    }
    else
    {
        cout << s.substr(0, k) << "...\n";
    }

    return 0;
}


// God put a smile upon your face <3

#include <bits/stdc++.h>

#define slld(longvalue) scanf("%lld", &longvalue)

#define ll long long
#define ull unsigned long long
#define pll pair < long long, long long >

#define fastio ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0)

#define pb push_back

#define bug printf("BUG\n")

#define mxlld LLONG_MAX
#define mnlld -LLONG_MAX

#define mxd 2e8
#define mnd -2e8

#define pi 3.14159265359

using namespace std;

bool check(ll n, ll pos)
{
	return n & (1LL << pos);
}

ll Set(ll n, ll pos)
{
	return n = n | (1LL << pos);
}

bool is(ll x)
{
	while(x)
	{
		if(x % 10 == 7) return 1;

		x /= 10;
	}

	return 0;
}

int main()
{
    ll i, j, k, l, m, n, o, r, q;
    ll testcase;
    ll input, flag, tag, ans;

//    freopen("input.txt", "r", stdin);

//    freopen("output.txt", "w", stdout);

    while(cin >> input)
    {
		if(is(input)) cout << "Yes\n";
		else cout << "No\n";
    }

}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    long long n, r = 0, b = 0, g = 0;
    string s;
    cin >> n >> s;

    for (int i = 0; i < n; i++)
    {
        if (s[i] == 'R') r++;
        else if (s[i] == 'B') b++;
        else g++;
    }

    long long ans = r * g * b;
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            if (s[i] == s[j]) continue;

            int k = 2 * j - i;
            if (k >= n) break;
            if(s[k]!=s[i]&&s[k]!=s[j]) --ans;
        }
    }

    cout << ans << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef double db;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef pair<ll,ll> PLL;

#define REP(x,l,u) for(ll x = l; x < u; x++)
#define RREP(x,l,u) for(ll x = l; x >= u; x--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mst(x,v) memset(x, v, sizeof(x))
#define sz(x) (ll)x.size()

string to_string(string s) {return s;}
string to_string(char c) {string s = string(1, c);return s;}
template <typename A, typename B> string to_string(pair<A,B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A> string to_string(vector<A> v) { string s = "("; int first = 1; for (A a : v) { if (!first) { s += ", "; } first = 0; s += to_string(a); } s += ")"; return s; }
template <typename A> string to_string(set<A> v) { string s = "("; int first = 1; for (A a : v) { if (!first) { s += ", "; } first = 0; s += to_string(a); } s += ")"; return s; }

void debug_out() {cerr << endl;}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }

void in() {}
template <typename A> void in(A & x) { cin >> x; }
template <typename A, typename B> void in(pair<A,B> & x) { in(x.first); in(x.second); }
template <typename A> void in(vector<A> & x) { REP(i,0,(ll)x.size()) in(x[i]); }
template <typename Head, typename... Tail> void in(Head & H, Tail & ... T) {in(H); in(T...); }

#ifndef ONLINE_JUDGE
#define debug(...) do { cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); } while (false)
#else
#define debug(...) do { } while (false)
#endif

const ll inf = (ll)1e18 + 5;
const ll mod = 1e9+7;

const ll nax = 100500;
ll F[nax], INVF[nax];

ll pow(ll a, ll b) {
	if (b == 0) return 1;
	ll h = pow(a, b/2);
	ll ans = b % 2 ? h * h % mod * a : h * h;
	ans %= mod;
	return ans;
}

ll ncr(ll n, ll r) {
	// n! / r! / (n-r)!
	ll ans = F[n];
	ans *= INVF[r];
	ans %= mod;
	ans *= INVF[n-r];
	ans %= mod;
	return ans;
}

void solve() {
	ll n, m;
	in(n, m);

	F[0] = 1;
	REP(i,1,nax) F[i] = (F[i-1] * i) % mod;
	REP(i,0,nax) INVF[i] = pow(F[i], mod-2);

	map<ll,ll> factors;
	ll f = 2;
	while (m != 1 && f*f <= m) {
		while (m % f == 0) {
			factors[f]++;
			m /= f;
		}
		f++;
	}
	if (m != 1) factors[m]++;

	ll ans = 1;
	for (PLL a : factors) {
		ans *= ncr(a.second + n - 1, a.second) % mod;
		ans %= mod;
	}
	cout << ans << endl;
}

signed main() {
	ll t = 1;
	REP(i,0,t) solve();
	return 0;
}


#include <bits/stdc++.h>
typedef long long ll;

using namespace std;

vector<vector<int>> G;
vector<int> colors;

bool dfs(int v, int color = 0) {
  colors[v] = color;

  for(auto u : G[v]) {
    if (colors[u] != -1) {
      if (colors[u] == color) return false;
      continue;
    }
    if(!dfs(u, 1 - color)) return false;
  }
  return true;
}

int main() {
  ll n, m;
  cin >> n >> m;
  colors.assign(n, -1);
  G.resize(n);

  for(int i = 0; i < m; ++i) {
    int a , b;
    cin >> a >> b;
    a--; b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  bool bipartite = true;
  if(!dfs(0)) bipartite = false;

  if (bipartite) {
    ll w = 0, b = 0;
    for(int i = 0; i < n; ++i) {
      b += colors[i];
    }
    w = n - b;
    cout << w*b-m << endl;
  } else {
    cout << n*(n-1)/2-m << endl;
  }
  return 0;
}

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define R(a) cin>>a
#define R2(a,b) cin>>a>>b
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int h,w,a[200000+1],b[200000+1];
multiset<int> s;
set<mp> se;
int main(){
	R2(h,w);
	rb(i,1,h)
		R2(a[i],b[i]);
	rb(i,1,w)
		s.insert(0),se.insert(II(i,i));
	set<mp> :: IT l,r,nex;
	rb(i,1,h){
		l=se.lower_bound(II(a[i],-INF));
		r=se.upper_bound(II(b[i],INF));
		if(r!=se.begin()&&l!=se.end()&&l!=r){
			r--;
			int save=(*r).SEC;
			while(1){
//				cout<<"E:"<<(*l).FIR<<","<<(*l).SEC<<endl;
				if(l==r){
					s.erase(s.lower_bound((*l).FIR-(*l).SEC));
					se.erase(l);
					if(b[i]!=w){
						se.insert(II(b[i]+1,save));
//						cout<<"I:"<<b[i]+1<<","<<save<<endl;
						s.insert(b[i]+1-save);
					}
					break;
				}
				l++;
				nex=l;
				l--;
				s.erase(s.lower_bound((*l).FIR-(*l).SEC));
				se.erase(l);
				l=nex;
			}

		}
		if(s.empty()) cout<<"-1\n";
		else cout<<(*s.begin())+i<<endl;
	}
	return 0;
}


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

const int MOD = 1000000007;
const int MAX = 2*1e5;

bool IsPrime(long long n){
  if(n == 1) return false;
  for(long long i = 2; i * i < n; i++){
    if(n%i == 0) return false;
  }
  return true;
}

vector<pair<long long, long long>> PrimeFactorize(long long n){
  vector<pair<long long, long long>> res;
  long long a = 0;
  for(long long i = 2; i * i <= n; i++){
    a = 0;
    if(n%i == 0){
      if(IsPrime(i)){
        while(n % i == 0){
          n /= i;
          a++;
        }
        res.push_back(make_pair(i, a));
    }}}
  if(n!= 1) res.push_back(make_pair(n, 1));
  return res;
}

long long modpow(long long a, long long n, long long mod){
  long long res = 1;
  while(n > 0){
    //nを２進数に分解してビットの立っているところだけ掛け算していく。
    if (n &  1) res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// a inverse (mod m)
// a^(-1) = a^(p-2) (mod p)
long long modinv(long long a, long long mod){
  return modpow(a, mod-2, mod);
}

long long Comb(long long n, long long k){
  if(n < k) return 0;
  if(n < 0 || k < 0) return 0;
  // return fac[n] * (finv[k] * finv[n-k] % MOD) % MOD;
  long long ans = 1;
  for(long long i = 0; i < k; i++){
    ans = ans * (n - i) % MOD;
    ans = ans * modinv(k - i, MOD) % MOD;
  }
  return ans;
}

int main(){
  long long n,m;
  cin >> n >> m;

  vector<pair<long long, long long>> res;
  res = PrimeFactorize(m);

  // CombInit();

  long long ans = 1;
  long long size = res.size();
  for(long long i = 0; i < size; i++){
    long long num = Comb(res[i].second + n - 1, res[i].second);
    // ans = (ans * Comb(res[i].second + n - 1, res[i].second)) % MOD;
    ans = (ans * num) % MOD;
  }

  cout << ans << endl;

  return 0;
}

#include <bits/stdc++.h>
#define rep(a,n) for (ll a = 0; a < (n); ++a)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<vector<ll> > Graph;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1e18;

int main(){
    ll n,m;
    cin >> n >> m;
    vector<ll>a(n);
    rep(i,n){
        cin>>a[i];
        a[i] %= m;
    }
    vector<ll>r(n+1,0);
    map<ll,ll>mp;
    mp[0]++;
    rep(i,n){
        r[i+1] = r[i]+a[i];
        r[i+1] %= m;
        mp[r[i+1]]++;
    }
    ll ans = 0;
    for(auto it = mp.begin();it!=mp.end();it++){
        ll now = it->second;
        ans += (now-1)*now/2;
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
template<class T> void chmax(T&x, T y){if(x < y) x = y;}
int N, K;
vector<int> G[100010];
int ans;

int dfs(int v, int p){
    int mx = 0;
    for(auto u : G[v]){
        chmax(mx, dfs(u, v));
    }
    if(mx == K - 1 && p > 1) mx = -1, ans++;
    return mx + 1;
}

int main(){
    cin >> N >> K;
    for(int i = 1; i <= N; i++){
        int p;
        cin >> p;
        if(i == 1) ans += (p == 1 ? 0 : 1);
        else G[p].push_back(i);
    }
    dfs(1, 0);
    cout << ans << endl;
    return 0;
}

#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define pqueue priority_queue
const int inf=1e9+7;
const ll mod=1e9+7;
const ll mod1=998244353;
const ll big=1e18;
const double PI=2*asin(1);

int N, K;
int parent[100005];
int doub[32][100005];
int depth[100005];
int ok[100005];
vector<vector<int> > edge(100005);

void prepare() {
  for(ll i=0;i<N;++i) {
    doub[0][i] = parent[i];
  }
  for(ll i=1;i<32;++i) {
    for(ll j=0;j<N;++j) {
      doub[i][j] = doub[i-1][doub[i-1][j]];
    }
  }
}

void getdepth(ll u, ll dist) {
  depth[u] = dist;
  for(ll i=0;i<edge[u].size();++i) {
    getdepth(edge[u][i], dist+1);
  }
  return;
}

void getok(ll u) {
  if(ok[u]) return;
  ok[u] = true;
  for(ll i=0;i<edge[u].size();++i) {
    getok(edge[u][i]);
  }
  return;
}

ll getcut(ll u) {
  ll h=K-1;
  ll two;
  ll ans=u;
  ll tmp;
  while(h>0) {
    two = 1;
    tmp = 0;
    while(2*two<h) {
      two *= 2;
      tmp++;
    }
    h -= two;
    ans = doub[tmp][ans];
  }
  return ans;
}

int main() {
  cin>>N>>K;
  ll tmp;
  ll ans = 0;
  for(ll i=0;i<N;++i) {
    cin>>tmp;
    tmp--;
    if(i==0 && tmp!=0) ans++;
    if(i!=0) {
      parent[i] = tmp;
      edge[tmp].push_back(i);
    }
  }
  prepare();
  /**
  for(ll i=0;i<10;++i) {
    for(ll j=0;j<N;++j) {
      cout<<doub[i][j]<<" ";
    }
    cout<<endl;
  }
  **/
  getdepth(0, 0);
  vector<pair<ll, ll> > arr(N);
  for(ll i=0;i<N;++i) arr[i] = mp(depth[i], i);
  sort(arr.begin(), arr.end(), greater<pair<ll, ll> >());
  ll cut;
  for(ll i=0;i<N;++i) {
    if(ok[arr[i].se]) continue;
    if(arr[i].fi<=K) continue;
    ans++;
    cut = getcut(arr[i].se);
    getok(cut);
  }
  cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,n-1,0)
#define all(v) v.begin(), v.end()
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n; cin >> n;
  vector<int> A(n);
  rep(i, n) cin >> A[i];
  double ave = (double)accumulate(all(A), 0)/n;
  double mn = inf;
  int ans = 0;
  rep(i, n) {
    if(abs(A[i]-ave) < mn) {
      ans = i;
      mn = abs(A[i]-ave);
    }
  }
  cout << ans << "\n";
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)

int main(){
    int N;
    cin >> N;
    int sum = 0;
    double ave = 0;
    double mini = 100;
    double aa = 0;
    int ans = 0;
    vector<int> a(N);
    for(int i = 0;i < N;i++){
        cin >> a.at(i);
        sum += a.at(i);
    }
    ave =  (double)sum/(double)N;
    /*cout << ave << endl;*/

    for(int i = 0;i < N;i++){
        aa = abs(ave - a.at(i));
        if(mini > aa){
            mini = aa;

            ans = i;
        }
    }
    cout << ans << endl;
}


#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 gc getchar_unlocked
#define fo(i,n) for(int i=0;i<n;i++)
#define Fo(i,k,n) for(int i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long int
#define s(x)	cin>>x
#define p(x)	cout<<x<<"\n"
#define goog(x, y)  cout<<"Case #"<<x<<": "<<y<<"\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF 100000000000
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pl;
typedef vector<int>		vi;
typedef vector<ll>		vl;
typedef vector<pii>		vpi;
typedef vector<pl>		vpl;
typedef vector<vi>		vvi;
typedef vector<vl>		vvl;
typedef long double ld;

mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
int mpow(int base, int exp);
void ipgraph(int n, int m);
void dfs(int u, int par);
int gcd(int a, int b) ;

const int mod = 1'000'000'007;
const int N = 3e5, M = N;
//=======================

vi g[N];
int a[N];

void solve() {
  int n;cin>>n;
  ll a[n];
  fo(i,n) cin>>a[i];
  ll cont[n];
  cont[0]=a[0];
  Fo(i,1,n)
  cont[i]=(cont[i-1]+a[i])%mod;
  ll res[n];
  res[0]=0;
  Fo(i,1,n)
  { res[i]=(res[i-1]+a[i]*cont[i-1])%mod;}
  p(res[n-1]);
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

    int t = 1;
    //cin >> t;
    while(t--) {
      solve();
    }

    return 0;
}

int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}

void ipgraph(int n, int m){
	int i, u, v;
	while(m--){
		cin>>u>>v;
    //u--, v--;
		g[u].pb(v);
		g[v].pb(u);
	}
}

void dfs(int u, int par){
	for(int v:g[u]){
		if (v == par) continue;
		dfs(v, u);
	}
}

int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

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

int main() {
    int64_t N; cin >> N;
    vector<int64_t> A(N);
    int64_t MOD = 1e9 + 7;
    for (int i = 0; i < N; i++) cin >> A.at(i);

    int64_t sum = 0;
    /* O(N^2)
    for (int i = 0; i < N - 1; i++) {
        for (int j = i + 1; j < N; j++) {
            if (i < j && j <= N) sum += (A.at(i) * A.at(j)) % MOD;
        }
    }
    */

    vector<int64_t> B(N + 1);
    for (int i = 0; i < N; i++) B.at(i + 1) = (B.at(i) + A.at(i)) % MOD;

    int64_t ans = 0;
    for (int i = 0; i < N - 1; i++) {
        ans += A.at(i) * (B.at(N) - B.at(i + 1) + MOD);
        ans %= MOD;
    }
    cout << ans << endl;
    return 0;
}


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

int main () {
  int64_t N, K;
  cin >> N >> K;
  vector<int> p(N);
  vector<double> memo(N + 1);
  memo.at(0) = 0;
  for (int64_t i = 0; i < N; i++) {
    cin >> p.at(i);
    memo.at(i + 1) = (memo.at(i) + ((p.at(i) + 1) / 2.0));
  }
  double ans = 0;
  for (int64_t i = 0; i <= N - K; i++) {
    double M = (memo.at(i + K) - memo.at(i));
    ans = max(M, ans);
  }
  cout << fixed << setprecision(16);
  cout << ans << endl;
}

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstring>
#define _GLIBCXX_DEBUG

#include <iomanip>
using namespace std;

template<typename T> vector<T> range(T begin, T end, T stride = 1) {
    vector<T> vec;
    for (T i = begin; i < end; i += stride) {
        vec.push_back(i);
    }
    return vec;
}

template<typename T> T factorial(T in) {
    long long ans = 1;
    for (int i=2; i < in + 1; ++i) ans *= i;
    return ans;
}

int main() {
    int n; cin >> n;
    vector<int> v = range(0, n);
    vector<int> x(n), y(n);
    for (int i=0; i < n; ++i) cin >> x[i] >> y[i];
    double sum = 0;
    do {
        for (int i=0; i<n-1; i++) {
            sum += sqrt((x[v[i+1]] - x[v[i]]) * (x[v[i+1]] - x[v[i]]) + (y[v[i+1]] - y[v[i]]) * (y[v[i+1]] - y[v[i]]));
        }
    } while(next_permutation(v.begin(), v.end()));
    cout << fixed << setprecision(15) << sum / factorial(n) << endl;
}


#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 2e5;
const int posp[4] = {3, 2, 1, 0}, negp[4] = {1, 0, 3, 2}, parp[4] = {2, 3, 0, 1};
const bool post[4] = {1, 1, 0, 0}, negt[4] = {0, 1, 1, 0}, part[4] = {1, 1, 0, 0};

int n, x[N + 1], y[N + 1], u[N + 1], id[256], ans = 0x7fffffff;
char c;
vector<int> pos[4][N * 2 + 1], neg[4][N * 2 + 1], par[4][N + 1];
vector<int>::iterator it;

int main(){
	id['U'] = 0, id['R'] = 1, id['D'] = 2, id['L'] = 3;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d%d %c", x + i, y + i, &c);
		u[i] = id[c];
		if(u[i] == 0 || u[i] == 2)
			par[u[i]][x[i]].push_back(y[i]);
		else par[u[i]][y[i]].push_back(x[i]);
		pos[u[i]][N + y[i] - x[i]].push_back(x[i]);
		neg[u[i]][x[i] + y[i]].push_back(x[i]);
	}
	for(int i = 0; i < 4; i++)
		for(int j = 0; j <= N; j++)
			sort(par[i][j].begin(), par[i][j].end());
	for(int i = 0; i < 4; i++)
		for(int j = N * 2; ~j; j--){
			sort(pos[i][j].begin(), pos[i][j].end());
			sort(neg[i][j].begin(), neg[i][j].end());
		}
	for(int i = 1; i <= n; i++){
		if(u[i] == 0 || u[i] == 2){
			if(part[u[i]]){
				if(par[parp[u[i]]][x[i]].size() && par[parp[u[i]]][x[i]].back() > y[i])
					ans = min(ans, *lower_bound(par[parp[u[i]]][x[i]].begin(), par[parp[u[i]]][x[i]].end(), y[i]) - y[i]);
			}else if(par[parp[u[i]]][x[i]].size() && par[parp[u[i]]][x[i]].front() < y[i])
				ans = min(ans, y[i] - *--lower_bound(par[parp[u[i]]][x[i]].begin(), par[parp[u[i]]][x[i]].end(), y[i]));
		}else{
			if(part[u[i]]){
				if(par[parp[u[i]]][y[i]].size() && par[parp[u[i]]][y[i]].back() > x[i])
					ans = min(ans, *lower_bound(par[parp[u[i]]][y[i]].begin(), par[parp[u[i]]][y[i]].end(), x[i]) - x[i]);
			}else if(par[parp[u[i]]][y[i]].size() && par[parp[u[i]]][y[i]].front() < x[i])
				ans = min(ans, x[i] - *--lower_bound(par[parp[u[i]]][y[i]].begin(), par[parp[u[i]]][y[i]].end(), x[i]));
		}
		if(post[u[i]]){
			if(pos[posp[u[i]]][N + y[i] - x[i]].size() && pos[posp[u[i]]][N + y[i] - x[i]].back() > x[i])
				ans = min(ans, (*lower_bound(pos[posp[u[i]]][N + y[i] - x[i]].begin(), pos[posp[u[i]]][N + y[i] - x[i]].end(), x[i]) - x[i]) * 2);
		}else if(pos[posp[u[i]]][N + y[i] - x[i]].size() && pos[posp[u[i]]][N + y[i] - x[i]].front() < x[i])
			ans = min(ans, (x[i] - *--lower_bound(pos[posp[u[i]]][N + y[i] - x[i]].begin(), pos[posp[u[i]]][N + y[i] - x[i]].end(), x[i])) * 2);
		if(negt[u[i]]){
			if(neg[negp[u[i]]][x[i] + y[i]].size() && neg[negp[u[i]]][x[i] + y[i]].back() > x[i])
				ans = min(ans, (*lower_bound(neg[negp[u[i]]][x[i] + y[i]].begin(), neg[negp[u[i]]][x[i] + y[i]].end(), x[i]) - x[i]) * 2);
		}else if(neg[negp[u[i]]][x[i] + y[i]].size() && neg[negp[u[i]]][x[i] + y[i]].front() < x[i])
			ans = min(ans, (x[i] - *--lower_bound(neg[negp[u[i]]][x[i] + y[i]].begin(), neg[negp[u[i]]][x[i] + y[i]].end(), x[i])) * 2);
	}
	if(ans == 0x7fffffff) printf("SAFE");
	else printf("%d", ans * 5);
	return 0;
}

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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
typedef long long ll;
const int INT_INF = 1e9;
const ll INF = 1LL << 30;
const ll MOD = 1e9 + 7;

const ll limit = 100010;
using edge = struct { ll to; ll cost; };
V<edge> G[limit];
ll dis[limit];

void dfs(ll v, ll p, ll d){
    dis[v] = d;
    for(auto g : G[v]){
        if(g.to == p) continue;
        dfs(g.to, v, d + g.cost);
    }
}

int main() {
    ll n; cin >> n;
    for(int i = 0; i < n - 1; i++){
        ll a, b, c; cin >> a >> b >> c;
        a--, b--;
        G[a].push_back({b, c});;
        G[b].push_back({a, c});
    }

    ll q, k; cin >> q >> k;
    k--;

    dfs(k, -1, 0);

    for(int i = 0; i < q; i++){
        ll x, y; cin >> x >> y;
        x--, y--;
        cout << dis[x] + dis[y] << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
inline char Get(){
	static char ch; ch = getchar();
	while (ch != '\n' && ch != '0' && ch != '1' && ch != '?') ch = getchar();
	if (ch == '\n') return '*';
	return ch;
}
const int P = 1e9 + 7;
inline void upd(int &x,int v){ x = (x+v>=P)?(x+v-P):(x+v); }
int f[9],g[9],nxt[9][2];
inline int getnxt(int s,int v){
	int cnt1 = s / 3,cnt0 = s % 3;
	if (v == 1){ if (!cnt0) cnt1 = min(2,cnt1+1); else --cnt0; }
	else{ if (cnt0 == 2) cnt0 = 0; ++cnt0; }
	return cnt1 * 3 + cnt0;
}

inline void DP(char c){
	int i;
	for (i = 0; i < 9; ++i) g[i] = f[i],f[i] = 0;
	for (i = 0; i < 9; ++i) if (g[i]){
		if (c != '1') upd(f[nxt[i][0]],g[i]);
		if (c != '0') upd(f[nxt[i][1]],g[i]);
	}
}

int main(){
	int i,j;
	for (i = 0; i < 9; ++i) for (j = 0; j < 2; ++j) nxt[i][j] = getnxt(i,j);
	f[0] = 1;
	char c; while ((c=Get())!='*') DP(c);
	int ans = 0;
	for (i = 0; i < 9; ++i) if (i / 3 >= i % 3) upd(ans,f[i]);
	cout << ans << '\n';
	return 0;
}

// This amazing code is by Eric Sunli Chen.
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
template<typename T> bool get_int(T &x)
{
	char t=getchar();
	bool neg=false;
	x=0;
	for(; (t>'9'||t<'0')&&t!='-'&&t!=EOF; t=getchar());
	if(t=='-')neg=true,t=getchar();if(t==EOF)return false;
	for(; t<='9'&&t>='0'; t=getchar())x=x*10+t-'0';
	if(neg)x=-x;return true;
}
template<typename T> void print_int(T x)
{
	if(x<0)putchar('-'),x=-x;
	short a[20]= {},sz=0;
	while(x>0)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1; i>=0; i--)putchar('0'+a[i]);
}
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define get1(a) get_int(a)
#define get2(a,b) (get1(a)&&get1(b))
#define get3(a,b,c) (get1(a)&&get2(b,c))
#define printendl(a) print_int(a),puts("")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const LL Linf=1ll<<61;
const double pi=acos(-1.0);

const int mod=1e9+7;
inline void add(int&x,const int&y){x+=y;if(x>=mod)x-=mod;}

char s[300111];
int n,dp[300111][3][2][3];
int main()
{
	scanf("%s",s+1);
	n=strlen(s+1);s[++n]='1';
	dp[1][0][0][0]=1;
	for(int i=1;i<=n;i++)for(int v=0;v<3;v++)for(int p=0;p<2;p++)for(int cur=0;cur<3;cur++)
	{
		if(s[i]!='1')add(dp[i+1][v==0?1:3-v][p][cur],dp[i][v][p][cur]);
		if(s[i]!='0')
		{
			if(v==1)add(dp[i+1][0][p][cur],dp[i][v][p][cur]);
			else
			{
				int nc=cur;
				if(cur==0&&v==0&&!p)nc=1;
				else if(cur==1&&v==0&&p)nc=2;
				add(dp[i+1][0][p^1][nc],dp[i][v][p][cur]);
			}
		}
	}
	printf("%d\n",(dp[n+1][0][0][2]+dp[n+1][0][1][2])%mod);
	return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repe(i, n) for (int i = 0; i <= (n); ++i)
#define all(x) (x).begin(),(x).end()
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
typedef long long ll;
const int INF = 1e9;
const ll mod = 1000000007;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a*b / gcd(a,b);}

int main() {
    string s;
    cin >> s;
    if (s.size() < 3) {
        cout << s << "\n";
    } else {
        cout << s[2] << s[1] << s[0] << "\n";
    }
}

#include <iostream>
#include <math.h>

using namespace std;
void Solve(int a)
{
	const double pi = 3.14;
	float ans;
	ans = 2 * pi * a;
	cout << ans;
}
int main()
{
	int x;
	cin >> x;
	Solve(x);
	return 0;
}


#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>

using namespace std;

int main()
{
	string S;
	cin >> S;

	for (int i = 0; i < S.length() - 8; i++) {
		cout << S[i];
	}
	cout << endl;

}

#include <iostream>
#include <map>

int main(void){
    char ans='0';
    std::string S;
    std::cin >> S;
    std::map<char, int> mp;
    for (int i=0; i<26; ++i){
        mp[(char)('a' + i)] = 0;
    }

    for (char c: S) ++mp[c];

    for (int i=0; i<26; ++i){
        if (mp[(char)('a' + i)] == 0){
            ans = (char)('a' + i);
            break;
        }
    }

    if (ans == '0') {
        std::cout << "None" << std::endl;
    } else {
        std::cout << ans << std::endl;
    }

    return 0;
}


#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <algorithm>
#include <iomanip>
#include <functional>
#include <numeric>

#define REP(i, n) for(int i = 0;i < (n); i++)
#define REP2(i, x, n) for(int i = (x); i < (n); i++)
#define REPR(i, n) for(int i = (n); i >= 0; i--)

#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

#define LL long long int
#define LD long double

#define PI 3.14159265358979

using namespace std;

//================================================

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

	string S; cin >> S; reverse(ALL(S)); cout << S << "\n";
	return 0;
}

#include <iostream>
using namespace std;
void reverseSequence()
{
	char s[20];
	cin >> s;
	int i;

	for (i = 0; i < 20; i++)
	{
		if (s[i] == '\0')
		{
			break;
		}
	}
	for (int j = i - 1; j >= 0; j--)
	{
		cout << s[j];
	}
	cout << endl;
}

int main()
{
	reverseSequence();
	return 0;
}


#include <iostream>
#include <vector>

int main()
{
    long long N;
    std::cin >> N;
    long long n_r = 0, n_g = 0, n_b = 0;
    std::vector<char> S(N);
    for (long long i = 0; i < N; i++)
    {
        char c;
        std::cin >> c;
        S[i] = c;
        if (c == 'R')
        {
            n_r++;
        }
        else if (c == 'G')
        {
            n_g++;
        }
        else if (c == 'B')
        {
            n_b++;
        }
        else
        {
            return -1;
        }
    }

    long long count = 0;
    for (long long j = 1; j < N - 1; j++)
    {
        for (long long diff = 1; j - diff >= 0 && j + diff < N; diff++)
        {
            long long i = j - diff;
            long long k = j + diff;
            if (S[i] != S[j] && S[j] != S[k] && S[k] != S[i])
            {
                count++;
            }
        }
    }
    std::cout << n_r * n_g * n_b - count << std::endl;
}

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, f, n) for (ll i = (f); i < (n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define repv(v) for (auto it = (v).begin(); it != (v).end(); it++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define INIT cin.tie(0); ios::sync_with_stdio(false);

template <class T>
inline bool chmax(T& a, T b) {
    return a = (a < b) ? b : a;
}
template <class T>
inline bool chmin(T& a, T b) {
    return a = (a > b) ? b : a;
}

ll const INF = 1LL << 60;
ll const MOD = 1000000007;

int main() {
    INIT;

    ll N, R;
    cin >> N >> R;

    cout << max(0LL, 10LL - N) * 100 + R << endl;

    return 0;
}


///Bismillahir Rahmanir Rahim
#include "bits/stdc++.h"

#define                           ll                          long long
#define                           int                         ll
#define                           fi                          first
#define                           si                          second
#define                           mp                          make_pair
#define                           pb                          push_back
#define                           pi                          pair<ll,ll>
#define                           clr(x)                      memset(x,0,sizeof(x));
#define                           f(i,l,r)                    for(int i=l;i<=r;i++)
#define                           rf(i,r,l)                   for(int i=r;i>=l;i--)
#define                           done(i)                     cout<<"done = "<<i<<endl;
#define                           show(x,y)                   cout<<x<<" : ";for(auto z:y)cout<<z<<" ";cout<<endl;
#define                           fast                        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

const ll inf=1e18;
const int mod=1e9+7;
const int M=1000005;
inline ll bigmod(ll B,ll P){ll R=1;while(P>0){if(P&1){R=(R*B)%mod;}P>>=1;B=(B*B)%mod;}return R;}
inline int gun(int x,int y)
{
    int ret=(x*y)%mod;
    if(ret<0){ret+=mod;ret=ret%mod;}
    return ret;
}
bitset<M>sv;
vector<int>prime;
int fact[M];
int infact[M];
void sieve()
{
    prime.pb(2);
    for(int i=4;i<M;i=i+2)sv[i]=1;
    for(int i=3;i*i<M;i=i+2)
    {
        if(sv[i]==0){

        for(int j=i*i;j<M;j=j+2*i)
        {
           sv[j]=1;
        }

        }
    }
    for(int i=3;i<M;i=i+2)
    {
        if(sv[i]==0)prime.pb(i);
    }
}
void init()
{
    fact[0]=1;
    infact[0]=1;
    f(i,1,M-1)
    {
        fact[i]=(fact[i-1]*i)%mod;
    }
    infact[M-1]=bigmod(fact[M-1],mod-2);
    rf(i,M-2,1)
    {
        infact[i]=(infact[i+1]*(i+1))%mod;
    }
}
vector<int>p,c;
void prime_factorization(int x)
{
    p.clear();c.clear();
    for(int i=0;prime[i]*prime[i]<=x;i++)
    {
        int cnt=0;
        if(x%prime[i]==0)
        {
            while(x%prime[i]==0)
            {
                x=x/prime[i];
                cnt++;
            }
            p.pb(prime[i]);
            c.pb(cnt);
        }
    }
    if(x>1)
    {
        p.pb(x);
        c.pb(1);
    }
}
int star_bar(int bol,int box)
{
   int ret=fact[bol+box-1];
   ret=gun(ret,infact[box-1]);
   ret=gun(ret,infact[bol]);
   return ret;
}
 main()

{
    fast
    init();
    sieve();
    int n,m;
    cin>>n>>m;
    prime_factorization(m);
    int sz=c.size();
    int ses=1;
    for(int i=0;i<sz;i++)
    {
        ses=gun(ses,star_bar(c[i],n));
    }
    cout<<ses<<endl;
    return 0;

}

#include <bits/stdc++.h>

using namespace std;

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

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

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

const int N = (int)1e5 + 9;
const int LOG = 40;
const int MOD = (int)1e9 + 7;

int dp[N][LOG];

int main(){
  fastIO;
  int n, m;
  cin >> n >> m;
  dp[0][0] = 1;
  int s;
  for(int i = 1; i <= n; i ++ ){
    s = 0;
    for(int j = 0 ; j < LOG; j ++ ){
      s += dp[i - 1][j];
      s %= MOD;
      dp[i][j] = s;
    }
  }
  int cnt;
  int ans = 1;
  for(int i = 2; i * i <= m ; i ++ ){
    if(m % i != 0) continue;
    cnt = 0;
    while(m % i == 0){
      cnt ++ ;
      m /= i;
    }
    ans = (ans * 1ll * dp[n][cnt]) % MOD;
  }
  if(m != 1) ans = (ans * 1ll * dp[n][1]) % MOD;
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int MOD=1e9+7;

int main(void){
    int h,w;
    cin>>h>>w;
    vector<string> M(h);
    rep(i,h) cin>>M[i];
    queue<pair<int,int>> que;
    vector<vector<int>> seen(h,vector<int>(w,-1));
    rep(i,h) rep(j,w){
        if(M[i][j]=='#'){
            que.push(make_pair(i,j));
            seen[i][j]=0;
        }
    }
    int dx[]={1,0,-1,0};
    int dy[]={0,1,0,-1};
    while(!que.empty()){
        int x=que.front().first;
        int y=que.front().second;
        que.pop();
        rep(dir,4){
            int nx=x+dx[dir];
            int ny=y+dy[dir];
            if(nx<0 || nx>=h || ny<0 ||ny>=w) continue;
            if(seen[nx][ny]!=-1) continue;
            seen[nx][ny]=seen[x][y]+1;
            que.push(make_pair(nx,ny));
        }
    }

    int ans=0;
    rep(i,h){
        rep(j,w){
            //cout<<seen[i][j]<<" ";
            ans=max(ans,seen[i][j]);
        }
        //cout<<endl;
    }
    cout<<ans<<endl;
}

#include <bits/stdc++.h>
#include <numeric>
#define rep(i,n) for (int i = 0; i < n; ++i)
#define per(i,n) for (int i = n - 1; i >= 0; --i)
#define rep1(i,n) for (int i = 1; i <= n; ++i)
template<typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> inline T intceil(T a, T b) { return (a + (b - 1)) / b; }
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;
const long long MOD = 1e9+7;
#define precout() cout << std::fixed << std::setprecision(20);
const string alphabet = "abcdefghijklmnopqrstuvwxyz";
const int dy[4] = { 0, 1, 0, -1 };
const int dx[4] = { 1, 0, -1, 0 };
static const double pi = 3.141592653589793;

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

  int n; cin >> n;
  ll prev; cin >> prev;
  bool ok = 1;

  if(prev > 0) {
    ok = 0;
  }

  ll ans = 0;
  rep(i, n - 1) {
    ll tmp; cin >> tmp;
    if(!ok) continue;

    if(tmp == 0) {
      prev = tmp;
      continue;
    }

    ll diff = tmp - prev;
    if(diff > 1) {
      ok = 0;
      continue;
    }

    if(diff == 1) {
      ++ans;
    } else {
      ans += tmp;
    }

    prev = tmp;
  }

  if(ok) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
}


#include "bits/stdc++.h"
using namespace std;
using ll = long long; using ull = unsigned long long;
#define int ll
using vb = vector<bool>; using vvb = vector<vb>;
using vi = vector<int>; using vvi = vector<vi>;
using vl = vector<ll>; using vvl = vector<vl>;
using pii = pair<int,int>; using pll = pair<ll,ll>;
#define FOR(i,a,b) for(ll i = (a); i < (ll)(b); ++i)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(ll i = (ll)(b)-1;i >= a;--i)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define eb(val) emplace_back(val)
const double PI = acos(-1);
const double EPS = 1e-10;
const ll MOD = 1e9+7;
void cioacc(){//accelerate cin/cout
  cin.tie(0);
  ios::sync_with_stdio(false);
}
struct dice{
  int up,s,e,w,n,dn;
  void to(char dir){
    if(dir=='N'){
      int tmp = n;
      n = up;
      up = s;
      s = dn;
      dn = tmp;
    }else if(dir=='S'){
      int tmp = s;
      s = up;
      up = n;
      n = dn;
      dn = tmp;
    }else if(dir=='E'){
      int tmp = e;
      e = up;
      up = w;
      w = dn;
      dn = tmp;
    }else if(dir=='W'){
      int tmp = w;
      w = up;
      up = e;
      e = dn;
      dn = tmp;
    }
  }
  void to(string dirs){
    REP(i,dirs.size()) to(dirs[i]);
  }
};
signed main(){
  int a[6];
  REP(i,6) cin >> a[i];
  dice di = {a[0],a[1],a[2],a[3],a[4],a[5]};
  string s;
  cin >> s;
  di.to(s);
  cout << di.up << endl;
}

#include <iostream>

const int N = 7;

typedef struct {
  int panels[N];
  int upper;
  int east;
  int south;
} Dice;

void rotate_dice(int *x, int *y, int d) {
  *x = *y;
  *y = d;
}

int main(void) {
  Dice dice;

  for (int i = 1; i < N; i++) {
    std::cin >> dice.panels[i];
  }

  dice.upper = 1;
  dice.east = 3;
  dice.south = 2;

  std::string input;
  std::cin >> input;

  for (int i = 0; i < input.size(); i++) {
    char m = input[i];

    if (m == 'E') {
      rotate_dice(&dice.east, &dice.upper, N - dice.east);
    } else if (m == 'W') {
      rotate_dice(&dice.upper, &dice.east, N - dice.upper);
    } else if (m == 'S') {
      rotate_dice(&dice.south, &dice.upper, N - dice.south);
    } else {
      rotate_dice(&dice.upper, &dice.south, N - dice.upper);
    }
  }

  std::cout << dice.panels[dice.upper] << std::endl;

  return 0;
}


#include<iostream>
#include<string>

int main() {
	int K;
	std::string S;
	std::cin >> K >> S;
	if (S.size() <= K) std::cout << S << std::endl;
	else {
		std::string answer = S.substr(0, K);
		answer += "...";
		std::cout << answer << std::endl;
	}
	return 0;
}

#include <bits/stdc++.h>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string.h>
using namespace std;
#define pb push_back
#define all(v) v. begin(),v. end()
#define  rep(i,n,v) for(i=n;i<v;i++)
#define per(i,n,v) for(i=n;i>v;i--)
#define ff first
#define ss second
#define pp pair<ll,ll>
#define eps 1e-6
#define ll  long long
#define endl '\n'
void solve()
{
  ll n, a,m=0,b=1, k=0, i, j,l=2e9+7;
  string s, r, y;
  cin>>n>>s;
 rep(i,0,s.length())
 {
   if(i==n) break;
   cout<<s[i];
 }
 if(i!=s. length())
 cout<<"...";
}
int main()
{
 ios_base::sync_with_stdio(false);
  cin. tie(0);cout. tie(0);
    ll t=1;
  // cin>>t;
    while(t--)
    {
      solve();
    }
    return 0;
}


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

//Made with <3 by MinumCoklatPanas
typedef long long ll;
const ll INF = 1e9;
const ll INFF = 1e18;
const ll MAXN = 510;
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
const double INFD = 1E9;
const double EPS = 1e-9;

ll mul(ll x,ll y)
{
    return ((x % MOD) * (y % MOD)) % MOD;
}

ll add(ll x,ll y)
{
    return ((x % MOD) + (y % MOD)) % MOD;
}

ll sub(ll x,ll y)
{
    ll ret = ((x % MOD) - (y % MOD)) % MOD;
    if (ret < 0)
        ret += MOD;
    return ret;
}

ll modpow(ll x,ll y)
{
    if (!y) return 1;
    ll z = modpow(x,y / 2);
    z = mul(z,z);
    if (y & 1) z = mul(z,x);
    return z;
}

ll inv(ll x)
{
    return modpow(x,MOD - 2);
}

ll bagi(ll x,ll y)
{
    return mul(x,inv(y));
}

ll pref[65][300010];
ll tab[300010];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n;
    cin >> n;
    for (ll i = 1 ; i <= n ; i++)
    {
        for (ll j = 0 ; j < 60 ; j++)
            pref[j][i] = pref[j][i - 1];
        ll ta;
        cin >> ta;
        for (ll j = 0 ; j < 60 ; j++) if (ta & (1ll << j))
        {
            pref[j][i]++;
        }
        tab[i] = ta;
    }
    ll ans = 0;
    for (ll i = 1 ; i <= n ; i++)
    {
        ll pengali = 1;
        ll rem = n - i;
        for (ll j = 0 ; j < 60 ; j++)
        {
            ll satu = pref[j][n] - pref[j][i];
            ll nol = rem - satu;
            // cout << j << ": " << satu << " " << nol << endl;
            if (tab[i] & (1ll << j))
                ans = add(ans,mul(nol,pengali));
            else
            {
                // cout << "yo\n";
                ans = add(ans,mul(satu,pengali));
            }
            pengali = mul(pengali,2ll);
        }
        // cout << ans << endl;
    }
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

constexpr const int mod = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  ll ans = 0, two_factor = 1;
  rep(i, 60) {
    ll even = 0;
    rep(j, n) if ((a[j] >> i) & 1) even++;
    ll add = (even * (n - even)) % mod * two_factor % mod;
    ans = (ans + add) % mod;
    two_factor = (two_factor * 2) % mod;
  }
  cout << ans << endl;
  return 0;
}


#include<stdio.h>

void devide(int x, int y, int z)
{
    if((x == 100) || (y == 100) || (z == 100)) {
        printf("A\n");
    } else if ((x + y) >= 90*2) {
        printf("A\n");
    } else if ((x + y + z) >= 80*3) {
        printf("A\n");
    } else if ((x + y + z) >= 70*3) {
        printf("B\n");
    } else if ((x + y + z) >= 50*3) {
        if ((x >= 80) || (y >= 80)){
            printf("B\n");
        } else {
            printf("C\n");
        }
    } else {
        printf("C\n");
    }
}

int main(void)
{
    int pm, pe, pj, num;

    scanf("%d", &num);

    while (num != 0) {
        for (int i = 0; i < num; i++) {
            scanf("%d %d %d", &pm, &pe, &pj);
            devide(pm, pe, pj);
        }

        scanf("%d", &num);
    }

    return 0;
}

#define _USE_MATH_DEFINES
#define INF 10000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>

using namespace std;

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

static const double eps = 1e-8;

int getAvr(int* p,int n){
	int sum = 0;
	for(int i=0;i<n;i++){
		sum += p[i];
	}

	return sum/3;
}

int main(){
	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;

		for(int i=0;i<n;i++){
			int p[3];
			//math,english,japanese
			scanf("%d %d %d",p,p+1,p+2);

			for(int i=0;i<3;i++){
				if(p[i]==100){
					printf("A\n");
					goto next;
				}
			}

			if(p[0] + p[1] >= 180 || getAvr(p,3) >= 80){
				printf("A\n");
			}

			else if(getAvr(p,3) >= 70
				|| (getAvr(p,3) >= 50 && (p[0] >= 80 || p[1] >= 80))){
				printf("B\n");
			}

			else{
				printf("C\n");
			}
next:;

		}

	}
}


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

int main(){
  int N;
  cin >> N;
  vector<long long> A(N), sumA(N+1, 0);
  long long mod_ = 1000000007;
  for (int i=0; i<N; i++){
    cin >> A.at(i);
    sumA.at(i+1) = sumA.at(i)+A.at(i);
    if (sumA.at(i+1) >= mod_) sumA.at(i+1) %= mod_;
  }

  long long ans = 0;
  long long sum_comp = 0;
  for (int i=0; i<N-1; i++){
    sum_comp = sumA.at(N) > sumA.at(i+1) ? sumA.at(N)-sumA.at(i+1) : sumA.at(N)+mod_-sumA.at(i+1);
    ans += A.at(i)*sum_comp;
    if (ans >= mod_) ans %= mod_;
  }

  cout << ans << endl;
}

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
	int n;
	string abc = "abcdefghijklmnopqrstuvwxyz";
	string abcc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	string aa = abcc + abc + abcc + abc;
	int sz = aa.size();
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		vector<int> k(n);
		for (int i = 0; i < n; i++) {
			cin >> k[i];
		}
		string s;
		cin >> s;
		int c = 0;
		for (int i = 0; i < s.size(); i++) {
			for (int j = sz - 1; j >= 0; j--) {
				if (aa[j] == s[i]) {
					cout << aa[j - k[c]];
					c++;
					c %= n;
					break;
				}
			}
		}
		cout << endl;
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
int main(){
	int a, b;
	scanf("%d %d", &a, &b);
	getchar();

	char str[a+b+1];
	scanf("%s", &str);
	int length = strlen(str);
	int flag = 0;

	for(int i=0; i<length; i++)
	{
		if(str[i] >= '0' && str[i] <= '9')
		{
			continue;		//kl ada yg bukan angka, di skip
		}
		else if ('0' > str[i] || str[i] > '9')
		{
			if (i==a) //kalo dia bukan angka tp ternyata di indeks ke a, di okin
			{
				flag = 1;
				continue;
			}
			else if(i !=a)
			{
				flag = 0;
				break;
			}
		}
	}

	flag ? printf("Yes\n") : printf("No\n");

	return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <math.h>
#include <utility>
#include <vector>
#include <map>
#include <cstdlib>
#define div1 1000000007
const long long INF = 1LL << 60;
using namespace std;
// a>b -> a==b;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
// a<b -> a==b;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

int main(void)
{
    string s;
    int i;

    cin >> s;

    for(i=0; i<s.size(); i++){
        if(i < s.size() - 8){
            cout << s[i];
        }
    }

    cout << endl;

    return 0;
}

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

int main(){
  string s;  cin>>s;
  reverse(s.begin(),s.end());
  int n=s.size();
  string t = s.substr(8,n-8);
  reverse(t.begin(),t.end());
  cout << t << endl;
}


#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
typedef long long ll;
const int INF = 1e9+1;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
using namespace std;

int div(int x) {
	int cnt = 0;
	for (int i = 1; i <= x;i++) {
		if (x % i == 0)cnt++;
	}
	return cnt;
}
bool prime(long long int x) {
	for (int i = 2; i <= sqrt(x) ;  i++) {
		if (x % i == 0)return 0;
	}
	return 1;

}

long long int divi(long long int n,long long int p) {
	int cn = 0;
	int q = p;
	while (n / q != 0) {
		cn += n / q;
		q = q * p;
	}
	return cn;
}

int main() {
	long long int n;
	int cnt = 0;
	long long int ans = 1;
	cin >> n;
	vector<int> p(n);
	vector<int> a(n);

	for (int i = 2; i <= n; i++) {
		if (prime(i) == 1) {
			p.at(cnt) = i;
			cnt++;
		}
	}
	//for (int i = 0; i < cnt; i++)cout << p.at(i) << endl;

	for (int i = 0; i < cnt; i++) {
		a.at(i) = divi(n, p.at(i));
	}

	for (int i = 0; i < cnt; i++) {
		ans =(ans%MOD) * (a.at(i)+1)%MOD;
	}

	cout << ans%MOD << endl;

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
 int H, W;
 int seen[10000][10000];
  char S[10000][10000];
vector<pair<ll, ll>> prime_factorize(ll n) { //素因数分解と各素数の個数
    vector<pair<ll, ll>> res;

    for (ll i = 2; i * i <= n; i++)
    {
        if (n % i != 0)
        {
            continue;
        }
        int num = 0;

        while (n % i == 0)
        {
            ++num;
            n /= i;
        }
        res.push_back(make_pair(i, num));

    }
     if (n != 1)
        {
            res.push_back(make_pair(n, 1));
        }
        return res;

}
long long GCD(long long a, long long b) {
    if (b == 0) return a;
    else return GCD(b, a % b);
}
ll lcm(ll x, ll y) {
    return x / GCD(x, y) * y;//先に割り算をして掛けられる数を小さくして掛け算を行う
}
const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

void dfs(int st, int fi) {
  //  cout << st << " " << fi <<  endl;
    if (st < 0 || fi < 0 || st >= H || fi >= W)
    {
        return ;
    }
    if (S[st][fi] == '#')
    {
        return ;
    }

    if (seen[st][fi] == 1)
    {
        return ;
    }

    if (S[st][fi] == '.')
    {
        seen[st][fi] = 1;
    }

    dfs(st + 1, fi);
    dfs(st, fi + 1);
    dfs(st, fi - 1);
    dfs(st - 1, fi);

}
int main() {
    int N;
    cin >> N;
    vector<int> p(N + 1);
    p[1] = 0;
    for(int i = 2; i <= N; i++) {
        int n = i;
        vector<pair<int, int>> b;
       for(int j = 2; j * j <= n; j++) {
           if(n % j != 0) continue;
           int ex = 0;

           while(n % j == 0) {
               ex++;
               n /= j;
           }

           b.push_back({j, ex});
       }
       if(n != 1) b.push_back({n, 1});

       for(auto v : b) {
           p[v.first] += v.second;
       }
    }

    /*for(int i = 1; i <= N; i++)
        cout << p[i] << endl;*/

    ll ans = 1;

    for(int i = 2; i <= N; i++) {
        ans *= (p[i] + 1);
        ans %= 1000000007;
    }

    cout << ans << endl;
 return 0;
}


#include<iostream>
using namespace std;
typedef long long LL;
const int N = 1e5;
int a[N];
int info[N];
bool check_feasibility(int n)
{
	for(int i = 2; i < n; i++)
		a[i] = a[i-2]^a[i-1]^info[i-1];
	if((a[n-2]^a[n-1]^a[0])!=info[n-1] || (a[n-1]^a[0]^a[1])!=info[0])
		return false;
	else
		return true;
}
bool solve(int n)
{
	for(int i = 0; i <= 1; i++)
		for(int j = 0; j <= 1; j++)
		{
			a[0] = i;
			a[1] = j;
			if(check_feasibility(n))
				return true;
		}
	return false;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n; cin >> n;
	for(int i = 0; i < n; i++)
	{
		char c; cin >> c;
		info[i] = c=='x';
	}
	if(solve(n))
	{
		for(int i = 0; i < n; i++)
			cout<<(a[i] ? "W" : "S");
		cout<<endl;
	}
	else
		cout<<-1<<endl;

	cout<<flush;
	return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#define vv(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define vvi vector<vector<int> >
#define vvl vector<vector<ll> >
#define vll vector<ll>
#define rep(c, a, b) for(ll c=a;c<b;c++)
#define re(c, b) for(ll c=0;c<b;c++)
typedef long long int ll;
typedef long double ld;
using namespace std;

int main(int argc, char const *argv[]) {
  ll n;std::cin >> n;
  string t, s;std::cin >> s;
  //SS

  char end='&';
  char pre = 'S';
  t = "S";
  re(i, s.size()){
    int k = (s[i]=='o'?1:-1);
    if(t[i]=='W') k*=-1;
    int b = ((i==0?pre:t[i-1])=='S'?1:-1);
    if(k==-1) b*=-1;
    if(b==1){
      if(i==s.size()-1){
        end = 'S';
        break;
      }
      t+='S';
    }else {
      if(i==s.size()-1){
        end = 'W';
        break;
      }
      t+='W';
    }
  }
  if(pre==t[n-1]&&t[0]==end){
    std::cout << t << '\n';
    return 0;
  }
  //SW
  pre = 'S';
  t = "W";
  re(i, s.size()){
    int k = (s[i]=='o'?1:-1);
    if(t[i]=='W') k*=-1;
    int b = ((i==0?pre:t[i-1])=='S'?1:-1);
    if(k==-1) b*=-1;
    if(b==1){
      if(i==s.size()-1){
        end = 'S';
        break;
      }
      t+='S';
    }else {
      if(i==s.size()-1){
        end = 'W';
        break;
      }
      t+='W';
    }
  }
  if(pre==t[n-1]&&t[0]==end){
    std::cout << t << '\n';
    return 0;
  }
  //WW
  pre = 'W';
  t = "W";
  re(i, s.size()){
    int k = (s[i]=='o'?1:-1);
    if(t[i]=='W') k*=-1;
    int b = ((i==0?pre:t[i-1])=='S'?1:-1);
    if(k==-1) b*=-1;
    if(b==1){
      if(i==s.size()-1){
        end = 'S';
        break;
      }
      t+='S';
    }else {
      if(i==s.size()-1){
        end = 'W';
        break;
      }
      t+='W';
    }
  }
  if(pre==t[n-1]&&t[0]==end){
    std::cout << t << '\n';
    return 0;
  }
  //WS
  pre = 'W';
  t = "S";
  re(i, s.size()){
    int k = (s[i]=='o'?1:-1);
    if(t[i]=='W') k*=-1;
    int b = ((i==0?pre:t[i-1])=='S'?1:-1);
    if(k==-1) b*=-1;
    if(b==1){
      if(i==s.size()-1){
        end = 'S';
        break;
      }
      t+='S';
    }else {
      if(i==s.size()-1){
        end = 'W';
        break;
      }
      t+='W';
    }
  }
  if(pre==t[n-1]&&t[0]==end){
    std::cout << t << '\n';
    return 0;
  }
  std::cout << -1 << '\n';
  return 0;
}


#include<iostream>
using namespace std;
#include <cmath>

int main(){
  int n;
  cin >> n;
  cout << (int)pow((int)sqrt(n),2) << endl;
}

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int N;
    cin >> N;
    int a = N % 10;
    int b = ((N-a) % 100) / 10;
    int c = ((N-b*10) % 1000) / 100;
    if (a == 7 | b == 7 | c == 7) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}


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

int main(void)
{
    int N;
    cin >> N;
    vector<vector<int>> part(3, vector<int>(N));

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < N; j++)
        {
            cin >> part[i][j];
        }

        sort(part[i].begin(),part[i].end());
    }

    ll cases = 0;
    for (int mid = 0; mid < N; mid++)
    {
        ll as = lower_bound(part[0].begin(), part[0].end(), part[1][mid]) - part[0].begin();
        ll cs = N - (upper_bound(part[2].begin(), part[2].end(), part[1][mid]) - part[2].begin());

        cases += (as * cs);
    }

    cout << cases << endl;
    return 0;
}

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

class Dice
{
private:
  int surf[6];
public:
  void set_surf(int* input);
  void roll(char orient);
  void disp_top();
};

void Dice::set_surf(int* input)
{
  memcpy(surf, input, 6*sizeof(int));
}

void Dice::roll(char orient){
  int temp[6];

  switch(orient){
  case 'S':
    temp[0]=surf[4];
    temp[1]=surf[0];
    temp[2]=surf[2];
    temp[3]=surf[3];
    temp[4]=surf[5];
    temp[5]=surf[1];
    break;
  case 'N':
    temp[0]=surf[1];
    temp[1]=surf[5];
    temp[2]=surf[2];
    temp[3]=surf[3];
    temp[4]=surf[0];
    temp[5]=surf[4];
    break;
  case 'W':
    temp[0]=surf[2];
    temp[1]=surf[1];
    temp[2]=surf[5];
    temp[3]=surf[0];
    temp[4]=surf[4];
    temp[5]=surf[3];
    break;
  case 'E':
    temp[0]=surf[3];
    temp[1]=surf[1];
    temp[2]=surf[0];
    temp[3]=surf[5];
    temp[4]=surf[4];
    temp[5]=surf[2];
    break;
  }

  memcpy(surf, temp, 6*sizeof(int));

}

void Dice::disp_top(){
  cout << surf[0] << endl;
}

int main()
{
  int input[6];
  Dice dice;
  string order;

  for(int i=0; i<6; i++){
    cin >> input[i];
  }
  dice.set_surf(input);

  cin >> order;

  for(int i=0; i<order.size(); i++){
    dice.roll(order[i]);
  }

  dice.disp_top();

  return(0);
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...) for (int i = (b-1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;

int main (void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    string a, b, c; cin >> a >> b >> c;
    int ans = 0;
    rep (i, n) {
        set<char> st;
        st.insert(a[i]); st.insert(b[i]); st.insert(c[i]);
        ans += st.size() - 1;
    }
    cout << ans << "\n";
    return 0;
}

#include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
template<class T> T div_floor(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>=0 ? a/b : (a+1)/b-1;
}
template<class T> T div_ceil(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>0 ? (a-1)/b+1 : a/b;
}

constexpr lint mod = 1e9+7;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

int main(){
    int n;
    string a, b, c;
    cin >> n >> a >> b >> c;
    int ans = 0;
    rep(i, n){
        if(a[i] == b[i] && b[i] == c[i]) continue;
        else if(a[i] == b[i] || b[i] == c[i] || c[i] == a[i]) ans += 1;
        else ans += 2;
    }
    printf("%d\n", ans);
}


#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
using namespace std;

set<int> guest;
int n, m;
vector<int> people[501];
int main()
{
    while((cin >> n >> m) && n && m)
    {
        int a, b;
        int t;
        for(int i=0;i<m;i++)
        {
            cin >> a >> b;
            people[a].push_back(b);
            people[b].push_back(a);
        }
        for(int i=0;i<people[1].size();i++)
        {
            t = people[1][i];
            guest.insert(t);
            for(int j=0;j<people[t].size();j++) guest.insert(people[t][j]);
        }
        guest.insert(1);
        cout << guest.size()-1 << endl;
        guest.clear();
        for(int i=0;i<n;i++) people[i].clear();
    }
}

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)

int main(void){
  int n;
  while(cin>>n){
    if(n==0) break;
    vector<int> friends[501];
    int m;
    cin>>m;
    REP(i,m){
      int a,b;
      cin>>a>>b;
      friends[a].push_back(b);
      friends[b].push_back(a);
    }
    set<int> ans;
    REP(i,friends[1].size()){
      int s = friends[1][i];
      ans.insert(s);
      REP(i,friends[s].size()){
        ans.insert(friends[s][i]);
      }
    }

    if(ans.size()!=0)cout<<ans.size()-1<<endl;
    else cout<<0<<endl;
  }
  return 0;
}