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

using namespace std;

int main() {
	int n, k;
	string s;
	set<char> chars;
	cin >> n >> k;
	cin >> s;
	for (int i = 0; i < k; i++) {
		char c;
		cin >> c;
		chars.insert(c);
	}

	int elems = 0, substrings = 0;
	for (int i = 0; i < s.size(); i++) {
		if (chars.find(s[i]) == chars.end()) {
			substrings += (int)(elems * (elems + 1)) / 2;
			elems = 0;
		}
		else
			elems++;
	}
	substrings += (int)(elems * (elems + 1)) / 2;
	cout << substrings << endl;
}


#include <iostream>
#include <vector>
#include <set>

using namespace std;

int main() {
	int n, k;
	string s;
	set<char> chars;
	cin >> n >> k;
	cin >> s;
	for (int i = 0; i < k; i++) {
		char c;
		cin >> c;
		chars.insert(c);
	}

	int elems = 0, substrings = 0;
	for (int i = 0; i < s.size(); i++) {
		if (chars.find(s[i]) == chars.end()) {
			substrings += (int)(elems * (elems + 1)) / 2;
			elems = 0;
		}
		else
			elems++;
	}
	substrings += (int)(elems * (elems + 1)) / 2;
	cout << substrings << endl;
}


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

using namespace std;

int main() {
	int n, k;
	string s;
	set<char> chars;
	cin >> n >> k;
	cin >> s;
	for (int i = 0; i < k; i++) {
		char c;
		cin >> c;
		chars.insert(c);
	}

	long long elems = 0, substrings = 0;
	for (int i = 0; i < s.size(); i++) {
		if (chars.find(s[i]) == chars.end()) {
			if (i == 0) continue;
			substrings += (long long) (elems * (elems + 1)) / 2;
			elems = 0;
		}
		else
			elems++;
	}
	substrings += (long long)(elems * (elems + 1)) / 2;
	cout << substrings << endl;
}


#include<iostream>
using namespace std;
int q,n,m,ans,wu;
char a[200000+10];
char ex[30];
int main()
{
		cin >> n >> m;
		for(int i=1;i<=n;i++)
		cin >> a[i];
		for(int i=1;i<=m;i++)
		cin >> ex[i];
		for(int i=1;i<=n;i++)
		{
			int flag=0;
			for(int j=1;j<=m;j++)
			{
				if(a[i]!=ex[j])	flag++;
				else break;
			}
			if(flag==m)	a[i]='0';
		}
		int flag=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]!='0')
			flag++;
			else
			{
				ans+=flag*(flag+1)/2;
				flag=0;
			}
		}
		if(a[n]!='0')
		ans+= flag*(flag+1)/2;
		cout <<ans;
	return 0;
}


#include<iostream>
#include<iomanip>
#include<ctime>
#include<stdlib.h>
using namespace std;
long long q,n,m,ans,wu;
char a[200000+10];
char ex[30];
int a1[200000+10];
int main()
{
	/*
	srand(int(time(0)));
	n=rand()%200000+1;
	m=rand()%26+1;
	cout << n  << " " << m<< endl;
	*/
 		cin >> n >> m;
		for(int i=1;i<=n;i++)
	//	a[i]='a'+rand()%26;
		cin >> a[i];
		for(int i=1;i<=m;i++)
	//	ex[i]='a'+rand()%26;
		cin >> ex[i];
		/*
		for(int i=1;i<=n;i++)
		cout << a[i];
		cout <<endl;
		for(int i=1;i<=m;i++)
		cout << ex[i];
		cout << endl;
*/		for(int i=1;i<=n;i++)
		{
			int flag=0;
			for(int j=1;j<=m;j++)
			{
				if(a[i]!=ex[j])	flag++;
				else break;
			}
			if(flag==m)	a1[i]=1;
		}
		long long flag=0;
		for(int i=1;i<=n;i++)
		{
			if(a1[i]==0)
			flag++;
			else
			{
				ans+=flag*(flag+1)/2;
				flag=0;
			}
		}
		ans+= flag*(flag+1)/2;
		cout <<ans;
	return 0;
}


#include<iostream>
#include<iomanip>
#include<ctime>
#include<stdlib.h>
using namespace std;
long long q,n,m,ans,wu;
char a[200000+10];
char ex[30];
int a1[200000+10];
int main()
{
	/*
	srand(int(time(0)));
	n=rand()%200000+1;
	m=rand()%26+1;
	cout << n  << " " << m<< endl;
	*/
 		cin >> n >> m;
		for(int i=1;i<=n;i++)
	//	a[i]='a'+rand()%26;
		cin >> a[i];
		for(int i=1;i<=m;i++)
	//	ex[i]='a'+rand()%26;
		cin >> ex[i];
		/*
		for(int i=1;i<=n;i++)
		cout << a[i];
		cout <<endl;
		for(int i=1;i<=m;i++)
		cout << ex[i];
		cout << endl;
*/		for(int i=1;i<=n;i++)
		{
			int flag=0;
			for(int j=1;j<=m;j++)
			{
				if(a[i]!=ex[j])	flag++;
				else break;
			}
			if(flag==m)	a1[i]=1;
		}
		int flag=0;
		for(int i=1;i<=n;i++)
		{
			if(a1[i]==0)
			flag++;
			else
			{
				ans+=flag*(flag+1)/2;
				flag=0;
			}
		}
		ans+= flag*(flag+1)/2;
		cout <<ans;
	return 0;
}


#include<iostream>
using namespace std;
long long q,n,m,ans,wu;
char a[200000+10];
char ex[30];
int main()
{
		cin >> n >> m;
		for(int i=1;i<=n;i++)
		cin >> a[i];
		for(int i=1;i<=m;i++)
		cin >> ex[i];
		for(int i=1;i<=n;i++)
		{
			int flag=0;
			for(int j=1;j<=m;j++)
			{
				if(a[i]!=ex[j])	flag++;
				else break;
			}
			if(flag==m)	a[i]='0';
		}
		int flag=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]!='0')
			flag++;
			else
			{
				ans+=flag*(flag+1)/2;
				flag=0;
			}
		}
		if(a[n]!='0')
		ans+= flag*(flag+1)/2;
		cout <<ans;
	return 0;
}


#include<iostream>
#include<iomanip>
#include<ctime>
#include<stdlib.h>
using namespace std;
long long q,n,m,ans,wu;
char a[200000+10];
char ex[30];
int main()
{
	/*
	srand(int(time(0)));
	n=rand()%200000+1;
	m=rand()%26+1;
	cout << n  << " " << m<< endl;
	*/
 		cin >> n >> m;
		for(int i=1;i<=n;i++)
	//	a[i]='a'+rand()%26;
		cin >> a[i];
		for(int i=1;i<=m;i++)
	//	ex[i]='a'+rand()%26;
		cin >> ex[i];
		/*
		for(int i=1;i<=n;i++)
		cout << a[i];
		cout <<endl;
		for(int i=1;i<=m;i++)
		cout << ex[i];
		cout << endl;
*/		for(int i=1;i<=n;i++)
		{
			int flag=0;
			for(int j=1;j<=m;j++)
			{
				if(a[i]!=ex[j])	flag++;
				else break;
			}
			if(flag==m)	a[i]='A';
		}
		int flag=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]!='A')
			flag++;
			else
			{
				ans+=flag*(flag+1)/2;
				flag=0;
			}
		}
		ans+= flag*(flag+1)/2;
		cout <<ans;
	return 0;
}


#include<iostream>
using namespace std;
int q,n,m,ans,wu;
char a[200000+10];
char ex[30];
int main()
{
		cin >> n >> m;
		for(int i=1;i<=n;i++)
		cin >> a[i];
		for(int i=1;i<=m;i++)
		cin >> ex[i];
		for(int i=1;i<=n;i++)
		{
			int flag=0;
			for(int j=1;j<=m;j++)
			{
				if(a[i]!=ex[j])	flag++;
				else break;
			}
			if(flag==m)	a[i]='0';
		}
		int flag=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]!='0')
			flag++;
			else
			{
				ans+=flag*(flag+1)/2;
				flag=0;
			}
		}
		ans+= flag*(flag+1)/2;
		cout <<ans;
	return 0;
}


#include<iostream>
using namespace std;
int q,n,m,ans,wu;
char a[200000+10];
char ex[30];
string s;
int main()
{
		cin >> n >> m;
		for(int i=1;i<=n;i++)
		cin >> a[i];
		for(int i=1;i<=m;i++)
		cin >> ex[i];
		for(int i=1;i<=n;i++)
		{
			int flag=0;
			for(int j=1;j<=m;j++)
			{
				if(a[i]!=ex[j])	flag++;
				else break;
			}
			if(flag==m)	a[i]='0';
		}
		for(int i=1;i<=n;i++)
		{
			if(a[i]=='0') 	continue;
			for(int j=i;j<=n;j++)
			{
				if(a[j]=='0')	break;
				else ans++;
			}
		}
		cout <<ans;
	return 0;
}


#include<string>
#include<iostream>
using namespace std;
#define ll long long
int main()
{
	ll int n,k;
	cin>>n>>k;
	string s,s1,s3;
	cin>>s;
	while(k--)
	{
		char c;
		cin>>c;
		s3.push_back(c);
	}
	ll int c=0,tt=0;
	//cout<<s3<<endl;
	for(int i=0;i<n;i++)
	{
		if(find(s3.begin(),s3.end(),s[i])==s3.end())
		c=0;
		else
		{
			c++;
			tt+=c;
		}

	}
	cout<<tt<<endl;

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string>
using namespace std;

void outVector(vector<int>& a) {
	for (int i = 0; i < a.size(); i++) {
		cout << a[i] << " ";
	}
	cout << "\n";
}

int main() {
	int n, k;
	cin >> n >> k;
	string s;
	cin >> s;
	vector<char> ch(k);
	for (int i = 0; i < k; i++) {
		cin >> ch[i];
	}
	long long ans = 0;
	long long c = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < k; j++) {
			if (s[i] == ch[j]) {
				c++;
				break;
			}
			if (j == k - 1) {
				ans += c * (c + 1) / 2;
				c = 0;
			}
		}
	}
	ans += c * (c + 1) / 2;
	cout << ans << "\n";
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string>
using namespace std;

void outVector(vector<int>& a) {
	for (int i = 0; i < a.size(); i++) {
		cout << a[i] << " ";
	}
	cout << "\n";
}

int main() {
	int n, k;
	cin >> n >> k;
	string s;
	cin >> s;
	vector<char> ch(k);
	for (int i = 0; i < k; i++) {
		cin >> ch[i];
	}
	long long ans = 0;
	int c = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < k; j++) {
			if (s[i] == ch[j]) {
				c++;
				break;
			}
			if (j == k - 1) {
				ans += c * (c + 1) / 2;
				c = 0;
			}
		}
	}
	ans += c * (c + 1) / 2;
	cout << ans << "\n";
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_set>
#include <string>

using namespace std;

int main() {
	unordered_set<char> s;
	string str;
	char a;
	int n, k, count, res = 0;
	cin >> n >> k >> str;
	for (int i = 0; i < k; ++i) {
		cin >> a;
		s.insert(a);
	}
	for (int i = 0; i < n; ++i) {
		count = 0;
		while (s.count(str[i])) {
			++count;
			++i;
		}
		res += count * (count + 1) / 2;
	}
	cout << res;
	/*system("pause");*/
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_set>
#include <string>

using namespace std;

int main() {
	unordered_set<char> s;
	string str;
	char a;
	int n, k;
	long long count, res = 0;
	cin >> n >> k >> str;
	for (int i = 0; i < k; ++i) {
		cin >> a;
		s.insert(a);
	}
	for (int i = 0; i < n; ++i) {
		count = 0;
		while (s.count(str[i])) {
			++count;
			++i;
		}
		res += count * (count + 1) / 2;
	}
	cout << res;
	/*system("pause");*/
	return 0;
}


#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <unordered_set>
#include <cmath>
#include <algorithm>
#include <unordered_map>
#include <list>
#include <set>
#include<map>
#include <queue>
#include <stdio.h>
#include "cstring"

using namespace std;

int main () {

    int n , k ;
    unsigned long long a = 0 ;
    cin >> n >> k ;
    map<char , int> m ;
    string z ;
    cin >> z ;
    while(k--) {
        char c ;
        cin >> c ;
        m[c]++ ;
    }

    int c = 0 ;
    for ( int i =0 ; i < n ; i++ ) {
        if( m[z[i]] > 0 )
            c++ ;
        else {
            a += (c*(c+1)) /2 ;
            c = 0 ;
        }

    }

    a += (c*(c+1))/2 ;

    cout << a ;

    return 0 ;
}


#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <unordered_set>
#include <cmath>
#include <algorithm>
#include <unordered_map>
#include <list>
#include <set>
#include<map>
#include <queue>
#include <stdio.h>
#include "cstring"

using namespace std;

int main () {

    long long n , k ;
    long long a = 0 ;
    cin >> n >> k ;
    int m [26] = {0} ;
    string z ;
    cin >> z ;
    while(k--) {
        char c ;
        cin >> c ;
        m[c-'a']++ ;
    }

    long long c = 0 ;
    for ( int i =0 ; i < n ; i++ ) {
        if( m[ z[i] - 'a'  ] > 0 )
            c++ ;
        else {
            a += (c*(c+1)) /2 ;
            c = 0 ;
        }

    }

    a += (c*(c+1))/2 ;

    cout << a ;

    return 0 ;
}


#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <unordered_set>
#include <cmath>
#include <algorithm>
#include <unordered_map>
#include <list>
#include <set>
#include<map>
#include <queue>
#include <stdio.h>
#include "cstring"

using namespace std;

int main () {

    int n , k ;
    long long a = 0 ;
    cin >> n >> k ;
    set <char> s ;
    string z ;
    cin >> z ;
    while(k--) {
        char c ;
        cin >> c ;
        s.insert(c) ;
    }

    int c = 0 ;
    for ( int i =0 ; i < n ; i++ ) {
        if( s.find( z[i] ) != s.end() )
            c++ ;
        else {
            a += c*(c+1)/2 ;
            c = 0 ;
        }

    }

    a += c*(c+1)/2 ;

    cout << a ;

    return 0 ;
}


#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <unordered_set>
#include <cmath>
#include <algorithm>
#include <unordered_map>
#include <list>
#include <set>
#include<map>
#include <queue>
#include <stdio.h>
#include "cstring"

using namespace std;

int main () {

    int n , k ;
    long long a = 0 ;
    cin >> n >> k ;
    map<char , int> m ;
    vector<int>v ;
    string z ;
    cin >> z ;
    while(k--) {
        char c ;
        cin >> c ;
        m[c]++ ;
    }

    int c = 0 ;
    for ( int i = 0 ; i < n ; i++ ) {
        if( m[z[i]] )
            c++ ;
        else {
           // a += c*(c+1)/2 ;
            //c = 0 ;
            if (c)
                v.push_back(c) ;
            c = 0 ;
        }

        if ( i == n-1 && c )
            v.push_back(c) ;

    }

    for (int i =0 ; i< v.size(); i++) {
        c = v[i] ;
        a += c*(c+1)/2 ; ;

    }

    cout << a ;

    return 0 ;
}


#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <unordered_set>
#include <cmath>
#include <algorithm>
#include <unordered_map>
#include <list>
#include <set>
#include<map>
#include <queue>
#include <stdio.h>
#include "cstring"

using namespace std;

int main () {

    int n , k ;
    unsigned long long a = 0 ;
    cin >> n >> k ;
    int m [26] = {0} ;
    string z ;
    cin >> z ;
    while(k--) {
        char c ;
        cin >> c ;
        m[c-'a']++ ;
    }

    int c = 0 ;
    for ( int i =0 ; i < n ; i++ ) {
        if( m[ z[i] - 'a'  ] > 0 )
            c++ ;
        else {
            a += (c*(c+1)) /2 ;
            c = 0 ;
        }

    }

    a += (c*(c+1))/2 ;

    cout << a ;

    return 0 ;
}


#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <unordered_set>
#include <cmath>
#include <algorithm>
#include <unordered_map>
#include <list>
#include <set>
#include<map>
#include <queue>
#include <stdio.h>
#include "cstring"

using namespace std;

int main () {

    int n , k ;
    long long a = 0 ;
    cin >> n >> k ;
    map<char , int> m ;
    string z ;
    cin >> z ;
    while(k--) {
        char c ;
        cin >> c ;
        m[c]++ ;
    }

    int c = 0 ;
    for ( int i =0 ; i < n ; i++ ) {
        if( m[z[i]])
            c++ ;
        else {
            a += c*(c+1)/2 ;
            c = 0 ;
        }

    }

    a += c*(c+1)/2 ;

    cout << a ;

    return 0 ;
}


#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <unordered_set>
#include <cmath>
#include <algorithm>
#include <unordered_map>
#include <list>
#include <set>
#include<map>
#include <queue>
#include <stdio.h>
#include "cstring"

using namespace std;

int main () {

    int n , k ;
    unsigned long long a = 0 ;
    cin >> n >> k ;
    map<char , int> m ;
    string z ;
    cin >> z ;
    while(k--) {
        char c ;
        cin >> c ;
        m[c]++ ;
    }

    int c = 0 ;
    for ( int i =0 ; i < n ; i++ ) {
        if( m[z[i]])
            c++ ;
        else {
            a += (c*(c+1)) /2 ;
            c = 0 ;
        }

    }

    a += (c*(c+1))/2 ;

    cout << a ;

    return 0 ;
}


#include<iostream>
using namespace std;
int main()
{
	int a[28]={0},s[300000]={0},max=0,n,k;
	char c[300000];
	cin>>n>>k>>c;
	for(int i=0;i<k;i++)
	{
		char dd;
		cin>>dd;
		int q;
		q=dd-'a';
		a[q]=1;
	}
	for(int i=0;i<n;i++)
	{
		if(a[c[i]-'a'])
		{
			s[i]=1;
		}
	}
	for(int i=0;i<n;i++)
	{
		int p=0;
		while(s[i])
		{
			p++;
			i++;
		}
		max=max+p*(p+1)/2;
	}
	cout<<max;
}


#include<iostream>
using namespace std;
int main()
{
	int b[28]={0},max=0;
	char a[200000];
	int n,k;
	cin>>n>>k>>a;
	for(int i=0;i<k;i++)
	{
		char d;
		cin>>d;
		b[d-'a']=1;
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<=n-i;j++)
		{
			int k;
			for(k=0;k<i;k++)
			{
				int pp;
				pp=a[k+j]-'a';
				if(b[pp]==0)
				break;
			}
			if(k==i)
			max++;
		}
	}
	cout<<max;
}


#include<iostream>
using namespace std;
int main()
{
	long long a[28]={0},s[300000]={0},max=0,n,k;
	char c[300000];
	cin>>n>>k>>c;
	for(int i=0;i<k;i++)
	{
		char dd;
		cin>>dd;
		long long q;
		q=dd-'a';
		a[q]=1;
	}
	for(int i=0;i<n;i++)
	{
		if(a[c[i]-'a'])
		{
			s[i]=1;
		}
	}
	for(int i=0;i<n;i++)
	{
		long long p=0;
		while(s[i])
		{
			p++;
			i++;
		}
		max=max+p*(p+1)/2;
	}
	cout<<max;
}


#include<iostream>
using namespace std;
int main()
{
	int a[28]={0},s[300000]={0},n,k;
	long long max=0;
	char c[300000];
	cin>>n>>k>>c;
	for(int i=0;i<k;i++)
	{
		char dd;
		cin>>dd;
		int q;
		q=dd-'a';
		a[q]=1;
	}
	for(int i=0;i<n;i++)
	{
		if(a[c[i]-'a'])
		{
			s[i]=1;
		}
	}
	for(int i=0;i<n;i++)
	{
		int p=0;
		while(s[i])
		{
			p++;
			i++;
		}
		max=max+p*(p+1)/2;
	}
	cout<<max;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#define ll long long
const int mod = 1e9 + 7;
using namespace std;

int main()
{
	int n, k, j = 0;
	ll ans = 0;
	string s1, s2;
	cin >> n >> k >> s1;
	getline(cin, s2);
	getline(cin, s2);
	for (int i = 0; i < n; i++)
	{
		if (s2.find(s1[i]) != -1)
			j++;
		else
			ans += (j * (j + 1) / 2), j = 0;
	}
	ans += (j * (j + 1) / 2), j = 0;
	cout << ans;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#define ll long long
const int mod = 1e9 + 7;
using namespace std;

int main()
{
	ll n, k, j = 0, ans = 0;
	string s1, s2;
	cin >> n >> k >> s1;
	getline(cin, s2);
	getline(cin, s2);
	for (int i = 0; i < n; i++)
	{
		if (s2.find(s1[i]) != -1)
			j++;
		else
			ans += (j * (j + 1) / 2), j = 0;
	}
	ans += (j * (j + 1) / 2), j = 0;
	cout << ans;
	return 0;
}


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

#define ll long long

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

	string s;
	cin >> s;

	set<char> allowed;
	for (int i = 0; i < k; i++) {
		char c;
		cin >> c;
		allowed.insert(c);
	}

	// DO SHIT
	ll sum = 0, l = 0;
	for (int i = 0; i < n; i++) {
		if (allowed.find(s[i]) != allowed.end()) {
			if (l == -1) {
				l = i;
			}
			sum+=i-l+1;
		}
		else {
			l = -1;
		}
	}

	cout << sum << endl;

	return 0;
}


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

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

	string s;
	cin >> s;

	set<char> allowed;
	for (int i = 0; i < k; i++) {
		char c;
		cin >> c;
		allowed.insert(c);
	}

	// DO SHIT
	int sum = 0, l = 0;
	for (int i = 0; i < n; i++) {
		if (allowed.find(s[i]) != allowed.end()) {
			if (l == -1) {
				l = i;
			}
			sum+=i-l+1;
		}
		else {
			l = -1;
		}
	}

	cout << sum << endl;

	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <set>
using namespace std;
const int MAX = 2e5 + 5;
typedef long long ll;

ll f(ll k) {
	return k * (k + 1) / 2;
}

int main() {
	int n, k; cin >> n >> k;
	string s;
	set<char> c;
	cin >> s;
	for (int i = 0; i < k; i++) {
		char u;
		cin >> u;
		c.insert(u);
	}

	int l = -1, r = -1;
	for (int i = 0; i < n; i++) {
		if (c.count(s[i]) >= 1) {
			l = r = i;
			break;
		}
	}

	if (l == -1) {
		cout << 0 << '\n';
	}
	else {
		ll ans = 0;
		while (true) {
			if (r+1 == n || c.count(s[r + 1]) == 0) {
				ans += f(r - l + 1);
				if (r + 1 == n) break;
				else {
					for (int i = r + 1; i < n; i++) {
						if (c.count(s[i]) >= 1) {
							l = r = i;
							break;
						}
					}
				}
			}
			else {
				r++;
				continue;
			}
		}
		cout << ans << '\n';
	}

}


#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
using namespace std;
const int MAX = 2e5 + 5;
typedef long long ll;

ll f(ll k) {
	return k * (k + 1) / 2;
}

int main() {
	int n, k; cin >> n >> k;
	string s;
	set<char> c;
	vector<int> vIdx;
	cin >> s;
	for (int i = 0; i < k; i++) {
		char u;
		cin >> u;
		c.insert(u);
	}

	for (int i = 0; i < n; i++) {
		if (c.count(s[i]) >= 1) {
			vIdx.push_back(i);
		}
	}

	if (vIdx.empty()) {
		cout << 0 << '\n';
	}
	else {
		ll ans = 0;
		int r, l;
		l = r = vIdx[0];
		while (true) {
			if (r+1 == n || c.count(s[r + 1]) == 0) {
				ans += f(r - l + 1);
				if (upper_bound(vIdx.begin(), vIdx.end(), r)==vIdx.end()) break;
				else {
					l=r = *upper_bound(vIdx.begin(), vIdx.end(), r);
				}
			}
			else {
				r++;
				continue;
			}
		}
		cout << ans << '\n';
	}

}


#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
using namespace std;
const int MAX = 2e5 + 5;
typedef long long ll;

ll f(ll k) {
	return k * (k + 1) / 2;
}

int main() {
	int n, k; cin >> n >> k;
	string s;
	set<char> c;
	vector<int> vIdx;
	cin >> s;
	for (int i = 0; i < k; i++) {
		char u;
		cin >> u;
		c.insert(u);
	}

	for (int i = 0; i < n; i++) {
		if (c.count(s[i]) >= 1) {
			vIdx.push_back(i);
		}
	}

	if (vIdx.empty()) {
		cout << 0 << '\n';
	}
	else {
		ll ans = 0;
		int r, l;
		l = r = vIdx[0];
		while (true) {
			if (r+1 == n || c.count(s[r + 1]) == 0) {
				ans += f(r - l + 1);
				if (r + 1 == n) break;
				else {
					l=r = *upper_bound(vIdx.begin(), vIdx.end(), r);
				}
			}
			else {
				r++;
				continue;
			}
		}
		cout << ans << '\n';
	}

}


#include<iostream>
#include<string>
#include<set>
#include<algorithm>
#include<conio.h>
#define loopi(b,e) for(auto i=b;i<e;i++)
using namespace std;
int prcr(int n)
{
	int i, s = 0;
	loopi(0, n)
		s += i + 1;
	return s;
}
int main()
{
	string a;
	set <char> kb;
	int n, k;
	cin >> n >> k;
	cin >> a;
	while (k--)
	{
		char buf = _getch();
		_getch();
		kb.insert(buf);
	}
	int res = 0, ps=0;
	loopi(0, n)
	{
		if (kb.count(a[i]))
		{
			ps++;
			//cout << a[i] << ' ' << ps << endl;
		}
		else
		{
			res += prcr(ps);
			ps = 0;
		}
	}
	res += prcr(ps);
	cout << res;
	return 0;
}


#include<iostream>
#include<string>
#include<set>
#include<algorithm>
#define loopi(b,e) for(auto i=b;i<e;i++)
using namespace std;
int prcr(int n)
{
	int i, s = 0;
	loopi(0, n)
		s += i + 1;
	return s;
}
int main()
{
	string a;
	set <char> kb;
	int n, k;
	cin >> n >> k;
	cin >> a;
	while (k--)
	{
		char buf;
		cin >> buf;
		kb.insert(buf);
	}
	int res = 0, ps = 0;
	loopi(0, n)
	{
		if (kb.count(a[i]))
		{
			ps++;
			//cout << a[i] << ' ' << ps << endl;
		}
		else
		{
			res += prcr(ps);
			ps = 0;
		}
	}
	res += prcr(ps);
	cout << res;
	return 0;
}


#include<iostream>
#include<string>
#include<set>
#include<algorithm>
#include<conio.h>
#define loopi(b,e) for(auto i=b;i<e;i++)
using namespace std;
int prcr(int n)
{
	int i, s = 0;
	loopi(0, n)
		s += i + 1;
	return s;
}
int main()
{
	string a;
	set <char> kb;
	int n, k;
	cin >> n >> k;
	cin >> a;
	while (k--)
	{
		char buf = _getch();
		if (k) _getch();
		kb.insert(buf);
	}
	int res = 0, ps=0;
	loopi(0, n)
	{
		if (kb.count(a[i]))
		{
			ps++;
			//cout << a[i] << ' ' << ps << endl;
		}
		else
		{
			res += prcr(ps);
			ps = 0;
		}
	}
	res += prcr(ps);
	cout << res;
	return 0;
}


#include<iostream>
#include<string>
#include<set>
#include<algorithm>
#define loopi(b,e) for(auto i=b;i<e;i++)
using namespace std;
long long prcr(int n)
{
	long long i, s = 0;
	loopi(0, n)
		s += (i + 1);
	return s;
}
int main()
{
	string a;
	set <char> kb;
	int n, k;
	cin >> n >> k;
	cin >> a;
	while (k--)
	{
		char buf;
		cin >> buf;
		kb.insert(buf);
	}
	long long res = 0, ps = 0;
	loopi(0, n)
	{
		if (kb.count(a[i]))
		{
			ps++;
			//cout << a[i] << ' ' << ps << endl;
		}
		else
		{
			res += prcr(ps);
			ps = 0;
		}
	}
	res += prcr(ps);
	cout << res;
	return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <deque>
#include <algorithm>
#include <ctype.h>
#include <string>
#include <map>
#include <numeric>
#include <set>
#include <array>
#include <functional>

using namespace std;

int main() {
	int n, k;
	cin >> n >> k;
	string s;
	cin >> s;
	vector<char> v;
	char c;
	for (int i = 0; i < k; i++) {
		cin >> c;
		v.push_back(c);
	}
	// (n x (n + 1)) / 2
	long long sum = 0;
	long long l = 0;
	for (char c : s) {
		if (find(v.begin(), v.end(), c) == v.end()) {
			sum += (l * (l + 1)) / 2;
			l = 0;
		}
		else {
			l++;
		}
	}
	sum += (l * (l + 1)) / 2;
	cout << sum << "\n";

}


#include <iostream>
#include <iomanip>
#include <vector>
#include <deque>
#include <algorithm>
#include <ctype.h>
#include <string>
#include <map>
#include <numeric>
#include <set>
#include <array>
#include <functional>

using namespace std;

int main() {
	int n, k;
	cin >> n >> k;
	string s;
	cin >> s;
	vector<char> v;
	char c;
	for (int i = 0; i < k; i++) {
		cin >> c;
		v.push_back(c);
	}
	// (n x (n + 1)) / 2
	int sum = 0;
	int l = 0;
	for (char c : s) {
		if (find(v.begin(), v.end(), c) == v.end()) {
			sum += (l * (l + 1)) / 2;
			l = 0;
		}
		else {
			l++;
		}
	}
	sum += (l * (l + 1)) / 2;
	cout << sum << "\n";

}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

bool used[100];
char str[2000001];
int main() {
	int n, k;
	char c;
	scanf("%d%d", &n, &k);
	scanf("%s\n", str);
	for (int i = 0; i < k - 1; i++) {
		scanf("%c ", &c);
		used[c - 'a'] = true;
	}
	scanf("%c", &c);
	used[c - 'a'] = true;
	int len = 0;
	int ans = 0;
	for (int i = 0; i < n ; i++) {
		if (used[str[i] - 'a']) len++;
		else {
			ans += (len * (len + 1)) / 2;
			len = 0;
		}
	}
	if (used[str[n-1] - 'a']) {
		ans += (len * (len + 1)) / 2;
	}
	printf("%d", ans);
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

int main() {
	int n, k;
	char str[200001];
	char c[26];
	scanf("%d%d", &n, &k);
	scanf("%s\n", str);
	for (int i = 0; i < k - 1; i++) scanf("%c ", &c[i]);
	scanf("%c", &c[k - 1]);
	int len = 0;
	int ans = 0;
	bool flag = false;
	for (int i = 0; i < n; i++) {
		flag = false;
		for (int j = 0; j < k; j++) {
			if (str[i] == c[j]) flag = true;
		}
		if (flag) len++;
		else {
			ans += (len * (len + 1)) / 2;
			len = 0;
		}
	}
	if (flag) ans += (len * (len + 1)) / 2;
	printf("%d", ans);
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

bool used[100];
char str[2000001];
int main() {
	int n, k;
	char c;
	scanf("%d%d", &n, &k);
	scanf("%s\n", str);
	for (int i = 0; i < k - 1; i++) {
		scanf("%c ", &c);
		used[c - 'a'] = true;
	}
	scanf("%c", &c);
	used[c - 'a'] = true;
	long long len = 0;
	long long ans = 0;
	for (int i = 0; i < n ; i++) {
		if (used[str[i] - 'a']) len++;
		else {
			ans += (len * (len + 1)) / 2;
			len = 0;
		}
	}
	if (used[str[n-1] - 'a']) {
		ans += (len * (len + 1)) / 2;
	}
	printf("%lld", ans);
	return 0;
}


#include <iostream>
#include <string>
#include <set>

using namespace std;

int main()
{
	int n, m;
	string str;
	cin >> n >> m >> str;
	set<char> s;
	for (size_t i = 0; i < m; i++)
	{
		char a;
		cin >> a;
		s.insert(a);
	}
	long long int l = 0;
	long long int ans = 0;
	for (size_t i = 0; i < n; i++)
	{
		if (s.find(str[i]) != s.end())
			l++;
		else
		{
			ans += (l*(l + 1) / 2);
			l = 0;
		}
	}
	ans += (l*(l + 1) / 2);
	cout << ans;
}


#include <iostream>
#include <string>
#include <set>

using namespace std;

int main()
{
	int n, m;
	string str;
	cin >> n >> m >> str;
	set<char> s;
	for (size_t i = 0; i < m; i++)
	{
		char a;
		cin >> a;
		s.insert(a);
	}
	int l = 0;
	long long int ans = 0;
	for (size_t i = 0; i < n; i++)
	{
		if (s.find(str[i]) != s.end())
			l++;
		else
		{
			ans += (l*(l + 1) / 2);
			l = 0;
		}
	}
	ans += (l*(l + 1) / 2);
	cout << ans;
}


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

bool arr[27];
vector<long long> v;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int n, k;
	cin >> n >> k;
	string s;
	cin >> s;
	for (int i = 0; i < k; i++) {
		char c;
		cin >> c;
		arr[c - 'a'] = true;
	}
	int cnt = 0;
	for (int i = 0; i < s.size(); i++) {
		if (arr[s[i] - 'a']) cnt++;
		else if(cnt!=0){
			v.push_back(cnt);
			cnt = 0;
		}
	}
	v.push_back(cnt);
	long long ans = 0;
	for (int i = 0; i < v.size(); i++) {
		ans += (v[i] * (v[i] + 1)) / 2;
	}
	cout << ans;
}


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

int main()
{
    int m[200]={0};
    char c[300000],x;
    int n,k;
    cin>>n>>k;
    cin>>c;
    for(int i=0;i<k;i++){
        getchar();
        scanf("%c",&x);
        m[(int)(x)]=1;
    }
    int z=0;
    int sum=0;
    for(int i=0;i<n;i++){
        if(m[(int)(c[i])]==1){
            z+=1;
        }else{
            sum+=z*(z+1)/2;
            z=0;
        }
    }
    sum+=z*(z+1)/2;
    cout<<sum;
    return 0;
}


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

int main()
{
    int m[200]={0};
    char c[300000],x;
    long long int n,k;
    cin>>n>>k;
    cin>>c;
    for(int i=0;i<k;i++){
        getchar();
        scanf("%c",&x);
        m[(int)(x)]=1;
    }
    long long int z=0;
    long long int sum=0;
    for(int i=0;i<n;i++){
        if(m[(int)(c[i])]==1){
            z+=1;
        }else{
            sum+=z*(z+1)/2;
            z=0;
        }
    }
    sum+=z*(z+1)/2;
    cout<<sum;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <bitset>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <deque>
#include <set>
#include <map>
#include <queue>
using namespace std;

#define pb push_back
#define mp make_pair
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long LL;
const LL INF = 2e9;

void solve() {
    int ok[26];
    int n, k;
    string s;
    cin >> n >> k >> s;
    while (k--) {
        char c;
        cin >> c;
        ok[c] = 1;
    }
    LL ans = 0;
    forn(i, n) {
        int pos = i;
        while (i < n && ok[s[i]]) {
            i++;
        }
        ans += 1LL * (i - pos) * (i - pos + 1) / 2;
    }
    cout << ans << endl;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    solve();
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <bitset>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <deque>
#include <set>
#include <map>
#include <queue>
using namespace std;

#define pb push_back
#define mp make_pair
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long LL;
const LL INF = 2e9;

void solve() {
    int ok[26];
    int n, k;
    string s;
    cin >> n >> k >> s;
    while (k--) {
        char c;
        cin >> c;
        ok[c] = 1;
    }
    LL ans = 0;
    forn(i, n) {
        int start = i;
        while (i < n && ok[s[i]]) {
            i++;
        }
        ans += 1LL * (i - start) * (i - start + 1) / 2;
    }
    cout << ans << endl;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    solve();
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <bitset>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <deque>
#include <set>
#include <map>
#include <queue>
using namespace std;

#define pb push_back
#define mp make_pair
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long LL;
const LL INF = 2e9;

void solve() {
    int ok[26];
    int n, k;
    string s;
    cin >> n >> k >> s;
    while (k--) {
        char c;
        cin >> c;
        ok[c] = 1;
    }
    LL ans = 0;
    forn(i, n) {
        int pos = i;
        while (i < n && ok[s[i]]) {
            i++;
        }
        ans += (i - pos) * (i - pos + 1) / 2;
    }
    cout << ans << endl;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    solve();
}


#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<deque>
#include<map>
#include<set>
#include<stack>
#include <numeric>
#include <functional>
#define ll long long
#define ms(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x & -x
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
const int maxn = 2e5 + 50;
const int inf = 1e9;
const int mod = 1e9 + 7;
char s[maxn];
int a[30];
int main()
{
    IOS;
    int n,k;
    cin>>n>>k;
    cin>>s;
    for(int i=0;i<k;i++)
    {
        char ch;
        cin>>ch;
        a[ch-'a']=1;
    }
    ll ans=0,flgage=0,t=0;
    for(int i=0;i<n;i++)
    {
        if(a[s[i]-'a']==0)
        {
            ans+=t*(t+1)/2;
            t=0;
        }
        else
        {
            t++;
        }
    }
    cout<<ans+t*(t+1)/2<<endl;
    return 0;
}


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

int main()
{
	char str[100000],a; long long i, q,n,k,c,ans;
	cin >> q;
	while (q--)
	{
		int l[26] = { 0 };
		ans = 0;
		cin >> n >> k;
		cin >> str;
		for (i = 0; i < k; i++)
		{
			cin >> a;
			l[int(a) -97] = 1;
		}
		c = 0;

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

			while (l[int(str[i]) -97] != 0&&i<n)
			{

				c++;
				i++;
			}
			i++;
			ans += c * (c + 1) / 2;
			c = 0;
		}
		cout << ans << endl;

	}

	return 0;
}


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

int main()
{
	char str[200001],a; long long i,n,k,c,ans;

		int l[26] = { 0 };
		ans = 0;
		cin >> n >> k;
		cin >> str;

		for (i = 0; i < k; i++)
		{
			cin >> a;
			l[int(a) -97] = 1;
		}
		c = 0;

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

			while (l[int(str[i]) -97] != 0&&i<n)
			{

				c++;
				i++;
			}
			i++;
			ans += c * (c + 1) / 2;
			c = 0;
		}
		cout << ans << endl;

	return 0;
}


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

int main()
{
	char str[100000],a; long long i, q,n,k,c,ans;

		int l[26] = { 0 };
		ans = 0;
		cin >> n >> k;
		cin >> str;

		for (i = 0; i < k; i++)
		{
			cin >> a;
			l[int(a) -97] = 1;
		}
		c = 0;

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

			while (l[int(str[i]) -97] != 0&&i<n)
			{

				c++;
				i++;
			}
			i++;
			ans += c * (c + 1) / 2;
			c = 0;
		}
		cout << ans << endl;

	return 0;
}


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

int main()
{
	char str[100000],a; long long i,n,k,c,ans;

		int l[26] = { 0 };
		ans = 0;
		cin >> n >> k;
		cin >> str;

		for (i = 0; i < k; i++)
		{
			cin >> a;
			l[int(a) -97] = 1;
		}
		c = 0;

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

			while (l[int(str[i]) -97] != 0&&i<n)
			{

				c++;
				i++;
			}
			i++;
			ans += c * (c + 1) / 2;
			c = 0;
		}
		cout << ans << endl;

	return 0;
}


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

int main()
{
	char str[100000],a; long long i, q,n,k,c,ans;
	cin >> q;

		int l[26] = { 0 };
		ans = 0;
		cin >> n >> k;
		cin >> str;
		for (i = 0; i < k; i++)
		{
			cin >> a;
			l[int(a) -97] = 1;
		}
		c = 0;

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

			while (l[int(str[i]) -97] != 0&&i<n)
			{

				c++;
				i++;
			}
			i++;
			ans += c * (c + 1) / 2;
			c = 0;
		}
		cout << ans << endl;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <ctime>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>

#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()

using namespace std;

typedef long long ll;
typedef double long dl;

const int MAXN = (int)1e9 + 1;
const int INF = (int)1e9 + 7;
const ll MAXSIZE = 400000;

void input() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifdef _DEBUG
	freopen("input", "r", stdin);
	freopen("output", "w", stdout);
#endif
}

ll mod(ll x, ll m) {
	return (x % m + m) % m;
}

int main() {
	input();
	int q;
	cin >> q;
	int k;
	cin >> k;
	vector<bool> use(26, false);
	string s;
	cin >> s;
	char ch;
	for (int i = 0; i < k; i++) {
		cin >> ch;
		use[ch - 'a'] = true;
	}
	int l = -1, r = 0;
	ll ans = 0;
	bool flag = true;
	for (int i = 0; i < s.size();) {
		while (i < s.size() && !use[s[i] - 'a'])
			i++;
		if (i < s.size()) {
			l = i;
			while (i < s.size() && use[s[i] - 'a'])
				i++;
			if (use[s[i - 1] - 'a']) {
				r = i;
				ans += (r - l) * (r - l + 1) / 2;
				//cout << l << ' ' << r << '\n';
			}
		}
	}
	cout << ans;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <ctime>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>

#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()

using namespace std;

typedef long long ll;
typedef double long dl;

const int MAXN = (int)1e9 + 1;
const int INF = (int)1e9 + 7;
const ll MAXSIZE = 400000;

void input() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifdef _DEBUG
	freopen("input", "r", stdin);
	freopen("output", "w", stdout);
#endif
}

ll mod(ll x, ll m) {
	return (x % m + m) % m;
}

int main() {
	input();
	int q;
	cin >> q;
	int k;
	cin >> k;
	vector<bool> use(26, false);
	string s;
	cin >> s;
	char ch;
	for (int i = 0; i < k; i++) {
		cin >> ch;
		use[ch - 'a'] = true;
	}
	ll l = -1, r = 0;
	ll ans = 0;
	for (int i = 0; i < s.size();) {
		while (i < s.size() && !use[s[i] - 'a'])
			i++;
		if (i < s.size()) {
			l = i;
			while (i < s.size() && use[s[i] - 'a'])
				i++;
			if (use[s[i - 1] - 'a']) {
				r = i;
				ans += (r - l) * (r - l + 1) / 2;
				//cout << l << ' ' << r << '\n';
			}
		}
	}
	cout << ans;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <ctime>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>

#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()

using namespace std;

typedef long long ll;
typedef double long dl;

const int MAXN = (int)1e9 + 1;
const int INF = (int)1e9 + 7;
const ll MAXSIZE = 400000;

void input() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifdef _DEBUG
	freopen("input", "r", stdin);
	freopen("output", "w", stdout);
#endif
}

ll mod(ll x, ll m) {
	return (x % m + m) % m;
}

int main() {
	input();
	int q;
	cin >> q;
	int k;
	cin >> k;
	vector<bool> use(26, false);
	string s;
	cin >> s;
	char ch;
	for (int i = 0; i < k; i++) {
		cin >> ch;
		use[ch - 'a'] = true;
	}
	int l = -1, r = -1;
	ll ans = 0;
	bool flag = true;
	for (int i = 0; i < s.size(); i++) {
		if (use[s[i] - 'a'] && flag) {
			l = i;
			flag = false;
		}
		if (use[s[i] - 'a'])
			r = i;
		else {
			if (r != -1)
				ans += (r - l + 1) * (r - l + 2) / 2;
			//cout << l << ' ' << r << '\n';
			l = -1;
			r = -1;
			flag = true;
		}
	}
	//cout << l << ' ' << r << '\n';
	if (r != -1)
		ans += (r - l + 1) * (r - l + 2) / 2;
	cout << ans;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <ctime>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>

#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()

using namespace std;

typedef long long ll;
typedef double long dl;

const int MAXN = (int)1e9 + 1;
const int INF = (int)1e9 + 7;
const ll MAXSIZE = 400000;

void input() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifdef _DEBUG
	freopen("input", "r", stdin);
	freopen("output", "w", stdout);
#endif
}

ll mod(ll x, ll m) {
	return (x % m + m) % m;
}

void z_function(string s, vector<int>& z, vector<bool> &use) {
	int n = s.size();
	z.resize(n);
	z.assign(n, 0);
	int cnt = 0;
	while (cnt < n && use[s[cnt] - 'a']) {
		cnt++;
	}
	z[0] = cnt;
	for (int i = 1, l = 0, r = 0; i < n; i++) {
		cnt = 0;
		if (i <= r) {
			cnt = min(r - i + 1, z[i - l]);
		}
		while (i + cnt < n && use[s[i + cnt] - 'a']) {
			cnt++;
		}
		if (i + cnt - 1 > r) {
			l = i;
			r = i + cnt - 1;
		}
		z[i] = cnt;
	}
}

int main() {
	input();
	int q;
	cin >> q;
	int k;
	cin >> k;
	vector<bool> use(26, false);
	vector<int> z;
	string s;
	cin >> s;
	char ch;
	for (int i = 0; i < k; i++) {
		cin >> ch;
		use[ch - 'a'] = true;
	}
	z_function(s, z, use);
	ll ans = 0;
	for (int i = 0; i < z.size(); i++) {
		ans += z[i];
	}
	cout << ans;
	return 0;
}


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

int main()
{
	int n, k;
	long long sum = 0, len = 0;
	string s;
	bool alpha[26] = { false };
	cin >> n >> k;
	cin >> s;
	for (int i = 0; i < k; i++) {
		char c; cin >> c;
		alpha[c - 'a'] = true;
	}
	for (int i = 0; i < n; i++) {
		if (alpha[s[i] - 'a']) len++;
		else {
			sum += (len*(len + 1)) / 2;
			len = 0;
		}
	}
	sum += (len*(len + 1)) / 2;
	len = 0;
	cout << sum;
	return 0;
}


// 5565. 영수증

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

int main()
{
	int n, k, sum = 0, len = 0;
	string s;
	bool alpha[26] = { false };
	cin >> n >> k;
	cin >> s;
	for (int i = 0; i < k; i++) {
		char c; cin >> c;
		alpha[c - 'a'] = true;
	}
	for (int i = 0; i < n; i++) {
		if (alpha[s[i] - 'a']) len++;
		else {
			sum += (len*(len + 1)) / 2;
			len = 0;
		}
	}
	sum += (len*(len + 1)) / 2;
	len = 0;
	cout << sum;
	return 0;
}


#include <stdio.h>
#define M 3*100000
int main()
{
	char ch[M] = {'\0'};
	char useful[1000];
	long long n, k;
	scanf("%d %d", &n, &k);
	scanf("%s", ch);
	for(int i = 0; i < k; i++){
		getchar();
		scanf("%c", &useful[i]);
	}
	for(int i = 0; i < n; i++){
		for(int j = 0; j < k; j++){
			if(ch[i] == useful[j]){
				ch[i] = '1';
				break;
			}
		}
	}
	long long sum = 0, lianxu = 0;
	for(long i = 0; i < n;){
		lianxu = 0;
		while(ch[i++] == '1'){
			lianxu++;
		}sum+=lianxu*(lianxu+1)/2;
	}
	printf("%lld", sum);
	return 0;
}


#include <stdio.h>
#define M 3*100000
int main()
{
	char ch[M] = {'\0'};
	char useful[1000];
	int n, k;
	scanf("%d %d", &n, &k);
	scanf("%s", ch);
	for(int i = 0; i < k; i++){
		getchar();
		scanf("%c", &useful[i]);
	}
	for(int i = 0; i < n; i++){
		for(int j = 0; j < k; j++){
			if(ch[i] == useful[j]){
				ch[i] = '1';
				break;
			}
		}
	}
	int sum = 0, lianxu = 0;
	for(int i = 0; i < n;){
		lianxu = 0;
		while(ch[i++] == '1'){
			lianxu++;
		}sum+=lianxu*(lianxu+1)/2;
	}
	printf("%d", sum);
	return 0;
}


#include<iostream>
using namespace std;
int n, k;
char s[200005];
char ch[30];
int main()
{
    cin >> n >> k;
    cin >> s;
    for(int i = 0; i < k; i++) cin >> ch[i];
    long long ans = 0;
    long long len = 0;
    bool flag;
    for(int i = 0; i < n; i++){
        flag = false;
        for(int j = 0; j < k; j++){
            if(s[i] == ch[j]){
                flag = true;
                len++;
                break;
            }
        }
        if(!flag || i == n - 1){
            ans += (len + 1) * len / 2;
            len = 0;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <string>
#include <set>

using namespace std;

int main()
{
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    set<char> st;
    for (int i = 0; i < k; ++i) {
        char c;
        cin >> c;
        st.insert(c);
    }

    long long ans = 0;
    for (int i = 0; i < n; ++i) {
        int j = i;
        while (j < n && st.count(s[j])) ++j;
        int len = j - i;
        ans += len * 1ll * (len + 1) / 2;
        i = j;
    }
    cout << ans << endl;
    return 0;
}


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

using namespace std;

int main()
{
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    vector<char> keys;
    keys.reserve(k);
    char c;
    for (int i = 0; i<k; ++i)
    {
        cin >> c;
        keys.push_back(c);
    }
    long long result = 0;
    int count = 0;
    bool is_find = false;
    for (size_t i = 0; i < s.size(); ++i)
    {
        for (size_t j = 0; j < keys.size(); ++j)
        {
            is_find = false;
            if (s[i] == keys[j])
            {
                is_find = true;
                count++;
                break;
            }
        }
        if (!is_find || i == s.size() - 1)
        {
           result += ((count + 0) * (count + 1)) / 2;
           count = 0;
        }

    }

    cout << result << endl;
    return 0;
}


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

using namespace std;

int main()
{
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    vector<char> keys;
    keys.reserve(k);
    char c;
    for (int i = 0; i<k; ++i)
    {
        cin >> c;
        keys.push_back(c);
    }
    int result = 0;
    int count = 0;
    bool is_find = false;
    for (size_t i = 0; i < s.size(); ++i)
    {
        for (size_t j = 0; j < keys.size(); ++j)
        {
            is_find = false;
            if (s[i] == keys[j])
            {
                is_find = true;
                count++;
                break;
            }
        }
        if (!is_find || i == s.size() - 1)
        {
           result += ((count + 0) * (count + 1)) / 2;
           count = 0;
        }

    }

    cout << result << endl;
    return 0;
}


#include "stdio.h"
#include "iostream"
#include "string"

using namespace std;

int main()
{
	int n, k, x = 0, length = 0;
	int c = 1;
	string row;
	bool b = false;
	char key[26] = { ' ' };
	cin >> n >> k >> row;
	for (int i = 0; i < k; i++)
		cin >> key[i];
	for (int i = 0; i < row.length() - 1 && row.length() > 1; i++)
	{
		if (row[i] == row[i + 1]) c++;
	}
	for (int i = 0; i < row.length(); i++)
	{
		for (int j = 0; j < row.length(); j++)
		{
			if (b == true) break;
			for (int l = 0; l < k; l++)
				if (row[j] == key[l])
				{
					b = true;
					break;
				}
		}
		if (c == row.length() && b == true)
		{
			for (; c > 0; c--)
				x += c;
			break;
		}
		for (int j = 0; j < k; j++)
		{
			if (key[j] == row[i])
			{
				length++;
				i++;
				j = -1;
			}
		}
		if (length > 3) x += length * 3;
		else if (length <= 3 && length > 1) x += length * 2;
		else if (length == 1) x ++;
		length = 0;
	}
	cout << x << endl;
}


#include "stdio.h"
#include "iostream"
#include "string"

using namespace std;

int main()
{
	int n, k, x = 0, length = 0;
	string row;
	char key[26] = { ' ' };
	cin >> n >> k >> row;
	for (int i = 0; i < k; i++)
		cin >> key[i];
	for (int i = 0; i < row.length(); i++)
	{
		for (int j = 0; j < k; j++)
		{
			if (key[j] == row[i])
			{
				length++;
				i++;
				j = -1;
			}
		}
		if (length == 1) x++;
		else if (length == 2) x += 4;
		else if (length >= 3)
		{
			x += (length*(length + 1)) / 2;
		}
		length = 0;
	}
	cout << x << endl;
}


#include "stdio.h"
#include "iostream"
#include "string"

using namespace std;

int main()
{
	int n, k;
	long long x = 0, length = 0;
	string row;
	char key[26] = { ' ' };
	cin >> n >> k >> row;
	for (int i = 0; i < k; i++)
		cin >> key[i];
	for (int i = 0; i < row.length(); i++)
	{
		for (int j = 0; j < k; j++)
		{
			if (key[j] == row[i])
			{
				length++;
				i++;
				j = -1;
			}
		}
		x += (length*(length + 1)) / 2;
		length = 0;
	}
	cout << x << endl;
}


#include "stdio.h"
#include "iostream"
#include "string"

using namespace std;

int main()
{
	int n, k, x = 0, length = 0;
	char *row = new char;
	char key[26] = { ' ' };
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> *(row + i);
	}
	for (int i = 0; i < k; i++)
	{
		cin >> *(key + i);
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < k; j++)
		{
			if (*(key + j) == *(row + i))
			{
				length++;
				j = -1;
			}
		}
		x += (length*(length + 1)) / 2;
		length = 0;
	}
	cout << x << endl;
}


#include "stdio.h"
#include "iostream"
#include "string"

using namespace std;

int main()
{
	int n, k, x = 0, length = 0;
	string row;
	char key[26] = { ' ' };
	cin >> n >> k >> row;
	for (int i = 0; i < k; i++)
		cin >> key[i];
	for (int i = 0; i < row.length(); i++)
	{
		for (int j = 0; j < k; j++)
		{
			if (key[j] == row[i])
			{
				length++;
				i++;
				j = -1;
			}
		}
		if (length == 1) x++;
		else if (length == 2) x += 4;
		else if (length >= 3)
		{
			for (; length > 0; length--)
				x += length;
		}
		length = 0;
	}
	cout << x << endl;
}


#include "stdio.h"
#include "iostream"
#include "string"

using namespace std;

int main()
{
	int n, k, x = 0, length = 0;
	string row;
	char key[26] = { ' ' };
	cin >> n >> k >> row;
	for (int i = 0; i < k; i++)
		cin >> key[i];
	for (int i = 0; i < row.length(); i++)
	{
		for (int j = 0; j < k; j++)
		{
			if (key[j] == row[i])
			{
				length++;
				i++;
				j = -1;
			}
		}
		if (length > 3) x += length * 3;
		else if (length <= 3 && length > 0) x += length * 2;
		length = 0;
	}
	cout << x << endl;
	system("pause");
	return 0;
}


#include "stdio.h"
#include "iostream"
#include "string"

using namespace std;

int main()
{
	int n, k, length = 0;
	long long x = 0;
	string row;
	char key[26] = { ' ' };
	cin >> n >> k >> row;
	for (int i = 0; i < k; i++)
		cin >> key[i];
	for (int i = 0; i < row.length(); i++)
	{
		for (int j = 0; j < k; j++)
		{
			if (key[j] == row[i])
			{
				length++;
				i++;
				j = -1;
			}
		}
		for (int j = 0; length != 0; length--)
		{
			x += length;
		}
	}
	cout << x << endl;
}


#include "stdio.h"
#include "iostream"
#include "string"

using namespace std;

int main()
{
	int n, k, x = 0, length = 0;
	char *row = new char;
	char key[26] = { ' ' };
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> *(row + i);
	}
	for (int i = 0; i < k; i++)
	{
		cin >> *(key + i);
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < k; j++)
		{
			if (*(key + j) == *(row + i))
			{
				length++;
				i++;
				j = -1;
			}
		}
		x += (length*(length + 1)) / 2;
		length = 0;
	}
	cout << x << endl;
}


#include "stdio.h"
#include "iostream"
#include "string"

using namespace std;

int main()
{
	int n, k, x = 0, length = 0;
	int c = 1;
	string row;
	bool b = false;
	char key[26] = { ' ' };
	cin >> n >> k >> row;
	for (int i = 0; i < k; i++)
		cin >> key[i];
	for (int i = 0; i < row.length() - 1 && row.length() > 1; i++)
	{
		if (row[i] == row[i + 1]) c++;
	}
	for (int i = 0; i < row.length(); i++)
	{
		for (int j = 0; j < row.length(); j++)
		{
			if (b == true) break;
			for (int l = 0; l < k; l++)
				if (row[j] == key[l])
				{
					b = true;
					break;
				}
		}
		if (c == row.length() && b == true)
		{
			for (; c > 0; c--)
				x += c;
			break;
		}
		for (int j = 0; j < k; j++)
		{
			if (key[j] == row[i])
			{
				length++;
				i++;
				j = -1;
			}
		}
		if (length > 3) x += length * 3;
		else if (length <= 3 && length > 0) x += length * 2;
		length = 0;
	}
	cout << x << endl;
}


#include "stdio.h"
#include "iostream"
#include "string"

using namespace std;

int main()
{
	int n, k, x = 0, length = 0;
	string row;
	char key[26] = { ' ' };
	cin >> n >> k >> row;
	for (int i = 0; i < k; i++)
		cin >> key[i];
	for (int i = 0; i < row.length(); i++)
	{
		for (int j = 0; j < k; j++)
		{
			if (key[j] == row[i])
			{
				length++;
				i++;
				j = -1;
			}
		}
		x += (length*(length + 1)) / 2;
		length = 0;
	}
	cout << x << endl;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
const long long  INF=1e9+10;
long long aa[maxn];
int n,m;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        long long  minn=INF;
        long long  maxx=-INF;
        int k;
        bool a=0,b=0;
        for(int i=0;i<n;i++){
            cin>>aa[i];
            minn=min(minn,aa[i]);
            maxx=max(maxx,aa[i]);
        }
        if(m==1)  cout<<minn<<endl;
        else if(m>=3)  cout<<maxx<<endl;
        else {
              cout<<max(aa[0],aa[n-1])<<endl;
        }
    }
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
const long long  INF=9999999999;
long long aa[maxn];
int n,m;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        long long  minn=INF;
        long long maxx=-INF;
        int k;
        for(int i=0;i<n;i++){
            cin>>aa[i];
            minn=min(minn,aa[i]);
            if(maxx<aa[i]){
                maxx=aa[i];
                k=i;
            }
        }
        if(m==1)  cout<<minn<<endl;
        else if(m>=3)  cout<<maxx<<endl;
        else {
            if(k==n-1)  cout<<maxx<<endl;
            else {
                sort(aa,aa+n);
                cout<<aa[1]<<endl;
            }

        }
    }
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
const long long  INF=9999999999;
long long aa[maxn];
int n,m;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        long long  minn=INF;
        long long  maxx=-INF;
        int k;
        for(int i=0;i<n;i++){
            cin>>aa[i];
            minn=min(minn,aa[i]);
            if(maxx<aa[i]){
                maxx=aa[i];
                k=i;
            }
        }
        if(m==1)  cout<<minn<<endl;
        else if(m>=3)  cout<<maxx<<endl;
        else {
            if(k==n-1||k==0)  cout<<maxx<<endl;
            else {
                sort(aa,aa+n);
                cout<<aa[1]<<endl;
            }

        }
    }
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
const long long  INF=1e9+10;
long long aa[maxn];
int n,m;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        long long  minn=INF;
        long long  maxx=-INF;
        int k;
        for(int i=0;i<n;i++){
            cin>>aa[i];
            minn=min(minn,aa[i]);
            if(maxx<=aa[i]){
                maxx=aa[i];
                k=i;
            }
        }
        if(m==1)  cout<<minn<<endl;
        else if(m>=3)  cout<<maxx<<endl;
        else {
            if(k==n-1||k==0)  cout<<maxx<<endl;
            else {
                sort(aa,aa+n);
                cout<<aa[1]<<endl;
            }
        }
    }
}


#include<bits/stdc++.h>
#define ll long long
#define icyws
ll read(){
	ll x=0,f=1;char ch=getchar();
	while(ch>'9' or ch<'0'){if(ch=='-')f*=-1;ch=getchar();}
	while(ch>='0' and ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
ll data[100000],maxint=-1e11,minint=1e11;
ll za[100000];
ll zb[100000];
using std::cout;
using std::endl;
using std::max;
using std::min;
using std::cerr;
int main(){
#ifdef icys
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	ll n=read(),k=read();
	for(int i=1;i<=n;i++){
		data[i]=read();
		maxint=max(maxint,data[i]);
		minint=min(minint,data[i]);
	}
	za[1]=data[1],zb[n]=data[n];
	for(int i=2;i<=n;i++){
		za[i]=min(za[i-1],data[i]);
	}
	for(int i=n-1;i>=1;i--){
		zb[i]=min(zb[i+1],data[i]);
	}
	if(n==k){
		cout<<maxint<<endl;
	}
	else{
		if(k==1)cout<<minint<<endl;
		if(k==2){
			ll ans=-1e11;
			for(int i=1;i<=n;i++){
				ans=max(ans,za[i]);
				ans=max(ans,zb[i]);
			}
			cout<<ans<<endl;
		}
		if(k==3){
			cout<<maxint<<endl;
		}
		return 0;
	}
}


#include<bits/stdc++.h>
#define ll long long
#define icyws
ll read(){
	ll x=0,f=1;char ch=getchar();
	while(ch>'9' or ch<'0'){if(ch=='-')f*=-1;ch=getchar();}
	while(ch>='0' and ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
ll data[100000],maxint=-1e11,minint=1e11;
ll za[100000];
ll zb[100000];
using std::cout;
using std::endl;
using std::max;
using std::min;
using std::cerr;
int main(){
#ifdef icys
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif

	ll n=read(),k=read();
	for(int i=1;i<=n;i++){
		za[i]=zb[i]=data[i]=read();
		maxint=max(maxint,data[i]);
		minint=min(minint,data[i]);
	}
	za[1]=data[1],zb[n]=data[n];
	for(int i=2;i<=n;i++){
		za[i]=min(za[i-1],data[i]);
	}
	for(int i=n-1;i>=1;i--){
		zb[i]=min(zb[i+1],data[i]);
	}
	if(n==k){
		cout<<maxint<<endl;
	}
	else{
		if(k==1)cout<<minint<<endl;
		if(k==2){
			ll ans=-1e11;
			for(int i=1;i<n;i++){
				ans=max(ans,za[i]);
				ans=max(ans,zb[i+1]);
			}
			ans=max(ans,za[n]);
			cout<<ans<<endl;
		}
		if(k>=3){
			cout<<maxint<<endl;
		}
		return 0;
	}
}


#include<bits/stdc++.h>
#define ll long long
#define icyws
ll read(){
	ll x=0,f=1;char ch=getchar();
	while(ch>'9' or ch<'0'){if(ch=='-')f*=-1;ch=getchar();}
	while(ch>='0' and ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
ll data[100000],maxint=-1e11,minint=1e11;
ll za[100000];
ll zb[100000];
using std::cout;
using std::endl;
using std::max;
using std::min;
using std::cerr;
int main(){
#ifdef icys
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	ll n=read(),k=read();
	for(int i=1;i<=n;i++){
		data[i]=read();
		maxint=max(maxint,data[i]);
		minint=min(minint,data[i]);
	}
	za[1]=data[1],zb[n]=data[n];
	for(int i=2;i<=n;i++){
		za[i]=min(za[i-1],data[i]);
	}
	for(int i=n-1;i>=1;i--){
		zb[i]=min(zb[i+1],data[i]);
	}
	if(n==k){
		cout<<maxint<<endl;
	}
	else{
		if(k==1)cout<<minint<<endl;
		if(k==2){
			ll ans=-1e11;
			for(int i=1;i<=n;i++){
				ans=max(ans,za[i]);
				ans=max(ans,zb[i+1]);
			}
			cout<<ans<<endl;
		}
		if(k==3){
			cout<<maxint<<endl;
		}
		return 0;
	}
}


#include<bits/stdc++.h>
#define ll long long
#define icyws
ll read(){
	ll x=0,f=1;char ch=getchar();
	while(ch>'9' or ch<'0'){if(ch=='-')f*=-1;ch=getchar();}
	while(ch>='0' and ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
ll data[100000],maxint=-1e11,minint=1e11;
ll za[100000];
ll zb[100000];
using std::cout;
using std::endl;
using std::max;
using std::min;
using std::cerr;
int main(){
#ifdef icys
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	ll n=read(),k=read();
	for(int i=1;i<=n;i++){
		data[i]=read();
		maxint=max(maxint,data[i]);
		minint=min(minint,data[i]);
	}
	za[1]=data[1],zb[n]=data[n];
	for(int i=2;i<=n;i++){
		za[i]=min(za[i-1],data[i]);
	}
	for(int i=n-1;i>=1;i--){
		zb[i]=min(zb[i+1],data[i]);
	}
	if(k==1)cout<<minint<<endl;
	if(k==2){
		ll ans=-1e11;
		for(int i=1;i<=n;i++){
			ans=max(ans,za[i]);
			ans=max(ans,zb[i]);
		}
		cout<<ans<<endl;
	}
	if(k==3){
		cout<<maxint<<endl;
	}
	return 0;
}


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

typedef long long ll;
const int mod=1e9+7;
#define mem(s,v) memset(s,v,sizeof(s))
#define inf 0x3f3f3f3f
#define maxn 100010

int a[maxn];
int n,m;

int main()
{
	cin>>n>>m;
	for(int i=0;i<n;++i) cin>>a[i];
	sort(a,a+n);
	if(m==1) cout<<a[0]<<endl;
	else if(m==2) cout<<max(a[0],a[n-1])<<endl;
	else cout<<a[n-1]<<endl;
	return 0;
}

/*
8 2
-451199021 453228716 -37430259 793076270 974802521 -336811423 828855687 999975763
*/


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<math.h>
using namespace std;

typedef long long ll;
const int mod=1e9+7;
#define mem(s,v) memset(s,v,sizeof(s))
#define inf 0x3f3f3f3f
#define maxn 100010

ll a[maxn];
ll n,m;

int main()
{
	cin>>n>>m;
	for(int i=0;i<n;++i) cin>>a[i];
	sort(a,a+n);
	if(m==1) cout<<a[0]<<endl;
	else if(m==2) cout<<max(a[0],a[n-1])<<endl;
	else cout<<a[n-1]<<endl;
	return 0;
}

/*
8 2
-451199021 453228716 -37430259 793076270 974802521 -336811423 828855687 999975763
*/


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<math.h>
using namespace std;

typedef long long ll;
const int mod=1e9+7;
#define mem(s,v) memset(s,v,sizeof(s))
#define inf 0x3f3f3f3f
#define maxn 100010

ll a[maxn];
ll n,m;

int main()
{
	ll i,j;
	cin>>n>>m;
	for(i=0;i<n;++i) cin>>a[i];
	sort(a,a+n);
	if(m==1) cout<<a[0]<<endl;
	else if(m==2) cout<<max(a[0],a[n-1]);
	else cout<<a[n-1];
	return 0;
}

/*
8 2
-451199021 453228716 -37430259 793076270 974802521 -336811423 828855687 999975763
*/


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<math.h>
using namespace std;

typedef long long ll;
const int mod=1e9+7;
#define mem(s,v) memset(s,v,sizeof(s))
#define inf 0x3f3f3f3f
#define maxn 100010

int main()
{
	int n,m,i,j;
	ll a[maxn];
	while(~scanf("%d%d",&n,&m)){
		for(i=0;i<n;++i) cin>>a[i];
		sort(a,a+n);
		if(m==1) cout<<a[0]<<endl;
		else cout<<a[n-1]<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<math.h>
using namespace std;

typedef long long ll;
const int mod=1e9+7;
#define mem(s,v) memset(s,v,sizeof(s))
#define inf 0x3f3f3f3f
#define maxn 100010

ll a[maxn];
int n,m;

int main()
{
	int i,j;
	while(~scanf("%d%d",&n,&m)){
		for(i=0;i<n;++i) scanf("%lld",&a[i]);
		sort(a,a+n);
		if(m==1) printf("%lld\n",a[0]);
		else printf("%lld\n",a[n-1]);
	}
	return 0;
}

/*
8 2
-451199021 453228716 -37430259 793076270 974802521 -336811423 828855687 999975763
*/


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<math.h>
using namespace std;

typedef long long ll;
const int mod=1e9+7;
#define mem(s,v) memset(s,v,sizeof(s))
#define inf 0x3f3f3f3f
#define maxn 100010

ll a[maxn];
int n,m;

int main()
{
	int i,j;
	while(~scanf("%d%d",&n,&m)){
		for(i=0;i<n;++i) scanf("%lld",&a[i]);
		sort(a,a+n);
		if(m==1) printf("%lld\n",a[0]);
		else if(m==2) printf("%lld\n",max(a[0],a[n-1]));
		else printf("%lld\n",a[n-1]);
	}
	return 0;
}

/*
8 2
-451199021 453228716 -37430259 793076270 974802521 -336811423 828855687 999975763
*/


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<math.h>
using namespace std;

typedef long long ll;
const int mod=1e9+7;
#define mem(s,v) memset(s,v,sizeof(s))
#define inf 0x3f3f3f3f
#define maxn 100010

ll a[maxn];
ll n,m;

int main()
{
	ll i,j;
	cin>>n>>m;
	for(i=0;i<n;++i) cin>>a[i];
	sort(a,a+n);
	if(m==1) cout<<a[0]<<endl;
	else if(m==2) cout<<max(a[0],a[n-1])<<endl;
	else cout<<a[n-1]<<endl;
	return 0;
}

/*
8 2
-451199021 453228716 -37430259 793076270 974802521 -336811423 828855687 999975763
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define Clear(a,x) memset(a,x,sizeof(a))
#define ll long long
#define INF 2000000000
#define eps 1e-8
using namespace std;
int read(){
	int x=0,f=1;
	char ch=getchar();
	while (ch<'0'||ch>'9') f=ch=='-'?-1:f,ch=getchar();
	while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x*f;
}
const int maxn=100005;
int n,k,Min,Max,ans;
int a[maxn],mn[maxn];
int f[maxn][17];
void init(){
	mn[0]=-1;
	rep(i,1,n)
		f[i][0]=a[i];
	rep(i,1,n)
		mn[i]=((i&(i-1))==0)?mn[i-1]+1:mn[i-1];
	rep(j,1,mn[n])
		for (int i=1;i+(1<<j)-1<=n;i++)
			f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
}
int find(int l,int r){
	int k=mn[r-l+1];
	return min(f[l][k],f[r-(1<<k)+1][k]);
}
int main(){
	n=read(),k=read();
	rep(i,1,n)
		a[i]=read();
	if (k==1){
		Min=INF;
		rep(i,1,n)
			Min=min(Min,a[i]);
		printf("%d\n",Min);
		return 0;
	}
	if (k>=3){
		Max=0;
		rep(i,1,n)
			Max=min(Max,a[i]);
		printf("%d\n",Max);
		return 0;
	}
	init();
	ans=0;
	rep(i,1,n-1){
		int x=find(1,i);
		int y=find(i+1,n);
		ans=max(ans,max(x,y));
	}
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define Clear(a,x) memset(a,x,sizeof(a))
#define ll long long
#define INF 2000000000
#define eps 1e-8
using namespace std;
int read(){
	int x=0,f=1;
	char ch=getchar();
	while (ch<'0'||ch>'9') f=ch=='-'?-1:f,ch=getchar();
	while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x*f;
}
const int maxn=100005;
int n,k,Min,Max,ans;
int a[maxn],mn[maxn];
int f[maxn][17];
int main(){
	n=read(),k=read();
	rep(i,1,n)
		a[i]=read();
	if (k==1){
		Min=INF;
		rep(i,1,n)
			Min=min(Min,a[i]);
		printf("%d\n",Min);
		return 0;
	}
	if (k>=3){
		Max=0;
		rep(i,1,n)
			Max=min(Max,a[i]);
		printf("%d\n",Max);
		return 0;
	}
	printf("%d\n",max(a[1],a[n]));
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define Clear(a,x) memset(a,x,sizeof(a))
#define ll long long
#define INF 2000000000
#define eps 1e-8
using namespace std;
int read(){
	int x=0,f=1;
	char ch=getchar();
	while (ch<'0'||ch>'9') f=ch=='-'?-1:f,ch=getchar();
	while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x*f;
}
const int maxn=100005;
int n,k,Min,Max,ans;
int a[maxn];
int main(){
	n=read(),k=read();
	rep(i,1,n)
		a[i]=read();
	if (k==1){
		Min=INF;
		rep(i,1,n)
			Min=min(Min,a[i]);
		printf("%d\n",Min);
		return 0;
	}
	if (k>=3){
		Max=-INF;
		rep(i,1,n)
			Max=min(Max,a[i]);
		printf("%d\n",Max);
		return 0;
	}
	printf("%d\n",max(a[1],a[n]));
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define Clear(a,x) memset(a,x,sizeof(a))
#define ll long long
#define INF 2000000000
#define eps 1e-8
using namespace std;
int read(){
	int x=0,f=1;
	char ch=getchar();
	while (ch<'0'||ch>'9') f=ch=='-'?-1:f,ch=getchar();
	while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x*f;
}
const int maxn=100005;
int n,k,Min,Max,ans;
int a[maxn];
int main(){
	n=read(),k=read();
	rep(i,1,n)
		a[i]=read();
	if (k==1){
		Min=INF;
		rep(i,1,n)
			Min=min(Min,a[i]);
		printf("%d\n",Min);
		return 0;
	}
	if (k>=3){
		Max=-INF;
		rep(i,1,n)
			Max=max(Max,a[i]);
		printf("%d\n",Max);
		return 0;
	}
	printf("%d\n",max(a[1],a[n]));
	return 0;
}


#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;

int main(){
    int n,k;
    scanf("%d %d",&n,&k);
    int maxn = INF,minn = 0;
    for(int i = 0;i < n;i++){
        scanf("%d",&tmp);
        maxn = max(maxn,tmp);
        minn = min(minn,tmp);
    }

    if(k != 1) printf("%d\n",maxn);
    else printf("%d\n",minn);
}


#include<bits/stdc++.h>
#define INF 1e9
#define MAXN 100500
using namespace std;

int a[MAXN];

int main(){
    int n,k;
    scanf("%d %d",&n,&k);
    int maxn = -INF,minn = INF;
    for(int i = 0;i < n;i++){
        scanf("%d",&a[i]);
        maxn = max(maxn,a[i]);
        minn = min(minn,a[i]);
    }

    if(k >= 3) printf("%d\n",maxn);
    else if(k == 2){
        printf("%d\n",max(a[0],a[n - 1]));
    }else{
        printf("%d\n",minn);
    }
}
/*
5 2
3 1 2 4 1
*/


#include<bits/stdc++.h>
#define INF 1e9
using namespace std;

int main(){
    int n,k;
    scanf("%d %d",&n,&k);
    int maxn = -INF,minn = INF;
    for(int i = 0;i < n;i++){
        scanf("%d",&tmp);
        maxn = max(maxn,tmp);
        minn = min(minn,tmp);
    }

    if(k != 1) printf("%d\n",maxn);
    else printf("%d\n",minn);
}


#include<bits/stdc++.h>
#define INF 1e9
#define MAXN 100500
using namespace std;

int a[MAXN];

int main(){
    int n,k;
    scanf("%d %d",&n,&k);
    int maxn = -INF,minn = INF;
    for(int i = 0;i < n;i++){
        scanf("%d",&a[i]);
        maxn = max(maxn,a[i]);
        minn = min(minn,a[i]);
    }

    if(k >= 3) printf("%d\n",maxn);
    else if(k == 2){
        int tmp = INF;
        int ans = 0;
        for(int i = n - 1;i >= 0;i--){
            if(a[i] < tmp){
                tmp = a[i];
                ans = max(ans,tmp);
            }
        }
        printf("%d\n",ans);
    }else{
        printf("%d\n",minn);
    }
}
/*
5 2
1 2 4 5 3
*/


#include<bits/stdc++.h>
#define INF 1e9
#define MAXN 100500
using namespace std;

int a[MAXN];

int main(){
    int n,k;
    scanf("%d %d",&n,&k);
    int maxn = -INF,minn = INF;
    for(int i = 0;i < n;i++){
        scanf("%d",&a[i]);
        maxn = max(maxn,a[i]);
        minn = min(minn,a[i]);
    }

    if(k >= 3) printf("%d\n",maxn);
    else if(k == 2){
        int tmp = INF;
        int ans = 0;
        for(int i = n - 1;i > 0;i--){
            tmp = min(tmp,a[i]);
            ans = max(ans,tmp);
        }
        ans = max(a[0],ans);
        printf("%d\n",ans);
    }else{
        printf("%d\n",minn);
    }
}
/*
5 2
3 1 2 4 1
*/


/*	Codeforces 872B Maximum of Maximums of Minimums
	1st Edition:2017.10.20 Friday
	Algorithm:Greedy
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

#define For(a,b,c) for(ll (a)=(ll)(b);(a)<=(ll)(c);a++)
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
const ll inf=(ll)2e9;
const ll inf_ll=(ll)4e18;
const ll maxn=100005LL;
const ll mod=998244353LL;

//main
int n,k;
int a[maxn];
int minv,maxv;
int lmin[maxn],rmin[maxn];

int main(){
	cin>>n>>k;
	minv=inf;
	lmin[0]=inf;
	rmin[n+1]=inf;
	For(i,1,n){
		cin>>a[i];
		maxv=max(maxv,a[i]);
		minv=min(minv,a[i]);
		lmin[i]=minv;
	}
	for(int i=n;i>=1;i--)
		rmin[i]=min(rmin[i+1],a[i]);
	if(k>=3)
		printf("%d\n",maxv);
	else if(k==2){
		int res=-inf;
		For(i,1,n)
			res=max(res,min(lmin[i],rmin[i+1]));
		printf("%d\n",res);
	}else printf("%d\n",minv);
	return 0;
}


/*	Codeforces 872B Maximum of Maximums of Minimums
	1st Edition:2017.10.20 Friday
	Algorithm:Greedy
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

#define For(a,b,c) for(ll (a)=(ll)(b);(a)<=(ll)(c);a++)
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
const ll inf=(ll)2e9;
const ll inf_ll=(ll)4e18;
const ll maxn=100005LL;
const ll mod=998244353LL;

//main
int n,k;
int a[maxn];
int lmin,rmin,minv,pos;

int main(){
	cin>>n>>k;
	minv=lmin=rmin=inf;
	a[0]=-inf;
	For(i,1,n){
		cin>>a[i];
		if(a[i]>a[pos])
			pos=i;
		minv=min(minv,a[i]);
	}
	For(i,1,pos-1)
		lmin=min(lmin,a[i]);
	For(i,pos+1,n)
		rmin=min(rmin,a[i]);
	if(k>=3)
		printf("%d\n",a[pos]);
	else if(k==2)
		printf("%d\n",max(min(a[pos],lmin),min(a[pos],rmin)));
	else printf("%d\n",minv);
	return 0;
}


/*	Codeforces 872B Maximum of Maximums of Minimums
	1st Edition:2017.10.20 Friday
	Algorithm:Greedy
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

#define For(a,b,c) for(ll (a)=(ll)(b);(a)<=(ll)(c);a++)
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
const ll inf=(ll)2e9;
const ll inf_ll=(ll)4e18;
const ll maxn=100005LL;
const ll mod=998244353LL;

//main
int n,k;
int a[maxn];
int minv,maxv;
int lmin[maxn],rmin[maxn];

int main(){
	cin>>n>>k;
	minv=inf;maxv=-inf;
	lmin[0]=inf;
	rmin[n+1]=inf;
	For(i,1,n){
		cin>>a[i];
		maxv=max(maxv,a[i]);
		minv=min(minv,a[i]);
		lmin[i]=minv;
	}
	for(int i=n;i>=1;i--)
		rmin[i]=min(rmin[i+1],a[i]);
	if(k>=3)
		printf("%d\n",maxv);
	else if(k==2){
		int res=-inf;
		For(i,1,n-1)
			res=max(res,max(lmin[i],rmin[i+1]));
		printf("%d\n",res);
	}else printf("%d\n",minv);
	return 0;
}


//#define NOSTDCPP
#ifndef NOSTDCPP

#include <bits/stdc++.h>

#else

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <queue>
#include <stack>

#endif

#define RESET(_) memset(_, 0, sizeof(_))
#define RESET_(_, val) memset(_, val, sizeof(_))
#define mp make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef vector <int> vi;
typedef set <int> si;
typedef pair <int, int> pii;

const int MOD = 1e9 + 7;
const int maxn = 300009;
const int maxm = 300009;
const double pi = acos(-1.0);
const double eps = 1e-6;

int n, k;
ll data[maxn];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	while(cin >> n >> k)
	{
		cin >> data[1];
		ll minx = data[1], maxx = data[1];
		for(int i = 2;i <= n;i ++)
			cin >> data[i], minx = min(minx, data[i]), maxx = max(maxx, data[i]);
		if(k == 1)cout << minx << endl;
		else if(k >= 3) cout << maxx << endl;
		else
		{
			vi p;
			for(int i = 1;i <= n; i ++)
				if(data[i] == maxx)
					p.push_back(i);
			int psize = p.size();
			if(p[0] == 1 || p[psize - 1] == n)
				cout << maxx << endl;
			else
			{
				ll ans = -1000000009, sz;
/*
				for(int i = 0;i < psize;i ++)
				{
					sz = data[p[i]];
					cout << sz << endl;
					for(int )
					ans = max(sz, ans);
				}
*/
				bool isu = false;
				for(int i = 1;i <= n; i ++)
				{
					if(data[i] == maxx)
						isu = true;
					else sz = min(sz, data[i]);
					if(isu)
					{
						 ans = max(sz, ans);
						 isu = false;
						 sz = maxx;
					}
				}
				ans = max(sz, ans);
				cout << ans << endl;
			}
		}
	}
	return 0;
}


//#define NOSTDCPP
#ifndef NOSTDCPP

#include <bits/stdc++.h>

#else

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <queue>
#include <stack>

#endif

#define RESET(_) memset(_, 0, sizeof(_))
#define RESET_(_, val) memset(_, val, sizeof(_))
#define mp make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef vector <int> vi;
typedef set <int> si;
typedef pair <int, int> pii;

const int MOD = 1e9 + 7;
const int maxn = 300009;
const int maxm = 300009;
const double pi = acos(-1.0);
const double eps = 1e-6;

int n, k;
ll data[maxn];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	while(cin >> n >> k)
	{
		ll minx = (1 << 30), maxx = -(1 << 30);
		for(int i = 1;i <= n;i ++)
			cin >> data[i], minx = min(minx, data[i]), maxx = max(maxx, data[i]);
		if(k == 1)cout << minx << endl;
		else cout << maxx << endl;
	}
	return 0;
}


//#define NOSTDCPP
#ifndef NOSTDCPP

#include <bits/stdc++.h>

#else

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <queue>
#include <stack>

#endif

#define RESET(_) memset(_, 0, sizeof(_))
#define RESET_(_, val) memset(_, val, sizeof(_))
#define mp make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef vector <int> vi;
typedef set <int> si;
typedef pair <int, int> pii;

const int MOD = 1e9 + 7;
const int maxn = 300009;
const int maxm = 300009;
const double pi = acos(-1.0);
const double eps = 1e-6;

int n, k;
ll data[maxn];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	while(cin >> n >> k)
	{
		ll minx = (1 << 23), maxx = -(1 << 23);
		for(int i = 1;i <= n;i ++)
			cin >> data[i], minx = min(minx, data[i]), maxx = max(maxx, data[i]);
		if(k == 1)cout << minx << endl;
		else cout << maxx << endl;
	}
	return 0;
}


//#define NOSTDCPP
#ifndef NOSTDCPP

#include <bits/stdc++.h>

#else

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <queue>
#include <stack>

#endif

#define RESET(_) memset(_, 0, sizeof(_))
#define RESET_(_, val) memset(_, val, sizeof(_))
#define mp make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef vector <int> vi;
typedef set <int> si;
typedef pair <int, int> pii;

const int MOD = 1e9 + 7;
const int maxn = 300009;
const int maxm = 300009;
const double pi = acos(-1.0);
const double eps = 1e-6;

int n, k;
ll data[maxn];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	while(cin >> n >> k)
	{
		cin >> data[1];
		ll minx = data[1], maxx = data[1];
		for(int i = 2;i <= n;i ++)
			cin >> data[i], minx = min(minx, data[i]), maxx = max(maxx, data[i]);
		if(k == 1)cout << minx << endl;
		else if(k >= 3) cout << maxx << endl;
		else
		{
			vi p;
			for(int i = 1;i <= n; i ++)
				if(data[i] == maxx)
					p.push_back(i);
			int psize = p.size();
			if(p[0] == 1 || p[psize - 1] == n)
				cout << maxx << endl;
			else
			{
				//ll ans = -1000000009, sz;
/*
				for(int i = 0;i < psize;i ++)
				{
					sz = data[p[i]];
					cout << sz << endl;
					for(int )
					ans = max(sz, ans);
				}

				bool isu = false;
				for(int i = 1;i <= n; i ++)
				{
					if(data[i] == maxx)
						isu = true;
					else sz = min(sz, data[i]);
					if(isu)
					{
						 ans = max(sz, ans);
						 isu = false;
						 sz = maxx;
					}
				}
				ans = max(sz, ans);
*/
				ll ans = data[1], min1 = data[1], min2 = data[n];
				for(int i = 1;i <= n;i ++)
					min1 = min(min1, data[i]), ans = max(min1, ans);
				for(int i = n;i >= 1;i --)
					min2 = min(min2, data[i]), ans = max(ans, min2);
				cout << ans << endl;
			}
		}
	}
	return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

const int MAX_N = 100000 + 10;

int main() {
    int k = 0, n = 0;
    int a[MAX_N] = {0};
    scanf("%d %d", &n, &k);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    int ans = 0;
    if (k == 1) {
        ans = a[0];
        for (int i = 1; i < n; i++) {
            ans = min(a[i], ans);
        }
    } else if (k == 2) {
        int left[MAX_N] = {0}, right[MAX_N] = {0};
        left[0] = a[0];
        for (int i = 1; i < n; i++) {
            left[i] = min(a[i], left[i - 1]);
        }
        right[n - 2] = a[n - 1];
        for (int i = n - 3; i >= 0; i--) {
            right[i] = min(a[i + 1], right[i + 2]);
        }
        ans = max(left[0], right[0]);
        for (int i = 0; i < n - 1; i++) {
            ans = max(ans, max(left[i], right[i]));
        }
    } else {
        ans = a[0];
        for (int i = 1; i < n; i++) {
            ans = max(a[i], ans);
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

const int MAX_N = 100000 + 10;

int main() {
    int k = 0, n = 0;
    int a[MAX_N] = {0};
    scanf("%d %d", &n, &k);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    int ans = 0;
    if (k == 1) {
        ans = a[0];
        for (int i = 1; i < n; i++) {
            ans = min(a[i], ans);
        }
    } else if (k == 2) {
        ans = min (a[0], a[n - 1]);
    } else {
        ans = a[0];
        for (int i = 1; i < n; i++) {
            ans = max(a[i], ans);
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

const int MAX_N = 100000 + 10;

int main() {
    int k = 0, n = 0;
    int a[MAX_N] = {0};
    scanf("%d %d", &n, &k);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    int ans = 0;
    if (k == 1) {
        ans = a[0];
        for (int i = 1; i < n; i++) {
            ans = min(a[i], ans);
        }
    } else if (k == 2) {
        ans = max(a[0], a[n - 1]);
    } else {
        ans = a[0];
        for (int i = 1; i < n; i++) {
            ans = max(a[i], ans);
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

const int MAX_N = 100000 + 10;

int main() {
    int k = 0, n = 0;
    int a[MAX_N] = {0};
    scanf("%d %d", &n, &k);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    int ans = 0;
    if (k == 1) {
        ans = a[0];
        for (int i = 1; i < n; i++) {
            ans = min(a[i], ans);
        }
    } else if (k == 2) {
        int left[MAX_N] = {0}, right[MAX_N] = {0};
        left[0] = a[0];
        for (int i = 1; i < n; i++) {
            left[i] = min(a[i], left[i - 1]);
        }
        right[n - 2] = a[n - 1];
        for (int i = n - 3; i >= 0; i--) {
            right[i] = min(a[i + 1], right[i + 1]);
        }
        ans = max(left[0], right[0]);
        for (int i = 0; i < n - 1; i++) {
            ans = max(ans, max(left[i], right[i]));
        }
    } else {
        ans = a[0];
        for (int i = 1; i < n; i++) {
            ans = max(a[i], ans);
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 100;
int a[maxn];
int pre[maxn];
int las[maxn];
int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    int min1, max1;
    min1 = inf;
    max1 = -inf;
    int pos;
    pre[0] = inf;
    las[n + 1] = inf;
    for(int i = 1; i <= n; i ++)
    {
       scanf("%d", &a[i]);
       max1 = max(a[i], max1);
       min1 = min(a[i], min1);
    }
    if(m >= 3)
    {
        printf("%d\n", max1);
    }
    else
    if(m == 2)
    {
        printf("%d\n", max(a[1], a[n]));
    }
    else
    {
        printf("%d\n",min1);
    }

}


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 100;
int a[maxn];
int pre[maxn];
int las[maxn];
int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    int min1, max1;
    min1 = inf;
    max1 = -1;
    int pos;
    pre[0] = inf;
    las[n + 1] = inf;
    for(int i = 1; i <= n; i ++)
    {
       scanf("%d", &a[i]);
       pre[i] = min(pre[i - 1], a[i]);
       max1 = max(a[i], max1);
       min1 = min(a[i], min1);
    }
    for(int i = n; i >= 1; i --)
    {
        las[i] = min(las[i + 1], a[i]);
    }
    if(m >= 3)
    {
        printf("%d\n", max1);
    }
    else
    if(m == 2)
    {
        printf("%d\n", max(a[1], a[n]));
    }
    else
    {
        printf("%d\n",min1);
    }

}


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 100;
int a[maxn];
int pre[maxn];
int las[maxn];
int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    int min1, max1;
    min1 = inf;
    max1 = -1;
    int pos;
    pre[0] = inf;
    las[n + 1] = inf;
    for(int i = 1; i <= n; i ++)
    {
       scanf("%d", &a[i]);
       pre[i] = min(pre[i - 1], a[i]);
       max1 = max(a[i], max1);
       min1 = min(a[i], min1);
    }
    for(int i = n; i >= 1; i --)
    {
        las[i] = min(las[i + 1], a[i]);
    }
    if(m >= 3)
    {
        printf("%d", max1);
    }
    else
    if(m == 2)
    {
        int ans = -1;
        for(int i = 1; i < n; i ++)
        {
            ans = max(pre[i], las[i + 1]);
        }
        printf("%d\n", ans);
    }
    else
    {
        printf("%d",min1);
    }

}


#include<cstdio>
int main()
{
	int n,k;
	scanf("%d %d",&n,&k);
	int max=-1e9,min=1e9;
	for(int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x);
		if(x>max) max=x;
		if(x<min) min=x;
	}
	if(k==1) {printf("%d",min); return 0;}
	printf("%d",max);
	return 0;
}


#include<cstdio>
int main()
{
	int n,k;
	scanf("%d %d",&n,&k);
	int max=-1e10,min=1e10;
	for(int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x);
		if(x>max) max=x;
		if(x<min) min=x;
	}
	if(k==1) {printf("%d",min); return 0;}
	printf("%d",max);
	return 0;
}


#include<cstdio>
#include<algorithm>
int num[100005];
int l[100005];
int r[100005];
int n,k;
using namespace std;
int main()
{
	int ans=-1e9;
	scanf("%d %d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&num[i]);
		l[i]=(i==1?num[i]:min(num[i],l[i-1]));
		ans=max(ans,num[i]);
	}
	for(int i=n;i>=1;i--)
	{
		r[i]=(i==n?num[i]:min(num[i],r[i+1]));
	}
	if(k==1) {printf("%d",l[n]); return 0;}
	if(k==2) {int ans2=-1e9;for(int i=1;i<=n;i++) ans2=max(ans2,max(l[i],r[i])); printf("%d",ans2);return 0;}
	if(k>2) {printf("%d",ans);}
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <queue>
#include <climits>

using namespace std;

const int maxn = 1e5 + 5;
const int mod = 1e8 + 7;
int a[maxn],b[maxn],n,k,num,mininum,mininum1,mininum2,ans = INT_MIN;

inline void find(int l,int r)
{
	if(l == r) return;
	else
	{
		mininum = mininum1 = mininum2 = INT_MAX;
		// ans = INT_MIN;
		int m = l;
		for(int i = 1; i <= m; i++)
		{
			mininum1 = min(mininum1,a[i]);
		}
		for(int i = m + 1; i <= r; i++)
		{
			mininum2 = min(mininum2,a[i]);
		}
		mininum = max(mininum1,mininum2);
		ans = max(ans,mininum);
		find(m + 1,r);
	}
}

int main()
{
	scanf("%d%d",&n,&k);
	for(int i = 1; i <= n; i++)
	{
		scanf("%d",&a[i]);
		a[i] %= mod;
	}
	if(k == 1)
	{
		ans = INT_MAX;
		for(int i = 1; i <= n; i++) ans = min(ans,a[i]);
	}
	else if(k == 2)
	{
		find(1,n);
	}
	else
	{
		for(int i = 1; i <= n; i++) ans = max(ans,a[i]);
	}
	printf("%d",ans);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <queue>
#include <climits>

using namespace std;

const int maxn = 1e5 + 5;
const int mod = 1e8 + 7;
int a[maxn],b[maxn],c[maxn],n,k,num,mininum,mininum1,mininum2,ans = INT_MIN;

inline void find(int l,int r)
{
	if(l == r) return;
	else
	{
		mininum = mininum1 = mininum2 = INT_MAX;
		// ans = INT_MIN;
		int m = l;
		for(int i = 1; i <= m; i++)
		{
			mininum1 = min(mininum1,a[i]);
		}
		for(int i = m + 1; i <= r; i++)
		{
			mininum2 = min(mininum2,a[i]);
		}
		mininum = max(mininum1,mininum2);
		ans = max(ans,mininum);
		find(m + 1,r);
	}
}

int main()
{
	scanf("%d%d",&n,&k);
	for(int i = 1; i <= n; i++)
	{
		scanf("%d",&b[i]);
		a[i] = b[i];
		a[i] %= mod;
		c[a[i]] = b[i];
	}
	if(k == 1)
	{
		ans = INT_MAX;
		for(int i = 1; i <= n; i++) ans = min(ans,a[i]);
	}
	else if(k == 2)
	{
		find(1,n);
	}
	else
	{
		for(int i = 1; i <= n; i++) ans = max(ans,a[i]);
	}
	printf("%d",c[ans]);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <queue>
#include <climits>

using namespace std;

const int maxn = 1e5 + 5;
int a[maxn],b[maxn],c[maxn],n,k,num,mininum,mininum1,mininum2,ans;

/*inline void find(int l,int r)
{
	if(l == r) return;
	else
	{
		mininum = mininum1 = mininum2 = INT_MAX;
		// ans = INT_MIN;
		int m = l;
		for(int i = 1; i <= m; i++)
		{
			mininum1 = min(mininum1,a[i]);
		}
		for(int i = m + 1; i <= r; i++)
		{
			mininum2 = min(mininum2,a[i]);
		}
		mininum = max(mininum1,mininum2);
		ans = max(ans,mininum);
		find(m + 1,r);
	}
}*/

inline void find()
{
	b[1] = a[1];
	for(int i = 2; i <= n; i++)
	{
		b[i] = min(b[i - 1],a[i]);
	}
	c[n] = a[n];
	for(int i = n - 1; i >= 1; i--)
	{
		c[i] = min(c[i + 1],a[i]);
	}
	ans = max(b[1],c[2]);
	for(int i = 2; i < n; i++)
	{
		ans = max(ans,max(b[i],c[i + 1]));
	}
}

int main()
{
	scanf("%d%d",&n,&k);
	for(int i = 1; i <= n; i++)
	{
		scanf("%d",&a[i]);
	}
	if(k == 1)
	{
		ans = a[1];
		for(int i = 1; i <= n; i++) ans = min(ans,a[i]);
	}
	else if(k == 2)
	{
		find();
	}
	else
	{
		ans = a[1];
		for(int i = 1; i <= n; i++) ans = max(ans,a[i]);
	}
	printf("%d",c[ans]);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <queue>
#include <climits>

using namespace std;

const int maxn = 1e5 + 5;
int a[maxn],b[maxn],n,k,num,mininum,mininum1,mininum2,ans = INT_MIN;

inline void find(int l,int r)
{
	if(l == r) return;
	else
	{
		mininum = mininum1 = mininum2 = INT_MAX;
		// ans = INT_MIN;
		int m = l;
		for(int i = 1; i <= m; i++)
		{
			mininum1 = min(mininum1,a[i]);
		}
		for(int i = m + 1; i <= r; i++)
		{
			mininum2 = min(mininum2,a[i]);
		}
		mininum = max(mininum1,mininum2);
		ans = max(ans,mininum);
		find(m + 1,r);
	}
}

int main()
{
	scanf("%d%d",&n,&k);
	for(int i = 1; i <= n; i++)
	{
		scanf("%d",&a[i]);
	}
	if(k == 1)
	{
		ans = INT_MAX;
		for(int i = 1; i <= n; i++) ans = min(ans,a[i]);
	}
	else if(k == 2)
	{
		find(1,n);
	}
	else
	{
		for(int i = 1; i <= n; i++) ans = max(ans,a[i]);
	}
	printf("%d",ans);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <queue>
#include <climits>

using namespace std;

const int maxn = 1e5 + 5;
int a[maxn],b[maxn],c[maxn],n,k,ans = INT_MIN;

/*inline void find(int l,int r)
{
	if(l == r) return;
	else
	{
		mininum = mininum1 = mininum2 = INT_MAX;
		// ans = INT_MIN;
		int m = l;
		for(int i = 1; i <= m; i++)
		{
			mininum1 = min(mininum1,a[i]);
		}
		for(int i = m + 1; i <= r; i++)
		{
			mininum2 = min(mininum2,a[i]);
		}
		mininum = max(mininum1,mininum2);
		ans = max(ans,mininum);
		find(m + 1,r);
	}
}*/

inline void find()
{
	b[1] = a[1];
	for(int i = 2; i <= n; i++)
	{
		b[i] = min(b[i - 1],a[i]);
	}
	c[n] = a[n];
	for(int i = n - 1; i >= 1; i--)
	{
		c[i] = min(c[i + 1],a[i]);
	}
	ans = max(b[1],c[2]);
	for(int i = 2; i < n; i++)
	{
		ans = max(ans,max(b[i],c[i + 1]));
	}
}

int main()
{
	scanf("%d%d",&n,&k);
	for(int i = 1; i <= n; i++)
	{
		scanf("%d",&a[i]);
	}
	if(k == 1)
	{
		ans = a[1];
		for(int i = 1; i <= n; i++) ans = min(ans,a[i]);
	}
	else if(k == 2)
	{
		find();
	}
	else
	{
		ans = a[1];
		for(int i = 1; i <= n; i++) ans = max(ans,a[i]);
	}
	printf("%d",ans);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <queue>
#include <climits>

using namespace std;

const int maxn = 1e5 + 5;
int a[maxn],b[maxn],n,k,num,mininum,mininum1,mininum2,ans;

inline void find(int l,int r)
{
	if(l == r) return;
	else
	{
		mininum = mininum1 = mininum2 = INT_MAX;
		ans = INT_MIN;
		int m = l;
		for(int i = 1; i <= m; i++)
		{
			mininum1 = min(mininum1,a[i]);
		}
		for(int i = m + 1; i <= r; i++)
		{
			mininum2 = min(mininum2,a[i]);
		}
		mininum = max(mininum1,mininum2);
		ans = max(ans,mininum);
		find(m + 1,r);
	}
}

int main()
{
	scanf("%d%d",&n,&k);
	for(int i = 1; i <= n; i++)
	{
		scanf("%d",&a[i]);
	}
	if(k == 1)
	{
		ans = INT_MAX;
		for(int i = 1; i <= n; i++) ans = min(ans,a[i]);
	}
	else if(k == 2)
	{
		find(1,n);
	}
	else
	{
		for(int i = 1; i <= n; i++) ans = max(ans,a[i]);
	}
	printf("%d",ans);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <queue>
#include <climits>

using namespace std;

const int maxn = 1e5 + 5;
const int mod = 1e8 + 7;
int a[maxn],b[maxn],c[maxn],n,k,num,mininum,mininum1,mininum2,ans = INT_MIN;

inline void find(int l,int r)
{
	if(l == r) return;
	else
	{
		mininum = mininum1 = mininum2 = INT_MAX;
		// ans = INT_MIN;
		int m = l;
		for(int i = 1; i <= m; i++)
		{
			mininum1 = min(mininum1,a[i]);
		}
		for(int i = m + 1; i <= r; i++)
		{
			mininum2 = min(mininum2,a[i]);
		}
		mininum = max(mininum1,mininum2);
		ans = max(ans,mininum);
		find(m + 1,r);
	}
}

int main()
{
	scanf("%d%d",&n,&k);
	for(int i = 1; i <= n; i++)
	{
		scanf("%d",&b[i]);
		a[i] = b[i];
		a[i] %= mod;
		c[a[i]] = b[i];
	}
	if(k == 1)
	{
		ans = INT_MAX;
		for(int i = 1; i <= n; i++) ans = min(ans,b[i]);
	}
	else if(k == 2)
	{
		find(1,n);
	}
	else
	{
		for(int i = 1; i <= n; i++) ans = max(ans,b[i]);
	}
	printf("%d",c[ans]);
	return 0;
}


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

const int N = 1e5;

int a[N+10],n,k,premi[N+10],aftermi[N+10];

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    scanf("%d%d",&n,&k);
    for (int i = 1;i <= n;i++)
        scanf("%d",&a[i]);
    if (k==1)
    {
        int ans = a[1];
        for (int i = 2;i <= n;i++)
            ans = min(ans,a[i]);
        printf("%d\n",ans);
    }else if (k==2)
    {
        premi[1] = a[1];
        for (int i = 2;i <= n;i++)
            premi[i] = min(premi[i-1],a[i]);
        aftermi[n] = a[n];
        for (int i = n-1;i >= 1;i--)
            aftermi[i] = min(aftermi[i+1],a[i]);
        int ans = max(premi[1],aftermi[2]);
        for (int i = 2;i <= n-1;i++)
            ans = max(ans,max(premi[i],aftermi[i+1]));
        printf("%d\n",ans);
    }else {
        int ans = a[1];
        for (int i = 1;i <= n;i++)
            ans = max(ans,a[i]);
        printf("%d\n",ans);
    }
    return 0;
}


/*input
5 1
-4 -5 -3 -2 -1
*/
#include <bits/stdc++.h>
using namespace std;

int main() {
	int n,k,p;
	vector<int> arr,brr;
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>p;
		arr.push_back(p);
	}
	sort(arr.begin(),arr.end());
	if(k==1)cout<<arr[0]<<endl;
	else if(k>3) cout<<arr[n-1]<<endl;
	else{
		cout<<arr[1]<<endl;
	}
}


/*input
7 2
1 2 3 4 4 6 7
*/
#include <bits/stdc++.h>
using namespace std;

int main() {
	int n,k,p;
	vector<int> arr;
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>p;
		arr.push_back(p);
	}
	sort(arr.begin(),arr.end());
	for(int i=0;i< (k-n%k)%k;i++){
		arr.push_back(arr[n-1]);
	}
	p = arr.size();
	cout<<arr[p-k]<<endl;
}


/*input
5 1
-4 -5 -3 -2 -1
*/
#include <bits/stdc++.h>
using namespace std;

int main() {
	int n,k,p;
	vector<int> arr;
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>p;
		arr.push_back(p);
	}
	sort(arr.begin(),arr.end());
	if(k==1)cout<<arr[0]<<endl;
	else cout<<arr[n-1]<<endl;
}


/*input
5 2
5 3 2 4 1
*/
#include <bits/stdc++.h>
using namespace std;

int main() {
	int n,k,p,cur;
	vector<int> arr,brr,crr;
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>p;
		arr.push_back(p);
		brr.push_back(p);
		crr.push_back(p);
	}
	sort(arr.begin(),arr.end());
	if(k==1)cout<<arr[0]<<endl;
	else if(k>=3) cout<<arr[n-1]<<endl;
	else{
		cur = brr[0];
		for(int i=0;i<n;i++){
			cur = min(cur,brr[i]);
			brr[i] = cur;
		}

		cur = crr[n-1];
		for(int i=n-1;i>=0;i--){
			cur = min(cur,crr[i]);
			crr[i] = cur;
		}
		cur = max(crr[0],brr[0]);
		for(int i=0;i<n;i++){
			cur = max(cur, max(crr[i],brr[i]));
		}
		cout<<cur<<endl;
	}
}


#include <iostream>
#include <algorithm>

int main(void)
{
	int k, n;
	std::cin >> n >> k;

	if (k == 1) {
		long long min = 1000000001;
		for (int i = 0; i < n; ++i) {
			long long tmp;
			std::cin >> tmp;
			if (min > tmp)
				min = tmp;
		}
		std::cout << min << "\n";
	} else if (k == 2) {
		if (n < 2) {
			long long tmp;
			std::cin >> tmp;
			std::cout << tmp;
			return 0;
		}

		long long min1 = 1000000001;
		long long min2 = 1000000001;

		long long *arr = new long long[n];
		long long *arr1 = new long long[n];
		long long *arr2 = new long long[n];

		for (int i = 0; i < n; ++i) {
			std::cin >> arr[i];
		}

		for (int i = 0; i < n; ++i) {
			if (min1 > arr[i]) {
				min1 = arr[i];
				arr1[i] = min1;
			}
		}

		for (int i = n - 1; i >= 0; --i) {
			if (min2 > arr[i]) {
				min2 = arr[i];
				arr2[i] = min2;
			}
		}

		long long max_all = -1000000001;
		for (int i = 0; i < n; ++i) {
			long long tmp = std::max(arr1[i], arr2[n - 1 - i]);
			if (tmp > max_all)
				max_all = tmp;
		}

		/*
		for (int i = 0; i < n; ++i) {
			std::cout << arr1[i] << " " << arr2[i] << "\n";
		}
		*/

		delete arr;
		delete arr1;
		delete arr2;
		std::cout << max_all << "\n";

	} else {
		long long max = -1000000001;
		for (int i = 0; i < n; ++i) {
			long long tmp;
			std::cin >> tmp;
			if (max < tmp) {
				max = tmp;
				//std::cout << "change max to: " << max << "\n";
			}
		}
		std::cout << max << "\n";
	}
}


#include <iostream>
#include <algorithm>

int main(void)
{
	int k, n;
	std::cin >> n >> k;

	if (k == 1) {
		long long min = 1000000001;
		for (int i = 0; i < n; ++i) {
			long long tmp;
			std::cin >> tmp;
			if (min > tmp)
				min = tmp;
		}
		std::cout << min << "\n";
	} else if (k == 2) {
		if (n < 2) {
			long long tmp;
			std::cin >> tmp;
			std::cout << tmp;
			return 0;
		}

		long long min1 = 1000000001;
		long long min2 = 1000000001;

		long long *arr = new long long[n];
		long long *arr1 = new long long[n];
		long long *arr2 = new long long[n];

		for (int i = 0; i < n; ++i) {
			std::cin >> arr[i];
		}

		for (int i = 0; i < n; ++i) {
			if (min1 > arr[i]) {
				min1 = arr[i];
				arr1[i] = min1;
			}
		}

		for (int i = n - 1; i >= 0; --i) {
			if (min2 > arr[i]) {
				min2 = arr[i];
				arr1[i] = min2;
			}
		}

		long long max_all = -1000000001;
		for (int i = 0; i < n; ++i) {
			long long tmp = std::max(arr1[i], arr2[n - 1 - i]);
			if (tmp > max_all)
				max_all = tmp;
		}

		/*
		for (int i = 0; i < n; ++i) {
			std::cout << arr1[i] << " " << arr2[i] << "\n";
		}
		*/

		delete arr;
		delete arr1;
		delete arr2;
		std::cout << max_all << "\n";

	} else {
		long long max = -1000000001;
		for (int i = 0; i < n; ++i) {
			long long tmp;
			std::cin >> tmp;
			if (max < tmp) {
				max = tmp;
				//std::cout << "change max to: " << max << "\n";
			}
		}
		std::cout << max << "\n";
	}
}


#include <iostream>
#include <algorithm>

int main(void)
{
	int k, n;
	std::cin >> n >> k;

	if (k == 1) {
		long long min = 1000000001;
		for (int i = 0; i < n; ++i) {
			long long tmp;
			std::cin >> tmp;
			if (min > tmp)
				min = tmp;
		}
		std::cout << min << "\n";
	} else if (k == 2) {
		if (n < 2) {
			long long tmp;
			std::cin >> tmp;
			std::cout << tmp;
			return 0;
		}

		long long min1 = 1000000001;
		long long min2 = 1000000001;

		long long *arr = new long long[n];
		long long *arr1 = new long long[n];
		long long *arr2 = new long long[n];

		for (int i = 0; i < n; ++i) {
			std::cin >> arr[i];
		}

		for (int i = 0; i < n; ++i) {
			if (min1 > arr[i]) {
				min1 = arr[i];
				arr1[i] = min1;
			}
		}

		for (int i = n - 1; i >= 0; --i) {
			if (min2 > arr[i]) {
				min1 = arr[i];
				arr1[i] = min1;
			}
		}

		long long max_all = -1000000001;
		for (int i = 0; i < n; ++i) {
			long long tmp = std::max(arr1[i], arr2[n - 1 - i]);
			if (tmp > max_all)
				max_all = tmp;
		}

		/*
		for (int i = 0; i < n; ++i) {
			std::cout << arr1[i] << " " << arr2[i] << "\n";
		}
		*/

		delete arr;
		delete arr1;
		delete arr2;
		std::cout << max_all << "\n";

	} else {
		long long max = -1000000001;
		for (int i = 0; i < n; ++i) {
			long long tmp;
			std::cin >> tmp;
			if (max < tmp) {
				max = tmp;
				//std::cout << "change max to: " << max << "\n";
			}
		}
		std::cout << max << "\n";
	}
}


#include <iostream>
#include <algorithm>

int main(void)
{
	int k, n;
	std::cin >> n >> k;

	if (k == 1) {
		long long min = 1000000001;
		for (int i = 0; i < n; ++i) {
			long long tmp;
			std::cin >> tmp;
			if (min > tmp)
				min = tmp;
		}
		std::cout << min << "\n";
	} else if (k == 2) {
		if (n < 2) {
			long long tmp;
			std::cin >> tmp;
			std::cout << tmp;
			return 0;
		}

		long long min1 = 1000000001;
		long long min2 = 1000000001;

		long long *arr = new long long[n];
		long long *arr1 = new long long[n];
		long long *arr2 = new long long[n];

		for (int i = 0; i < n; ++i) {
			std::cin >> arr[i];
		}

		for (int i = 0; i < n; ++i) {
			if (min1 > arr[i]) {
				min1 = arr[i];
			}
			arr1[i] = min1;
		}

		for (int i = n - 1; i >= 0; --i) {
			if (min2 > arr[i]) {
				min2 = arr[i];
			}
			arr2[i] = min2;
		}

		long long max_all = -1000000001;
		for (int i = 0; i < n; ++i) {
			long long tmp = std::max(arr1[i], arr2[n - 1 - i]);
			if (tmp > max_all)
				max_all = tmp;
		}

		/*
		for (int i = 0; i < n; ++i) {
			std::cout << arr1[i] << " " << arr2[i] << "\n";
		}
		*/

		delete arr;
		delete arr1;
		delete arr2;
		std::cout << max_all << "\n";

	} else {
		long long max = -1000000001;
		for (int i = 0; i < n; ++i) {
			long long tmp;
			std::cin >> tmp;
			if (max < tmp) {
				max = tmp;
				//std::cout << "change max to: " << max << "\n";
			}
		}
		std::cout << max << "\n";
	}
}


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

int main() {
    long long int n,k,a[100005];
    cin>>n>>k;
    for(int i=0;i<n;i++)
    cin>>a[i];
    vector<int> v(a,a+n);
    sort(a,a+n);
    if(n==1)
    cout<<a[0];
    else if(n==2)
    {
        if(k==2)
        {
            if(v[0]<v[n-1])
            cout<<v[n-1];
            else
            cout<<v[0];
        }
        else
        cout<<a[0];

    }
    else
    {
        if(k==1)
            cout<<a[0];
        else if(k==2)
        {
            if(v[0]<v[n-1])
            cout<<v[n-1];
            else
            cout<<v[0];
        }
        else
            cout<<a[n-1];
    }
    return 0;
}


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

int main() {
    long long int n,k,a[100005];
    cin>>n>>k;
    for(int i=0;i<n;i++)
    cin>>a[i];
    vector<int> v(a,a+n);
    sort(a,a+n);
    if(n==1)
    cout<<a[0];
    else if(n==2)
    {
        if(v[0]<v[n-1])
        cout<<v[n-1];
        else
        cout<<v[0];
    }
    else
    {
        if(k==1)
            cout<<a[0];
        else if(k==2)
        {
            if(v[0]<v[n-1])
            cout<<v[n-1];
            else
            cout<<v[0];
        }
        else
            cout<<a[n-1];
    }
    return 0;
}